标签:文件 ref 3.1 nsstring and 经纬度 信息 address array
在文件中导入
#import <CoreLocation/CoreLocation.h>
地理编码:
/** 地理编码 */ - (void)geocoder { CLGeocoder *geocoder=[[CLGeocoder alloc]init]; NSString *addressStr = @"广东省深圳市宝安区";//位置信息 [geocoder geocodeAddressString:addressStr completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) { if (error!=nil || placemarks.count==0) { return ; } //创建placemark对象 CLPlacemark *placemark=[placemarks firstObject]; //经度 NSString *longitude =[NSString stringWithFormat:@"%f",placemark.location.coordinate.longitude]; //纬度 NSString *latitude =[NSString stringWithFormat:@"%f",placemark.location.coordinate.latitude]; NSLog(@"经度:%@,纬度:%@",longitude,latitude); }]; }
地理反编码:
/** 地理反编码 */ - (void)reverseGeocoder{ //创建地理编码对象 CLGeocoder *geocoder=[[CLGeocoder alloc]init]; //经度 NSString *longitude = @"113.23"; //纬度 NSString *latitude = @"23.16"; //创建位置 CLLocation *location=[[CLLocation alloc]initWithLatitude:[latitude floatValue] longitude:[longitude floatValue]]; //反地理编码 [geocoder reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) { //判断是否有错误或者placemarks是否为空 if (error !=nil || placemarks.count==0) { NSLog(@"%@",error); return ; } for (CLPlacemark *placemark in placemarks) { //详细地址 NSString *addressStr = placemark.name; NSLog(@"详细地址1:%@",addressStr); NSLog(@"详细地址2:%@",placemark.addressDictionary); NSLog(@"详细地址3:%@",placemark.locality); } }]; }
Demo:https://github.com/JnKindle/GeocoderAndReverseReocoder
/**
* Author:Jn
* GitHub:https://github.com/JnKindle
* cnblogs:http://www.cnblogs.com/JnKindle
* QQ:1294405741
*/
标签:文件 ref 3.1 nsstring and 经纬度 信息 address array
原文地址:http://www.cnblogs.com/JnKindle/p/6217827.html