iOS 反地理编码国外坐标

MKMapView 在国内使用的是 高德地图
所以当你身在国内时,如果不是通过特殊手段成为国外的 IP,那么是无法反地理编码国外的坐标的。

一般会报如下错误:
Geocode error: Error Domain=GEOErrorDomain Code=-8 "(null)"


有时,我们需要在国内的环境下获取国外坐标的相关信息,那该如何呢?
使用 Google 的反地理编码网址来请求数据

查看文档:
https://developers.google.cn/maps/documentation/geocoding/start#reverse

需要将请求地址中的 https://maps.googleapis.com/maps/api/geocode/json?latlng=40.714224,-73.961452&key=YOUR_API_KEY
maps.googleapis.com 修改为 maps.google.cn 就可以在国内使用。


这样以来,当系统自带的反地理编码出错时,就调用 Google 的来进行反地理编码,就可以在国内的化境下获取国外的坐标信息。

let location = CLLocation(latitude: coordinate.latitude, longitude: coordinate.longitude)
            
geocoder.reverseGeocodeLocation(location, completionHandler: { (placemarks, error) in
       if error == nil {
          // 系统反地理编码成功
       } else {
          // 系统自带反地理编码失败,由 Google 接管。
       }
})

不要忘记开启 Google 的 API
https://console.developers.google.com

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容