地理編碼和反地理編碼

來源:互聯網
上載者:User

標籤:

一、基本概念

  地理編碼:根據給定的地名,獲得具體的位置資訊(比如經緯度、地址的全稱等)

  反地理編碼:根據給定的經緯度,獲得具體的位置資訊

  編碼器CLGeocoder的每個執行個體,同時只能處理一個任務,非同步執行。

二、基本方法

 1. 地理編碼方法

     - (void)geocodeAddressString:(NSString *)addressString completionHandler:(CLGeocodeCompletionHandler)completionHandler;

 2. 反地理編碼方法

    - (void)reverseGeocodeLocation:(CLLocation *)location completionHandler:(CLGeocodeCompletionHandler)completionHandler;

 3. 當地理\反地理編碼完成時,就會調用CLGeocodeCompletionHandler   

   typedef void (^CLGeocodeCompletionHandler)(NSArray *placemarks, NSError *error);

   這個block傳遞2個參數  error :當編碼出錯時(比如編碼不出具體的資訊)有值    placemarks :裡面裝著CLPlacemark對象

    CLPlacemark的字面意思是地標,封裝詳細的地址位置資訊

 3.1 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;城市

三、地理編碼與反地理編碼實現

  1. 首先匯入 <CoreLocation/CoreLocation.h>架構,然後匯入#import <CoreLocation/CoreLocation.h>標頭檔

  2. 建立編碼器對象,並進行懶載入

@property(nonatomic,strong) CLGeocoder *geocoder;- (CLGeocoder *)geocoder{    if (!_geocoder) {        self.geocoder = [[CLGeocoder alloc] init];    }    return _geocoder;}

  

  3. 編碼方法實現

- (void)geocod{    NSLog(@"ddddd");    //1. 獲得輸入的地址    NSString *address = _addressField.text;    if (address.length == 0) return;        //2.開始編碼    [self.geocoder geocodeAddressString:address completionHandler:^(NSArray *placemarks, NSError *error) {        if (error || placemarks.count == 0) {            _detailAddressLabel.text = @"您輸入的地址找不到";        } else{ //編碼成功(找到了具體的位置資訊)            //輸出找的所有地標資訊            for (CLPlacemark *placeM in placemarks) {                NSLog(@"name:%@  locality:%@  country:%@  postalCode:%@",placeM.name, placeM.locality,placeM.country, placeM.postalCode);            }                        //顯示最前面的地標資訊            CLPlacemark *firstPlacemark = [placemarks firstObject];            _detailAddressLabel.text = firstPlacemark.name;                        CLLocationDegrees latitude = firstPlacemark.location.coordinate.latitude;            CLLocationDegrees longitude = firstPlacemark.location.coordinate.longitude;            _latitudeLabel.text = [NSString stringWithFormat:@"%.2f",latitude];            _longgitudeLable.text = [NSString stringWithFormat:@"%.2f",longitude];        }    }];}

 

  4. 反地理編碼實現

   NSString *longTitudeText = _longgitudeField.text;    NSString *latitudeText = _latitudeField.text;    if (longTitudeText.length == 0 || latitudeText.length == 0) return;        CLLocationDegrees latitude = [latitudeText doubleValue];    CLLocationDegrees longtitude = [longTitudeText doubleValue];        //開始反向編碼    CLLocation *location = [[CLLocation alloc] initWithLatitude:latitude longitude:longtitude];    [self.geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {        if (error || placemarks.count == 0) {            _reverseDetailAddressLabel.text = @"您輸入的地址找不到";        } else{            for (CLPlacemark *placeM in placemarks) {                NSLog(@"name:%@  locality:%@  country:%@  postalCode:%@",placeM.name, placeM.locality,placeM.country, placeM.postalCode);            }                        CLPlacemark *firstPlacemark = [placemarks firstObject];            _reverseDetailAddressLabel.text = firstPlacemark.name;                        CLLocationDegrees latitude = firstPlacemark.location.coordinate.latitude;            CLLocationDegrees longitude = firstPlacemark.location.coordinate.longitude;            self.latitudeField.text = [NSString stringWithFormat:@"%.2f", latitude];            self.longgitudeField.text = [NSString stringWithFormat:@"%.2f", longitude];        }    }];

:

   

 

地理編碼和反地理編碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.