一、基本概念
地理编码:根据给定的地名,获得具体的位置信息(比如经纬度、地址的全称等)
反地理编码:根据给定的经纬度,获得具体的位置信息
编码器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]; } }];
效果图: