美文网首页iOS 常见问题汇总
iOS 地理编码反编码

iOS 地理编码反编码

作者: leonardni | 来源:发表于2017-04-12 15:51 被阅读532次

本文目录

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

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

配置

// 包含头文件 #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"]];                      // 城市相关信息

相关文章

  • iOS 地理编码 / 反地理编码

    一、CLGeocoder 地理编码 与 反地理编码 地理编码:根据给定的地名,获得具体的位置信息(比如经纬度、地址...

  • iOS 地理编码反编码

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

  • CLGeocoder

    CLGeocoder(地理编码) 使用CLGeocoder可以完成“地理编码”和“反地理编码”地理编码:根据给定的...

  • 地图和定位(三)

    一、地理编码和反地理编码 地理编码:把地址转为经纬度反地理编码:把经纬度转为地址 二、获取当前城市名称(定位+反地...

  • 地理编码

    地理编码和反地理编码都使用CLGeocoder类来实现. 地理编码使用 geocodeAddressString:...

  • iOS - 定位、地理编码、反地理编码

    #pragma mark---*定位: 一、介绍 1、定位使用CoreLocation框架 2、功能:(1)基础定...

  • 基于CLGeocoder - 反地理编码

    iOS中CoreLocatio框架中的CLGeocoder 类不但为我们提供了地理编码方法,而且还提供了反地理编码...

  • 基于百度API的 - 反地理编码

    iOS中CoreLocatio框架中的CLGeocoder 类不但为我们提供了地理编码方法,而且还提供了反地理编码...

  • 高德地图问题

    1:定位的时候获取用户的省市区位置,通过反地理编码 地理编码与反地理编码 地理编码:根据地址获得相应的经纬度以及详...

  • 学习笔记 - 地图

    1.iOS系统定位 2.地理编码与反编码 3.MapView 4.添加大头针

网友评论

    本文标题:iOS 地理编码反编码

    本文链接:https://www.haomeiwen.com/subject/cdpmattx.html