来自 Google Places 地址的核心位置地理编码不适用于意大利城市

Posted

技术标签:

【中文标题】来自 Google Places 地址的核心位置地理编码不适用于意大利城市【英文标题】:Core Location geocoding from Google Places address does not work for Italian cities 【发布时间】:2020-01-28 09:02:06 【问题描述】:

我正在使用 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

我需要 CLGeocoder 才能从我从 Google Places API 获得的地址字符串中提取 CLLocation。这适用于大多数情况,除了意大利城市。地理编码器无法处理完整地址(来自 Google 地方信息结果的示例:Roma, Metropolitan City of Rome, Italy)。发生的情况是您收到 CLGeocoder 错误。

我尝试了什么

我尝试只提取城市+国家名称。这适用于意大利城市,但不适用于同名城市:New York, NY 和 New York, IA 等(您会得到错误的结果)。

另一种解决方案是尝试使用prediction.attributedFullText.string,如果它不起作用,请使用上述解决方法重试。

有人遇到过同样的问题,或者知道如何更优雅地处理它吗?我知道这是一种极端情况,但我认为 Google Places 应该考虑使用 CLGeocoder 的结果。

【问题讨论】:

你到底想要什么?要将地址转换为位置坐标吗? 是的,我需要获取坐标。 【参考方案1】:

您可以使用回调函数轻松获取coordinatesformattedAddress

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 ?? ""



【讨论】:

现在很明显了。我什至没有使用此方法进行测试,因为我认为每次键入时都会调用它,而不是在选择结果时调用。谢谢。

以上是关于来自 Google Places 地址的核心位置地理编码不适用于意大利城市的主要内容,如果未能解决你的问题,请参考以下文章

Google仅来自一个国家/地区的自动填充地址[重复]

查找最近的位置 Google Places API

Google 地图 - Facebook Places 使用 JSON 的虚假位置

javascript 获取当前地址 - Google Places API

Google Places API,获取用户地址

在 Google Places API 中嵌入当前位置(经度和纬度)