//地理编码
__weak typeof(self) weakself = self;
// 1. 创建地理编码对象
CLGeocoder *coder = [CLGeocoder new];
// 2. 准备参数
NSString *address = self.addressTextField.text;
// 3. 进行编码(进行网络请求,网络必须打开状态)
[coder geocodeAddressString:address completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
// 4. 错误处理
if(error != nil && placemarks.count ==0) return ;
// 5. 处理结果
CLPlacemark *placemark = [placemarks firstObject];
weakself.longitudeTextField.text = [NSString stringWithFormat:@"%f",placemark.location.coordinate.longitude];
weakself.latitudeTextField.text = [NSString stringWithFormat:@"%f",placemark.location.coordinate.latitude];
// NSLog(@"addressDictionary:%@",placemark.addressDictionary);
weakself.infoTextView.text = [NSString stringWithFormat:@"%@\n%@\n%@",placemark.name,placemark.locality,placemark.administrativeArea];
/* 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);
// 通讯录地址信息(AddressBookUI framework)
@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; // 国家编码
@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; // 感兴趣的点
__weak typeof(self) weakself = self;
// 1. 创建地理编码对象
CLGeocoder *coder = [CLGeocoder new];
// 2. 准备参数
NSString *address = self.addressTextField.text;
// 3. 进行编码(进行网络请求,网络必须打开状态)
[coder geocodeAddressString:address completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
// 4. 错误处理
if(error != nil && placemarks.count ==0) return ;
// 5. 处理结果
CLPlacemark *placemark = [placemarks firstObject];
weakself.longitudeTextField.text = [NSString stringWithFormat:@"%f",placemark.location.coordinate.longitude];
weakself.latitudeTextField.text = [NSString stringWithFormat:@"%f",placemark.location.coordinate.latitude];
// NSLog(@"addressDictionary:%@",placemark.addressDictionary);
weakself.infoTextView.text = [NSString stringWithFormat:@"%@\n%@\n%@",placemark.name,placemark.locality,placemark.administrativeArea];
/* 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);
// 通讯录地址信息(AddressBookUI framework)
@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; // 国家编码
@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; // 感兴趣的点
*/
//反地理编码
__weak
typeof(self) weakself =
self;
// 1. 创建地理编码对象
CLGeocoder *coder = [CLGeocoder new];
// 2. 准备参数
CLLocation *location = [[CLLocation alloc] initWithLatitude:self.latitudeTextField.text.floatValue longitude:self.longitudeTextField.text.floatValue];
// 3. 进行反编码(进行网络请求,网络必须打开状态)
[coder reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
// 4. 错误处理
// 1. 创建地理编码对象
CLGeocoder *coder = [CLGeocoder new];
// 2. 准备参数
CLLocation *location = [[CLLocation alloc] initWithLatitude:self.latitudeTextField.text.floatValue longitude:self.longitudeTextField.text.floatValue];
// 3. 进行反编码(进行网络请求,网络必须打开状态)
[coder reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
// 4. 错误处理
if(error !=
nil && placemarks.count
==0)
return
;
// 5.
处理结果
CLPlacemark *placemark = [placemarks firstObject];
weakself.longitudeTextField.text = [NSString stringWithFormat:@"%f",placemark.location.coordinate.longitude];
weakself.latitudeTextField.text = [NSString stringWithFormat:@"%f",placemark.location.coordinate.latitude];
// NSLog(@"addressDictionary:%@",placemark.addressDictionary);
weakself.infoTextView.text = [NSString stringWithFormat:@"%@\n%@\n%@",placemark.name,placemark.locality,placemark.administrativeArea];
CLPlacemark *placemark = [placemarks firstObject];
weakself.longitudeTextField.text = [NSString stringWithFormat:@"%f",placemark.location.coordinate.longitude];
weakself.latitudeTextField.text = [NSString stringWithFormat:@"%f",placemark.location.coordinate.latitude];
// NSLog(@"addressDictionary:%@",placemark.addressDictionary);
weakself.infoTextView.text = [NSString stringWithFormat:@"%@\n%@\n%@",placemark.name,placemark.locality,placemark.administrativeArea];
}];

7390

被折叠的 条评论
为什么被折叠?



