当我们在使用地图的时候,无论是MAMap(高德)或是MKMap(苹果),都不可或缺的需要使用定位等服务来将用户位置标记在地图上,
一、逆地理解析
可通过以下有三种方式实现:
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) -> 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”参数来设置多语言。
注意:
1)在模拟器上无法返回正确的数据,只有在真机上运行正常
2)在国内无法解析国外的经纬度,会返回失败
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=纬度,经度" + "client"(客户端标示) + "signature"(签名)
官网:https://developers.google.com/maps/documentation/geocoding/overview
Example:
https://maps.googleapis.com/maps/api/geocode/json?language=zh-CN&latlng=31.236580,121.501130&client=gme-hsbc&signature=UPEgWQdtVLBYPKle8pyXuPqjaL4=
注意:
其中的“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
官网:https://wiki.openstreetmap.org/wiki/Zh-hans:Nominatim
Example:
https://nominatim.openstreetmap.org/reverse?format=json&lat=34.5487429714954&lon=108.1602098644987&zoom=18&addressdetails=1&accept-language=zh
不需要任何配置,使用简单,支持ATS