地理編碼與反地理編碼的簡單介紹

一.地理編碼git

>正向地理編碼服務實現了將中文地址或地名描述轉換爲地球表面上相應位置的功能。web

1建立地理編碼對象:數組

 CLGeocoder *geocoder = [CLGeocoder new];編碼

2調用方法進行地理編碼 geocodeAddressString:spa

3在block中獲取地標對象:CLPlacemarkcode

    [geocoder geocodeAddressString:self.addressTF.text completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {orm

        //placemarks: 地標對象數組       //CLPlacemark: 地標對象對象

        //3.1 防錯處理
       
if (error || placemarks.count == 0) {
           
return ;
        }
       
       
//3.2 獲取數據
       
//一個地名可能對應多個經緯度, 因此未來在作此功能的時候, 須要注意判斷, 應該先讓用戶選擇城市
       
       
for (CLPlacemark *pm in placemarks) {
           
           
self.lagitudeLabel.text = [NSString stringWithFormat:@"%f",pm.location.coordinate.latitude];
           
           
self.longitudeLabel.text = [NSString stringWithFormat:@"%f",pm.location.coordinate.longitude];
           
           
self.detailAddressLabel.text = pm.name;
           
ci

            // 地標對象 --> 位置對象 --> 經緯度對象
           
NSLog(@"latitude: %f longitude: %f",pm.location.coordinate.latitude,pm.location.coordinate.longitude);

           
//地址數據字典
           
NSLog(@"dict: %@", pm.addressDictionary);
string

        

            //詳細地址
           
NSLog(@"name: %@", pm.name);
           
           
//城市
           
NSLog(@"city: %@", pm.locality);

        }

 }];

}

2、反地理編碼

反地理編碼服務實現了將地球表面的地址座標轉換爲標準地址的過程,反地理編碼提供了座標定位引擎,幫助用戶經過地面某個地物的座標值來反向查詢獲得該地物所在的行政區域、所處街道、以及最匹配的標準地址信息。

1. 建立地理編碼對象

2.  建立Location對象

3.  調用方法進行反地理編碼:reverseGeocoderLocation

4. 在block中獲取地標對象:CLPlacemark

#pragma mark 反地理編碼方法

- (IBAction)reverceGeocoder {

    

    //1. 建立地理編碼對象

    CLGeocoder *geocoder = [CLGeocoder new];

    

    //2. 調用反地理編碼方法

    //建立Location對象

    CLLocation *location = [[CLLocation alloc] initWithLatitude:[self.lagitudeTF.text doubleValue] longitude:[self.longitudeTF.text doubleValue]];

    

    [geocoder reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {

        //3. 根據需求獲取對標對象的值

        

        //3.1 防錯處理(一旦看到_Nullable可爲空, 必定要作好判斷)

        if (error || placemarks.count == 0) {

            NSLog(@"解析出錯/或者爲找到對應的城市");

            return;

        }

        

        //3.2 遍歷數據獲取值 --> 反地理編碼通常只有1個結果

        for (CLPlacemark *pm in placemarks) {

            

            

            //獲取城市: pm.locality / pm.addressDictionary[@"City"]

            

            // 獲取城市, 有些地方獲取不到. 暫時使用行政區域來代替一下.

            

            if (pm.locality) {

                self.cityLabel.text = pm.addressDictionary[@"City"];

            } else {

                //行政區域

                self.cityLabel.text = pm.administrativeArea;

            }

            

            

            

            NSLog(@"addressDictionary:%@",pm.addressDictionary[@"City"]);

            

            NSLog(@"locality:%@",pm.locality);

            

            NSLog(@"administrativeArea:%@",pm.administrativeArea);

        }

        

    }];

    

}

相關文章
相關標籤/搜索