MKMapView
在國(guó)內(nèi)使用的是高德地圖
。
所以當(dāng)你身在國(guó)內(nèi)時(shí),如果不是通過(guò)特殊手段成為國(guó)外的 IP辑舷,那么是無(wú)法反地理編碼國(guó)外的坐標(biāo)的。
一般會(huì)報(bào)如下錯(cuò)誤:
Geocode error: Error Domain=GEOErrorDomain Code=-8 "(null)"
有時(shí)槽片,我們需要在國(guó)內(nèi)的環(huán)境下獲取國(guó)外坐標(biāo)的相關(guān)信息何缓,那該如何呢?
使用 Google 的反地理編碼網(wǎng)址來(lái)請(qǐng)求數(shù)據(jù)
查看文檔:
https://developers.google.cn/maps/documentation/geocoding/start#reverse
需要將請(qǐng)求地址中的 https://maps.googleapis.com/maps/api/geocode/json?latlng=40.714224,-73.961452&key=YOUR_API_KEY
maps.googleapis.com
修改為maps.google.cn
就可以在國(guó)內(nèi)使用还栓。
這樣以來(lái)碌廓,當(dāng)系統(tǒng)自帶的反地理編碼出錯(cuò)時(shí),就調(diào)用 Google 的來(lái)進(jìn)行反地理編碼剩盒,就可以在國(guó)內(nèi)的化境下獲取國(guó)外的坐標(biāo)信息谷婆。
let location = CLLocation(latitude: coordinate.latitude, longitude: coordinate.longitude)
geocoder.reverseGeocodeLocation(location, completionHandler: { (placemarks, error) in
if error == nil {
// 系統(tǒng)反地理編碼成功
} else {
// 系統(tǒng)自帶反地理編碼失敗,由 Google 接管辽聊。
}
})
不要忘記開(kāi)啟 Google 的 API
https://console.developers.google.com