SWIFT limit google maps place API autocomplete to certain country

SWIFT limit google maps placeAPI autocomplete to certain country

我已经实施 google 地图位置 API GMSAutocomplete。它工作正常。但是,每次输入地址号码时,我都会看到很多不相关的外国地址。我想知道是否可以对 API 这个地方设置一个限制,这样结果只显示一个国家的地址?例如加拿大?

在我的按钮中点击 func(到 autocompleteViewController)我有:

    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)

我的 viewdidLoad 我有:

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

我的自动完成功能是:

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)

}

我需要类似 "locationManager.distanceFilter = 500" 的东西吗?或者有推荐的方法吗?

根据documentation,您可以进行以下操作:

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

他们在文档中包含了一个非常详细的示例,说明如何添加过滤器。