iOS - GeoCoder 地理编码

前言

    NS_CLASS_AVAILABLE(10_8, 5_0)
    @interface CLGeocoder : NSObject
  • 地理编码

    • 地名 -> 经纬度 等具体位置数据信息。根据给定的位置(通常是地名)确定地理坐标(经、纬度)。
  • 反地理编码

    • 经纬度 -> 地名。可以根据地理坐标(经、纬度)确定位置信息(街道、门牌等)。

1、GeoCoder 地理编码

  • 配置

        // 包含头文件
        #import <CoreLocation/CoreLocation.h>
  • 地理编码

        // 声明 CLGeocoder 对象
        @property (nonatomic, strong) CLGeocoder *geocoder;
    
        // 实例化 CLGeocoder 对象
        self.geocoder = [[CLGeocoder alloc] init];
    
        // 开始编码
        [self.geocoder geocodeAddressString:self.addressField.text 
                          completionHandler:^(NSArray *placemarks, NSError *error) {
    
            // 判断编码是否成功
            if (error || 0 == placemarks.count) {
    
                NSLog(@"erroe = %@, placemarks.count = %ld", error, placemarks.count);
                self.detailAddressLabel.text = @"你输入的地址找不到,可能在火星上";
    
            } else {  // 编码成功(找到了具体的位置信息)
    
                // 输出查询到的所有地标信息
                for (CLPlacemark *placemark in placemarks) {
    
                    NSLog(@"name = %@, locality = %@, country = %@", placemark.name, placemark.locality, placemark.country);
                }
    
                // 显示最前面的地标信息
                CLPlacemark *firstPlacemark = [placemarks firstObject];
    
                self.longitudeLabel.text = [NSString stringWithFormat:@"%.2f", firstPlacemark.location.coordinate.longitude];
                self.latitudeLabel.text = [NSString stringWithFormat:@"%.2f", firstPlacemark.location.coordinate.latitude];
    
                self.detailAddressLabel.text = [NSString stringWithFormat:@"%@,%@,%@", firstPlacemark.name, firstPlacemark.locality, firstPlacemark.country];
            }
        }];
  • 反地理编码

        // 声明 CLGeocoder 对象
        @property (nonatomic, strong)CLGeocoder *geocoder;
    
        // 实例化 CLGeocoder 对象
        self.geocoder = [[CLGeocoder alloc] init];
    
        // 创建 CLLocation 对象
        CLLocation *location = [[CLLocation alloc] initWithLatitude:[self.latitudeField.text doubleValue] 
                                                          longitude:[self.longtitudeField.text doubleValue]];
    
        // 开始反编码
        [self.geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {
    
            // 判断反编码是否成功
            if (error || 0 == placemarks.count) {
    
                NSLog(@"erroe = %@, placemarks.count = %ld", error, placemarks.count);
                self.reverseDetailAddressLabel.text = @"你输入的经纬度找不到,可能在火星上";
    
            } else {  // 反编码成功(找到了具体的位置信息)
    
                // 输出查询到的所有地标信息
                for (CLPlacemark *placemark in placemarks) {
    
                    NSLog(@"name=%@, locality=%@, country=%@", placemark.name, placemark.locality, placemark.country);
                }
    
                // 显示最前面的地标信息
                CLPlacemark *firstPlacemark = [placemarks firstObject];
    
                self.longtitudeField.text = [NSString stringWithFormat:@"%.2f", firstPlacemark.location.coordinate.longitude];
                self.latitudeField.text = [NSString stringWithFormat:@"%.2f", firstPlacemark.location.coordinate.latitude];
    
                self.reverseDetailAddressLabel.text = [NSString stringWithFormat:@"%@,%@,%@", firstPlacemark.name, firstPlacemark.locality, firstPlacemark.country];
            }
        }];
        地理编码信息:
    
            placemark.name,                                                    // 地名
            placemark.thoroughfare,                                            // 街道
            placemark.subThoroughfare,                                         // 街道相关信息,例如门牌等
            placemark.locality,                                                // 城市
            placemark.subLocality,                                             // 城市相关信息,例如标志性建筑
            placemark.administrativeArea,                                      // 州
            placemark.subAdministrativeArea,                                   // 其他行政区域信息
            placemark.postalCode,                                              // 邮编
            placemark.ISOcountryCode,                                          // 国家编码
            placemark.country,                                                 // 国家
            placemark.inlandWater,                                             // 水源、湖泊
            placemark.ocean,                                                   // 海洋
            placemark.areasOfInterest                                          // 关联的或利益相关的地标
    
            placemark.addressDictionary[@"City"]];                             // 城市
            placemark.addressDictionary[@"Country"]];                          // 国家
            placemark.addressDictionary[@"CountryCode"]];                      // 国家编码
            placemark.addressDictionary[@"FormattedAddressLines"][0]];         // 街道
            placemark.addressDictionary[@"Name"]];                             // 地名
            placemark.addressDictionary[@"State"]];                            // 州
            placemark.addressDictionary[@"SubLocality"]];                      // 城市相关信息
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值