Geocoder
地理编码 : 人文信息转化为地理信息的过程
反地理编码 : 地理信息转换为人文信息的过程
地理编码步骤:
1.创建地理编码者 (CLGeocoder)
2.地理编码 geocodeAddress...反地理编码步骤:
1.创建地理编码者(CLGeocoder)
2.地理信息转位置对象
3.反地理编码取值 reverseGeocodeLocation地理编码 简单界面搭建:
#import "GeocoderViewController.h"
#import <CoreLocation/CoreLocation.h>
@interface GeocoderViewController ()
// 地址输入框
@property (weak, nonatomic) IBOutlet UITextField *address_TF;
// 纬度文本
@property (weak, nonatomic) IBOutlet UILabel *latitude_Label;
// 经度文本
@property (weak, nonatomic) IBOutlet UILabel *longitude_Label;
// 详情文本
@property (weak, nonatomic) IBOutlet UILabel *detail_Label;
@end
@implementation GeocoderViewController
- (IBAction)geocoderBtnClick:(id)sender {
// 创建地理编码者
CLGeocoder *geocoder = [[CLGeocoder alloc]init];
// 地理编码
[geocoder geocodeAddressString:self.address_TF.text completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
// (泛型) NSArray<CLPlacemark *> * _Nullable placemarks 存放地标对象的数组 _Nullable可能为空,所以需要做非空判断
if ( placemarks.count == 0 || error) {
NSLog(@"查询失败: %@",error);
return ;
}
// 可能有重名,所以结果是一个数组
CLPlacemark *place = placemarks.lastObject;
// 设置数据
self.latitude_Label.text = [NSString stringWithFormat:@"%f",place.location.coordinate.latitude];
self.longitude_Label.text = [NSString stringWithFormat:@"%f",place.location.coordinate.longitude];
self.detail_Label.text = [NSString stringWithFormat:@"%@",place.name];
}];
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
演示结果:
- 反地理编码 简单界面搭建:
#import "ReverseGeocoderViewController.h"
#import <CoreLocation/CoreLocation.h>
@interface ReverseGeocoderViewController ()
@property (weak, nonatomic) IBOutlet UITextField *latitude_TF;
@property (weak, nonatomic) IBOutlet UITextField *longitude_TF;
@property (weak, nonatomic) IBOutlet UILabel *city_Label;
@end
@implementation ReverseGeocoderViewController
- (IBAction)reverseGeoCoderBtnClick:(id)sender {
// 创建地理编码者
CLGeocoder *geocoder = [[CLGeocoder alloc]init];
// 获取位置信息对应的位置对象
CLLocation *location = [[CLLocation alloc]initWithLatitude:[self.latitude_TF.text floatValue] longitude:[self.longitude_TF.text floatValue]];
// 转换人文信息
[geocoder reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
// 非空处理
if (placemarks.count == 0 || error) {
NSLog(@"查询失败");
return ;
}
CLPlacemark *place = placemarks.lastObject;
self.city_Label.text = place.locality;
}];
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
演示结果:
CLPlacemark 地标对象属性介绍:
// 位置对象,包含地理信息
@property (nonatomic, readonly, copy, nullable) CLLocation *location;
// 区域,范围
@property (nonatomic, readonly, copy, nullable) CLRegion *region;
// 时区
@property (nonatomic, readonly, copy, nullable) NSTimeZone *timeZone NS_AVAILABLE(10_11,9_0);
// 地址字典 以字典的形式包含地址(人文信息)
@property (nonatomic, readonly, copy, nullable) NSDictionary *addressDictionary;
// address dictionary properties
@property (nonatomic, readonly, copy, nullable) NSString *name; // 名称
@property (nonatomic, readonly, copy, nullable) NSString *thoroughfare; // 街道名
@property (nonatomic, readonly, copy, nullable) NSString *subThoroughfare; // 门牌号
@property (nonatomic, readonly, copy, nullable) NSString *locality; // 城市
@property (nonatomic, readonly, copy, nullable) NSString *subLocality; // 区
@property (nonatomic, readonly, copy, nullable) NSString *administrativeArea; // 州,省
@property (nonatomic, readonly, copy, nullable) NSString *subAdministrativeArea; // 郡,县
@property (nonatomic, readonly, copy, nullable) NSString *postalCode; // 邮政编码
@property (nonatomic, readonly, copy, nullable) NSString *ISOcountryCode; // ISO国家代号
@property (nonatomic, readonly, copy, nullable) NSString *country; // 国家
@property (nonatomic, readonly, copy, nullable) NSString *inlandWater; // 内河
@property (nonatomic, readonly, copy, nullable) NSString *ocean; // 海洋
@property (nonatomic, readonly, copy, nullable) NSArray<NSString *> *areasOfInterest; // 感兴趣的点