iOS地图 -- 地理编码和反地理编码
2016-09-06 11:23
447 查看
地理编码和反地理编码
用到的类和方法
CLGeocoder--> 地理编码管理器
- (void)geocodeAddressString:(NSString *)addressString completionHandler:(CLGeocodeCompletionHandler)completionHandler;--> 地理编码(将地址关键字 >> 经纬度)
- (void)reverseGeocodeLocation:(CLLocation *)location completionHandler:(CLGeocodeCompletionHandler)completionHandler;--> 反地理编码(将经纬度 >> 详细地址)
CLPlacemark--> 地标对象;包含对应的位置对象,地址名称,城市等等
地理编码和反地理编码的小练习
简单布局练习代码
#import "ViewController.h" #import <CoreLocation/CoreLocation.h> @interface ViewController () /** 地理编码管理器 */ @property (nonatomic, strong) CLGeocoder *geoC; @property (weak, nonatomic) IBOutlet UITextView *addressTV; @property (weak, nonatomic) IBOutlet UITextField *latitudeTF; @property (weak, nonatomic) IBOutlet UITextField *longitudeTF; @end @implementation ViewController #pragma mark - 懒加载 /** 地理编码管理器 */ - (CLGeocoder *)geoC { if (!_geoC) { _geoC = [[CLGeocoder alloc] init]; } return _geoC; } -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { [self.view endEditing:YES]; } // 地理编码(地址关键字 ->经纬度 ) - (IBAction)geoCode { NSString *address = self.addressTV.text; // 容错处理 if([address length] == 0) { return; } // 根据地址关键字, 进行地理编码 [self.geoC geocodeAddressString:address completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) { /** * CLPlacemark : 地标对象 * location : 对应的位置对象 * name : 地址全称 * locality : 城市 * 按相关性进行排序 */ CLPlacemark *pl = [placemarks firstObject]; if(error == nil) { NSLog(@"%f----%f", pl.location.coordinate.latitude, pl.location.coordinate.longitude); NSLog(@"%@", pl.name); self.addressTV.text = pl.name; self.latitudeTF.text = @(pl.location.coordinate.latitude).stringValue; self.longitudeTF.text = @(pl.location.coordinate.longitude).stringValue; } }]; } // 反地理编码(把经纬度---> 详细地址) - (IBAction)reverseGeoCode { double latitude = [self.latitudeTF.text doubleValue]; double longitude = [self.longitudeTF.text doubleValue]; CLLocation *location = [[CLLocation alloc] initWithLatitude:latitude longitude:longitude]; [self.geoC reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) { CLPlacemark *pl = [placemarks firstObject]; if(error == nil) { NSLog(@"%f----%f", pl.location.coordinate.latitude, pl.location.coordinate.longitude); NSLog(@"%@", pl.name); self.addressTV.text = pl.name; self.latitudeTF.text = @(pl.location.coordinate.latitude).stringValue; self.longitudeTF.text = @(pl.location.coordinate.longitude).stringValue; } }]; } @end
相关文章推荐
- iOS百度地图简单应用( iOS地图定位(定位、地理编码与反地理编码、mapView、大头针)
- ios 地图大头针固定在地图中心,用用地图中心点左边逆地理编码,及高德云图周边搜索
- iOS.定位服务与地图应用.02.地理信息反编码
- iOS.定位服务与地图应用.03.地理信息编码查询
- iOS定位服务与地图开发(2)---地理信息反编码
- iOS 原生地图地理编码与反地理编码(详解)
- iOS开发地图定位及地理编码
- iOS开发:高德地图显示、定位、反地理编码
- iOS定位服务与地图开发(3)---地理信息编码查询
- iOS 地图(Map)与地理信息反编码
- iOS开发——高德地图调研之显示地图、定位与地理编码等
- iOS 原生地图地理编码与反地理编码
- iOS开发-地图03-地理编码
- IOS网络笔记--地图内容2(正反向编码)
- iOS 高德地图反向地理编码 onReGeocodeSearchDone 不回调
- ios百度地图定位反地理编码
- 地图定位 - 定位,持续定位设置,比较两个位置的距离 地理编码 反地理编码
- iOS 开发 百度地图 逆地理编码
- IOS_开发技巧_定位反地理编码的一些坑
- iOS之获取经纬度并通过反向地理编码获取详细地址