地理编码与反地理编码的简单介绍

一.地理编码

>正向地理编码服务实现了将中文地址或地名描述转换为地球表面上相应位置的功能。

1创建地理编码对象:

 CLGeocoder *geocoder = [CLGeocoder new];

2调用方法进行地理编码 geocodeAddressString:

3在block中获取地标对象:CLPlacemark

    [geocoder geocodeAddressString:self.addressTF.text completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {

        //placemarks: 地标对象数组       //CLPlacemark: 地标对象

        //3.1 防错处理
       
if (error || placemarks.count == 0) {
           
return ;
        }
       
       
//3.2 获取数据
       
//一个地名可能对应多个经纬度, 所以将来在做此功能的时候, 需要注意判断, 应该先让用户选择城市
       
       
for (CLPlacemark *pm in placemarks) {
           
           
self.lagitudeLabel.text = [NSString stringWithFormat:@"%f",pm.location.coordinate.latitude];
           
           
self.longitudeLabel.text = [NSString stringWithFormat:@"%f",pm.location.coordinate.longitude];
           
           
self.detailAddressLabel.text = pm.name;
           

            // 地标对象 --> 位置对象 --> 经纬度对象
           
NSLog(@"latitude: %f longitude: %f",pm.location.coordinate.latitude,pm.location.coordinate.longitude);

           
//地址数据字典
           
NSLog(@"dict: %@", pm.addressDictionary);

        

            //详细地址
           
NSLog(@"name: %@", pm.name);
           
           
//城市
           
NSLog(@"city: %@", pm.locality);

        }

 }];

}

二、反地理编码

反地理编码服务实现了将地球表面的地址坐标转换为标准地址的过程,反地理编码提供了坐标定位引擎,帮助用户通过地面某个地物的坐标值来反向查询得到该地物所在的行政区域、所处街道、以及最匹配的标准地址信息。

1. 创建地理编码对象

2.  创建Location对象

3.  调用方法进行反地理编码:reverseGeocoderLocation

4. 在block中获取地标对象:CLPlacemark

#pragma mark 反地理编码方法

- (IBAction)reverceGeocoder {

    

    //1. 创建地理编码对象

    CLGeocoder *geocoder = [CLGeocoder new];

    

    //2. 调用反地理编码方法

    //创建Location对象

    CLLocation *location = [[CLLocation alloc] initWithLatitude:[self.lagitudeTF.text doubleValue] longitude:[self.longitudeTF.text doubleValue]];

    

    [geocoder reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {

        //3. 根据需求获取对标对象的值

        

        //3.1 防错处理(一旦看到_Nullable可为空, 一定要做好判断)

        if (error || placemarks.count == 0) {

            NSLog(@"解析出错/或者为找到对应的城市");

            return;

        }

        

        //3.2 遍历数据获取值 --> 反地理编码一般只有1个结果

        for (CLPlacemark *pm in placemarks) {

            

            

            //获取城市: pm.locality / pm.addressDictionary[@"City"]

            

            // 获取城市, 有些地方获取不到. 暂时使用行政区域来代替一下.

            

            if (pm.locality) {

                self.cityLabel.text = pm.addressDictionary[@"City"];

            } else {

                //行政区域

                self.cityLabel.text = pm.administrativeArea;

            }

            

            

            

            NSLog(@"addressDictionary:%@",pm.addressDictionary[@"City"]);

            

            NSLog(@"locality:%@",pm.locality);

            

            NSLog(@"administrativeArea:%@",pm.administrativeArea);

        }

        

    }];

    

}




转载于:https://my.oschina.net/u/2613740/blog/648433

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值