Reputation: 1053
I'm attempting to build my first app. I have a UITextField
that I want to have access to the Google Places API to search restaurants nearby. I do not need to view these locations on a map, I just need to return search results that the user can tap to see more detail about the location. What are the steps to integrating the Google Places API after I've installed pod GooglePlaces
?
Upvotes: 0
Views: 1773
Reputation: 401
Actually, if you want to use just a textfield which return establishments around you as a list you can do it like this using google places auto complete.
Follow those steps,
1) install google places https://developers.google.com/places/ios-api/start
2) in your main.storyboard add a view controller then create a ViewController.swift file and link it to the storyboard view controller
3) Click on view controller -> go to Editor in menu -> Embed in -> navigation controller. Now you should get a navigation controller linked to your view controller.
4) Copy paste the below codings into ViewController.swift
import UIKit
import GooglePlaces
class ViewController: UIViewController,UISearchBarDelegate{
var resultsViewController: GMSAutocompleteResultsViewController?
var searchController: UISearchController?
override func viewDidLoad() {
super.viewDidLoad()
resultsViewController = GMSAutocompleteResultsViewController()
resultsViewController?.delegate = self
searchController = UISearchController(searchResultsController: resultsViewController)
searchController!.searchResultsUpdater = resultsViewController
// Put the search bar in the navigation bar.
searchController!.searchBar.sizeToFit()
self.navigationItem.titleView = searchController?.searchBar
// When UISearchController presents the results view, present it in
// this view controller, not one further up the chain.
self.definesPresentationContext = true
// Prevent the navigation bar from being hidden when searching.
searchController!.hidesNavigationBarDuringPresentation = false
searchController!.searchBar.keyboardAppearance = UIKeyboardAppearance.Dark
searchController!.searchBar.barStyle = .Black
let filter = GMSAutocompleteFilter()
filter.country = "LK" //put your country here
filter.type = .Establishment
resultsViewController?.autocompleteFilter = filter
searchController?.searchBar.showsCancelButton = true
searchController?.searchBar.delegate = self
}
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
//self.searchController?.active = true
self.searchController!.searchBar.becomeFirstResponder()
}
func didPresentSearchController(searchController: UISearchController) {
self.searchController!.searchBar.becomeFirstResponder()
}
}
// Handle the user's selection.
extension ViewController: GMSAutocompleteResultsViewControllerDelegate {
func resultsController(resultsController: GMSAutocompleteResultsViewController,didAutocompleteWithPlace place: GMSPlace) {
// Do something with the selected place.
print("Pickup Place name: ", place.name)
print("Pickup Place address: ", place.formattedAddress)
print("Pickup Place attributions: ", place.placeID)
}
func resultsController(resultsController: GMSAutocompleteResultsViewController,
didFailAutocompleteWithError error: NSError){
// TODO: handle the error.
print("Error: ", error.description)
}
// Turn the network activity indicator on and off again.
func didRequestAutocompletePredictionsForResultsController(resultsController: GMSAutocompleteResultsViewController) {
UIApplication.sharedApplication().networkActivityIndicatorVisible = true
}
func didUpdateAutocompletePredictionsForResultsController(resultsController: GMSAutocompleteResultsViewController) {
UIApplication.sharedApplication().networkActivityIndicatorVisible = true
}
}
5) In your app delegate
import GooglePlaces
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
GMSPlacesClient.provideAPIKey(GoogleKey) //add your google key here
return true
}
Hope this helps to you. You can customize colors and all of this. And you can get all the details besides the primary details you getting after selecting a place in this by using the place ID of it.
You will get something like this which is totally customizable,
Upvotes: 1
Reputation: 81
You just need to implement google place API by providing the required keys values to fetch the near by places
Upvotes: 0