swift - 来自 Google Places 地址的核心位置地理编码不适用于意大利城市
问题描述
我正在使用 Google Places API 搜索城市名称。在我的视图控制器中,我符合GMSAutocompleteResultsViewControllerDelegate
:
func resultsController(_ resultsController: GMSAutocompleteResultsViewController,
didSelect prediction: GMSAutocompletePrediction) -> Bool {
let geocoder = CLGeocoder()
geocoder.geocodeAddressString(prediction.attributedFullText.string) { (placemark, error) in }
return true
}
我需要从我从 Google Places API 获得的地址字符串CLGeocoder
中提取。CLLocation
这适用于大多数情况,除了意大利城市。地理编码器无法处理完整地址(来自 Google Places 结果的示例:Roma, Metropolitan City of Rome, Italy)。发生的情况是您收到 CLGeocoder 错误。
我尝试了什么:
我尝试只提取城市+国家名称。这适用于意大利城市,但不适用于同名城市:New York, NY 和 New York, IA 等(你得到错误的结果)。
另一种解决方案是尝试使用prediction.attributedFullText.string
,如果它不起作用,则使用上述解决方法重试。
任何人都有同样的问题或知道如何更优雅地处理它?我知道这是一种极端情况,但我相信 Google Places 应该考虑使用 CLGeocoder 的结果。
解决方案
您可以轻松获取coordinates
并formattedAddress
使用回调函数。
func viewController(_ viewController: GMSAutocompleteViewController, didAutocompleteWith place: GMSPlace) {
print("Place name: \(String(describing: place.name))")
print("Place address: \(String(describing: place.formattedAddress))")
print("Place attributions: \(String(describing: place.attributions))")
dismiss(animated: true, completion: nil)
// get the selected coordinates and address like this
let yourCordniates = place.coordinate
let yourAddress = place.formattedAddress ?? ""
}
推荐阅读
- c# - 拖动一些像素后,GridSplitter 卡住了
- android - Android 在运行时从 XML 获取视图值
- java - Spring + MongoDB - MongoTemplate + Criteria 查询
- python - 无法使用 python + h5py 访问外部链接
- javascript - 如何显示 histogramSlider jquery 插件的 x 轴和 y 轴?
- python - 如何填充和访问 Django ArrayField?
- r - 对列进行分组并创建列表输出
- java - 将 Class 的对象转换为 Map
- javascript - 输入 | 将 JavaScript 函数设置的新值获取到 Controller 公共函数中
- xamarin.forms - Xamarin 形成 SFListview 多选问题