標籤:
一、基本概念
地理編碼:根據給定的地名,獲得具體的位置資訊(比如經緯度、地址的全稱等)
反地理編碼:根據給定的經緯度,獲得具體的位置資訊
編碼器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]; } }];
:
地理編碼和反地理編碼