iOS中地圖之定位的逆地理解析

當(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

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
官網(wǎng):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

不需要任何配置,使用簡(jiǎn)單,支持ATS

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容