地理编码和反地理编码

  //地理编码
   
__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. 错误处理
        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];
    }];
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值