0

I have implemented google maps place API GMSAutocomplete. It works fine. However, every time when I input a address number I see a lot of foreign addresses which are not relevant. I was wondering if it is possible to set a limit to the place API so the result only shows the addresses in a country? e.g. Canada?

In my button tapped func (to autocompleteViewController) I have:

    let orderVC = GMSAutocompleteViewController()
    orderVC.delegate = self

    let fields: GMSPlaceField = GMSPlaceField(rawValue: UInt(GMSPlaceField.name.rawValue) |
      UInt(GMSPlaceField.placeID.rawValue) | UInt(GMSPlaceField.coordinate.rawValue))!
    orderVC.placeFields = fields

    let filter = GMSAutocompleteFilter()
    filter.type = .address
    orderVC.autocompleteFilter = filter

    present(orderVC, animated: true, completion: nil)

My viewdidLoad I have:

let camera = GMSCameraPosition.camera(withLatitude: 43.6532,
                                          longitude: 79.3832,
                                          zoom: zoomLevel)
    mapView = GMSMapView.map(withFrame: view.bounds, camera: camera)
    mapView.settings.myLocationButton = true
    mapView.settings.compassButton = true
    mapView.autoresizingMask = [.flexibleWidth, .flexibleHeight]

    mapView.isMyLocationEnabled = true

My autocomplete function is:

func viewController(_ viewController: GMSAutocompleteViewController, didAutocompleteWith place: GMSPlace) {
print("Place name: \(place.name ?? "placenameNil")")
print("Place ID: \(place.placeID ?? "placeIdNil")")
print("Place attributions: \(place.attributions)")
print("place cordinate: \(place.coordinate.latitude) \(place.coordinate.longitude)")

dismiss(animated: true, completion: nil)

}

Do I need something like "locationManager.distanceFilter = 500" or is there a recommended method?

4

1 回答 1

1

According to the documentation, you can do the following:

let autocompleteController = GMSAutocompleteViewController()
// other controller initialization you might have

let filter = GMSAutocompleteFilter()
filter.type = .establishment // there are more types if you also need that
filter.country = "US" // this is a standard ISO 3166 country code

autocompleteController.autocompleteFilter = filter

They've included a very detailed example in their documentation on how to add the filter.

于 2020-02-10T19:44:12.220 回答