Ad

How To Use AutocompleteBounds In Swift 3

I want to use AutocompleteViewController in a special area with this Lat and Long

let SOUTHWEST_LATITUDE = 35.60;
   let SOUTHWEST_LONGITUDE = 51.11;
   let NORTHEAST_LATITUDE = 35.80;
   let NORTHEAST_LONGITUDE = 51.60;

and now I don't know how to use them in my code!! here in my code

public func googlePlacesVC() -> UIViewController{
    let autocompleteController = GMSAutocompleteViewController()
     //how to use this ?
    //autocompleteController.autocompleteBounds????
    autocompleteController.delegate = self
    return autocompleteController
  } 
Ad

Answer

You need define a GMSCoordinateBounds with the southWest coordinate and the northEast coordinate, also you need to fix your constants declarations, statics constants are final already, also the type of the constant must be after the constant name, not before

UPDATE Another thing you need to use autocompleteController.autocompleteBoundsMode = .restrict if you want to restrict the results only to this bounds

Full Code

private static let SOUTHWEST_LATITUDE : Double = 35.60;
private static let SOUTHWEST_LONGITUDE : Double  = 51.11;
private static let NORTHEAST_LATITUDE : Double  = 35.80;
private static let NORTHEAST_LONGITUDE : Double = 51.60;


public func googlePlacesVC() -> UIViewController{
    let autocompleteController = GMSAutocompleteViewController()
    let northEastCoordinate = CLLocationCoordinate2D(latitude: ViewController.NORTHEAST_LATITUDE, longitude: ViewController.NORTHEAST_LONGITUDE)
    let southWestCoordinate = CLLocationCoordinate2D(latitude: ViewController.SOUTHWEST_LATITUDE, longitude: ViewController.SOUTHWEST_LONGITUDE)
    let bounds = GMSCoordinateBounds(coordinate: southWestCoordinate, coordinate: northEastCoordinate)
    //how to use this ?
    autocompleteController.autocompleteBounds = bounds
    autocompleteController.autocompleteBoundsMode = .restrict
    autocompleteController.delegate = self
    return autocompleteController
}

this works, was tested ;)

Ad
source: stackoverflow.com
Ad