地理编码和反地理编码
2015-11-08 14:59
351 查看
// // ViewController.m #import "ViewController.h" #import <CoreLocation/CoreLocation.h> @interface ViewController () @property(nonatomic,strong)CLGeocoder *geocoder; #pragma mark - 地理编码 - (IBAction)geocode:(UIButton *)sender; @property (weak, nonatomic) IBOutlet UITextField *addressField; @property (weak, nonatomic) IBOutlet UILabel *longitudeLabel; @property (weak, nonatomic) IBOutlet UILabel *latitudeLabel; @property (weak, nonatomic) IBOutlet UILabel *detailAddressLabel; #pragma mark - 反地理编码 - (IBAction)reverseGeocode:(UIButton *)sender; @property (weak, nonatomic) IBOutlet UITextField *longitudeField; @property (weak, nonatomic) IBOutlet UITextField *latitudeField; @property (weak, nonatomic) IBOutlet UILabel *reverseDatailAddressLabel; @end @implementation ViewController /** * 延迟创建CLGeocoder对象 */ - (CLGeocoder *)geocoder { if (!_geocoder) { self.geocoder = [[CLGeocoder alloc] init]; } return _geocoder; } - (void)viewDidLoad { [super viewDidLoad]; } - (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { //退出键盘 [self.view endEditing:YES]; } /** * 地理编码 */ - (IBAction)geocode:(UIButton *)sender { NSString *address = self.addressField.text; if(address.length == 0) return; [self.geocoder geocodeAddressString:address completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) { if (error) { //有错误(地址乱输入) self.detailAddressLabel.text = @"你找的地址可能只在火星有!!!"; }else{ //编码成功 //取出最前面的地址 CLPlacemark *pm = [placemarks firstObject]; // 设置经纬度 self.latitudeLabel.text = [NSString stringWithFormat:@"%.1f",pm.location.coordinate.latitude]; self.latitudeLabel.text = [NSString stringWithFormat:@"%.1f",pm.location.coordinate.longitude]; //设置具体的位置 self.detailAddressLabel.text = pm.name; // NSLog(@"总共找到%lu个地址",placemarks.count); // // for (CLPlacemark *pm in placemarks) { // NSLog(@"----地址开始----"); // // NSLog(@"%f %f %@",pm.location.coordinate.latitude,pm.location.coordinate.longitude,pm.name); // // //遍历字典 // [pm.addressDictionary enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop) { // NSLog(@"%@ %@",key,obj); // }]; // // NSLog(@"----地址结束----"); // } } }]; } /** * 反地理编码 */ - (IBAction)reverseGeocode:(UIButton *)sender { //1.包装位置 CLLocationDegrees latitude = [self.latitudeLabel.text doubleValue]; CLLocationDegrees longitude = [self.longitudeField.text doubleValue]; CLLocation *loc = [[CLLocation alloc] initWithLatitude:latitude longitude:longitude]; //2.反地理编码 [self.geocoder reverseGeocodeLocation:loc completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) { if (error) { //有错误(地址乱输入) self.reverseDatailAddressLabel.text = @"你找的地址可能只在火星有!!!"; }else{ //编码成功 NSLog(@"总共找到%lu个地址",placemarks.count); for (CLPlacemark *pm in placemarks) { NSLog(@"----地址开始----"); NSLog(@"%f %f %@",pm.location.coordinate.latitude,pm.location.coordinate.longitude,pm.name); //遍历字典 [pm.addressDictionary enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop) { NSLog(@"%@ %@",key,obj); }]; NSLog(@"----地址结束----"); } } }]; } @end
相关文章推荐
- Android Service和Thread的区别
- Python pickle 的 dump() & load()
- 用Gparted调整ubuntu磁盘分区
- lightoj 1245 - Harmonic Number (II) 【数学 计数】
- 第三章 分支结构程序
- objective-c系列-NSString
- 内存溢出(OOM)and内存泄露---及其解决
- Ubuntu14.04+hadoop2.5.2完全分布式集群搭建
- MySQL常用命令
- matrix_world_final_2012
- Light oj1354:IP Checking
- view getwidth() 和getMeasuredWidth()区别
- java中线程安全的讲解
- 程序猿书单--转载
- 转自一个CG大神的文章
- D3.js数据可视化(三)——地图可视化
- 二叉树的各种遍历算法的递归和非递归实现
- S3C2440 NAND Flash的使用
- C++实现动态绑定代码分享
- 一个关于二维指针的问题