标签:
#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)geoCoder { NSString *address = self.addressTV.text; // 容错 if([address length] == 0) return; [self.geoC geocodeAddressString:address completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) { // CLPlacemark : 地标 // location : 位置对象 // addressDictionary : 地址字典 // name : 地址详情 // locality : 城市 if(error == nil) { CLPlacemark *pl = [placemarks firstObject]; self.addressTV.text = pl.name; self.latitudeTF.text = @(pl.location.coordinate.latitude).stringValue; self.longitudeTF.text = @(pl.location.coordinate.longitude).stringValue; }else { NSLog(@"错误"); } }]; } - (IBAction)reverseGeoCoder { // 获取用户输入的经纬度 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) { if(error == nil) { CLPlacemark *pl = [placemarks firstObject]; self.addressTV.text = pl.name; self.latitudeTF.text = @(pl.location.coordinate.latitude).stringValue; self.longitudeTF.text = @(pl.location.coordinate.longitude).stringValue; }else { NSLog(@"错误"); } }]; } @end
标签:
原文地址:http://www.cnblogs.com/yedayi/p/5137683.html