Swift google autocomplete with local search - ios

I am trying to do Google Autocomplete using Google Places in Swift 3.0. But I need to search depending upon my current location. Example, If I am in Kolkata, India and I type search keyword "Ko" it will show the results of Kolkata first .
Can anyone help me.
Here is my code.I import GooglePlaces in my class
#IBAction func txtFieldLocationDidStartEditing(_ sender: Any) {
self.placeAutocomplete()
}
func placeAutocomplete() {
let autocompleteController = GMSAutocompleteViewController()
autocompleteController.delegate = self
present(autocompleteController, animated: true, completion: nil)
}
// MARK: - autoComplete Delegates
func viewController(_ viewController: GMSAutocompleteViewController, didAutocompleteWith place: GMSPlace) {
print("Place name: \(place.name)")
dismiss(animated: true, completion: nil)
}
func viewController(_ viewController: GMSAutocompleteViewController, didFailAutocompleteWithError error: Error) {
// TODO: handle the error.
print("Error: ", error.localizedDescription)
}
// User canceled the operation.
func wasCancelled(_ viewController: GMSAutocompleteViewController) {
dismiss(animated: true, completion: nil)
}
// Turn the network activity indicator on and off again.
func didRequestAutocompletePredictions(_ viewController: GMSAutocompleteViewController) {
UIApplication.shared.isNetworkActivityIndicatorVisible = true
}
func didUpdateAutocompletePredictions(_ viewController: GMSAutocompleteViewController) {
UIApplication.shared.isNetworkActivityIndicatorVisible = false
}
Please anyone help me to solve it out.
Thanks in advance.

The only API provided by GMSAutocompleteViewController is to set the GMSCoordinateBounds like so (reference):
func placeAutocomplete() {
let visibleRegion = mapView.projection.visibleRegion()
let bounds = GMSCoordinateBounds(coordinate: visibleRegion.farLeft, coordinate: visibleRegion.nearRight)
let autocompleteController = GMSAutocompleteViewController()
acController.autocompleteBounds = bounds
autocompleteController.delegate = self
present(autocompleteController, animated: true, completion: nil)
}

Related

issue with alamofire pod on xcode 9

hello everyone I install the alamofire and google places pod to autocomplete the places search
but it gives me the error:
The “Swift Language Version” (SWIFT_VERSION) build setting must be set to a
supported value for targets that use Swift. This setting can be set in the
build settings editor.
my code:
class SetLocationViewController: UIViewController {
private var placesClient = GMSPlacesClient()
#IBOutlet weak var setLocationTf: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
placesClient = GMSPlacesClient.shared()
}
#IBAction func onClickTf(_ sender: Any) {
setLocationTf.resignFirstResponder()
let acController = GMSAutocompleteViewController()
acController.delegate = self
let filter = GMSAutocompleteFilter()
filter.type = .establishment
filter.countries = ["BR"]
acController.autocompleteFilter = filter
let field: GMSPlaceField = [.name, .placeID]
acController.placeFields = field
present(acController, animated: true, completion: nil)
}
}
extension SetLocationViewController: GMSAutocompleteViewControllerDelegate {
func viewController(_ viewController: GMSAutocompleteViewController, didAutocompleteWith place: GMSPlace) {
if let name = place.name {
setLocationTf.text = name
}
dismiss(animated: true, completion: nil)
}
func viewController(_ viewController: GMSAutocompleteViewController, didFailAutocompleteWithError error: Error) {
print("Error: ", error.localizedDescription)
}
func wasCancelled(_ viewController: GMSAutocompleteViewController) {
dismiss(animated: true, completion: nil)
}
}
The error I got:

How to get the google places automatically

I want display the Google Automatic places in Text field.
I write the following code but I unable to understand where the I give apikey.
Same time I getting the latitude and longitude also for selected address.
import UIKit
import GoogleMaps
import GooglePlaces
class ViewController: UIViewController ,UITextFieldDelegate,GMSAutocompleteViewControllerDelegate{
#IBOutlet weak var placeaddress: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
let apikey = "API_KEY"
self.placeaddress.delegate = self
}
func textFieldDidBeginEditing(_ textField: UITextField) {
let acController = GMSAutocompleteViewController()
acController.delegate = self
self.present(acController, animated: true, completion: nil)
}
// Handle the user's selection.
func viewController(_ viewController: GMSAutocompleteViewController, didAutocompleteWith place: GMSPlace) {
print("Place name: \(place.name)")
print("Place address: \(String(describing: place.formattedAddress))")
print("Place attributions: \(String(describing: place.attributions))")
dismiss(animated: true, completion: nil)
}
func viewController(_ viewController: GMSAutocompleteViewController, didFailAutocompleteWithError error: Error) {
// TODO: handle the error.
print("Error: ", error.localizedDescription)
}
// User canceled the operation.
func wasCancelled(_ viewController: GMSAutocompleteViewController) {
dismiss(animated: true, completion: nil)
}
}
First import Google Framework into appDelegate
import GoogleMaps
After that provide your api key into didFinishLaunchingWithOptions in appDelegate
GMSServices.provideAPIKey("Your API Key")
Hope this will help you.

How to limit Google Places Autocomplete to not include Address?

How to limit Google Places Autocomplete to not include Address? I currently have my setup
class FilterVC: UIViewController, GMSAutocompleteViewControllerDelegate {
// MARK: SEARCH BAR
#IBAction func searchBarAction(_ sender: Any) {
let autocompleteController = GMSAutocompleteViewController()
autocompleteController.delegate = self
placeAutocomplete(resultsViewController: autocompleteController)
UINavigationBar.appearance().barTintColor = UIColor.white
UINavigationBar.appearance().tintColor = UIColor.hiGreyishBrownTwo
UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).defaultTextAttributes = [NSAttributedStringKey.foregroundColor.rawValue: UIColor.hiGreyishBrownTwo]
present(autocompleteController, animated: true, completion: nil)
}
func placeAutocomplete(resultsViewController: GMSAutocompleteViewController) {
var placeClient = GMSPlacesClient()
let filter = GMSAutocompleteFilter()
filter.type = .city
filter.country = "USA"
resultsViewController.autocompleteFilter = filter
func viewController(_ viewController: GMSAutocompleteViewController, didAutocompleteWith place: GMSPlace) {
DataService.instance.place = place
FillAddress(place: place)
fillAddressForm()
print(DataService.instance._address_line1)
print(DataService.instance._city)
print(DataService.instance._postalCode)
print(DataService.instance._state)
print(DataService.instance._country)
DataService.instance.addressLabel = place.formattedAddress
dismiss(animated: true, completion: nil)
}
func wasCancelled(_ viewController: GMSAutocompleteViewController) {
dismiss(animated: true, completion: nil)
}
func viewController(_ viewController: GMSAutocompleteViewController, didFailAutocompleteWithError error: Error) {
print("ERROR \(error) Autocomplete")
}
I tried using a GMSAutocompleteFilter to limit my results to only include state city, zip, country. I don't want to display address in the autocomplete controller. When I do this, it only displays Country and city, I can't enter zip code. How would I make that available? I'm not quite sure what I'm missing or what the next step to take is. Any suggestions would be much appreciated.
I was going to take this off since I found a solution, but just in case some one runs into a similar issue, a very easy fix is to set filter type to region.
func placeAutocomplete(resultsViewController: GMSAutocompleteViewController) {
var placeClient = GMSPlacesClient()
let filter = GMSAutocompleteFilter()
filter.country = "USA"
filter.type = .region
resultsViewController.autocompleteFilter = filter
This will limit the search to not include address. When trying .city, that eliminated to much. Well hope this helps someone in the future.

Google Maps/PlacePicker ios api - is there a way to get nearby places as a GMSplace list?

I'm using place picker to pick a location on a map. After that, I want to get a GMSPlace list of restaurants in a specific radius, centered around the picked location.
Is there any way to implement that using the Google iOS API?
#IBAction func pickPlace(_ sender: UIButton) {
let config = GMSPlacePickerConfig(viewport: nil)
let placePicker = GMSPlacePickerViewController(config: config)
placePicker.delegate = self
present(placePicker, animated: true, completion: nil)
}
func placePicker(_ viewController: GMSPlacePickerViewController, didPick place: GMSPlace){
self.pickedNameLabel.text = place.name
self.pickedAddressLabel.text = place.formattedAddress?.components(separatedBy: ", ").joined(separator: "\n")
viewController.dismiss(animated: true, completion: nil)
}
func placePickerDidCancel(_ viewController: GMSPlacePickerViewController){
viewController.dismiss(animated: true, completion: nil)
}
In didPick delegate fetch the place lat long and pass it to :
https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=-33.8670,151.1957&radius=500&types=food&name=cruise&key=YOUR_API_KEY
Source:
https://developers.google.com/places/web-service/

ios Swift - API GoogleMaps - not conform protocol GSMAutocompleteViewControllerDelegate

I'm trying to use API GoogleMaps in an ios app to make an auto-completion, but I can't get my hand on the problem, I search for it but it seems I'm the only one to have it.
MyViewController doesn't conform to protocol GSMAutocompleteViewControllerDelegate
class MyViewController: UIViewController {
#IBAction func onLaunchClicked(sender: AnyObject) {
let acController = GMSAutocompleteViewController()
acController.delegate = self
self.presentViewController(acController, animated: true, completion: nil)
}
}
extension MyViewController: GMSAutocompleteViewControllerDelegate {
func viewController(viewController: GMSAutocompleteViewController!, didAutocompleteWithPlace place: GMSPlace!) {
// The user has selected a place.
self.dismissViewControllerAnimated(true, completion: nil)
}
func viewController(viewController: GMSAutocompleteViewController!, didAutocompleteWithError error: NSError!) {
self.dismissViewControllerAnimated(true, completion: nil)
}
func wasCancelled(viewController: GMSAutocompleteViewController!) {
self.dismissViewControllerAnimated(true, completion: nil)
}
}
It seems like I don't have all the protocol required, but can't get my hand on it.
Thanks for helping :)
Try adding:
func viewController(viewController: GMSAutocompleteViewController!, didFailAutocompleteWithError error: NSError!) {
}

Resources