當(dāng)我們?cè)谑褂玫貓D的時(shí)候,無論是MAMap(高德)或是MKMap(蘋果),都不可或缺的需要使用定位等服務(wù)來將用戶位置標(biāo)記在地圖上,
一、逆地理解析
可通過以下有三種方式實(shí)現(xiàn):
1、Apple API
API:
open func reverseGeocodeLocation(_ location: CLLocation, completionHandler: @escaping CLGeocodeCompletionHandler)
Or
open func reverseGeocodeLocation(_ location: CLLocation, preferredLocale locale: Locale?, completionHandler: @escaping CLGeocodeCompletionHandler)
Example:
private lazy var geoCoder: CLGeocoder = {
let geoCoder = CLGeocoder()
return geoCoder
}()
func geocodeWithLocation(_ location: CLLocation, completion: @escaping (Swift.Result<LocationInfoResponse, ResponseError>) -> Void) {
geoCoder.reverseGeocodeLocation(location) { result, error in
if let error = error {
completion(.failure(.nsurlError(error)))
return
}
let cityInfo = LocationInfoResponse(country: result?.first?.country, countryCode: result?.first?.isoCountryCode)
completion(.success(cityInfo))
}
}
可通過“Locale”參數(shù)來設(shè)置多語言。
注意:
1)在模擬器上無法返回正確的數(shù)據(jù),只有在真機(jī)上運(yùn)行正常
2)在國(guó)內(nèi)無法解析國(guó)外的經(jīng)緯度,會(huì)返回失敗
2、Google API
API:
https://maps.googleapis.com/maps/api/geocode/json?language=&latlng=&client=&signature=
組成:
Domain = "https://maps.googleapis.com"
Path = "maps/api/geocode/json"
Parameters = "language"(response的語言) + "latlng=緯度,經(jīng)度" + "client"(客戶端標(biāo)示) + "signature"(簽名)
官網(wǎng):https://developers.google.com/maps/documentation/geocoding/overview
注意:
其中的“signature”是通過加密算法得來的
配置起來比較麻煩,不支持ATS
3、WiKi API
API:
https://nominatim.openstreetmap.org/reverse?format=xml&lat=34.5487429714954&lon=108.1602098644987&zoom=18&addressdetails=1&accept-language=zh
官網(wǎng):https://wiki.openstreetmap.org/wiki/Zh-hans:Nominatim
不需要任何配置,使用簡(jiǎn)單,支持ATS