SWIFT 将谷歌地图 placeAPI 自动完成限制到某个国家/地区



我已经实现了谷歌地图放置API GMSAutocomplete。它工作正常。但是,每次我输入地址号码时,我都会看到很多不相关的外国地址。我想知道是否可以对地点 API 设置限制,以便结果仅显示一个国家/地区的地址?例如加拿大?

在我的按钮点击功能(自动完成视图控制器(中,我有:

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)

我的观点加载我有:

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"之类的东西,或者是否有推荐的方法?

根据文档,您可以执行以下操作:

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

他们在文档中包含一个有关如何添加筛选器的非常详细的示例。

相关内容

最新更新