一、簡單說明
clgeocoder:地理編碼器,其中geo是地理的英文單詞geography的簡寫。
1.使用clgeocoder可以完成“地理編碼”和“反地理編碼”
地理編碼:根據給定的地名,獲得具體的位置資訊(比如經緯度、位址的全稱等)
反地理編碼:根據給定的經緯度,獲得具體的位置資訊
(1)地理編碼方法
- (void)geocodeaddressstring:(nsstring *)addressstring completionhandler:(clgeocodecompletionhandler)completionhandler;
(2)反地理編碼方法
- (void)reversegeocodelocation:(cllocation *)location completionhandler:(clgeocodecompletionhandler)completionhandler;
2.clgeocodecompletionhandler
當地理\反地理編碼完成時,就會調用clgeocodecompletionhandler
![](https://img.laitimes.com/img/__Qf2AjLwojIjJCLyojI0JCLicmbw5SN1UjNwgjMwADMyAjMxEzLchDM0EDMy8CX2MTMwUDNvwVavwVbvNmLn9GbiRXauNmLzV2Zh1Wavw1LcpDc0RHaiojIsJye.png)
這個block傳遞2個參數
error :當編碼出錯時(比如編碼不出具體的資訊)有值
placemarks :裡面裝着clplacemark對象
3.clplacemark
說明:clplacemark的字面意思是地标,封裝詳細的位址位置資訊
地理位置 @property (nonatomic, readonly) cllocation *location;
區域 @property (nonatomic, readonly) clregion *region;
詳細的位址資訊 @property (nonatomic, readonly) nsdictionary *addressdictionary;
位址名稱 @property (nonatomic, readonly) nsstring *name;
城市 @property (nonatomic, readonly) nsstring *locality;
二、代碼示例:
在storyboard中搭建界面如下:
實作代碼:
yyviewcontroller.m檔案
實作效果:
(1)地理編碼:(地名->經緯度坐标)
列印輸出:
(2)反地理編碼:(經緯度—>地名)
(3)注意:調整鍵盤
點選經緯度textfield進行輸入的時候,彈出的鍵盤如下
(4)注意:搜尋的所有結果都是在中國境内的,因為蘋果在中國的地圖服務商是高德地圖。