GeoCoder 地理编码

前言

  • Objective-C

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

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

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

1、GeoCoder 地理编码

1.1 配置

  • Objective-C

    1
    2
    // 包含头文件
    #import <CoreLocation/CoreLocation.h>

1.2 地理编码

  • Objective-C

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    // 声明 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];
    }
    }];

1.3 反地理编码

  • Objective-C

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    // 声明 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];
    }
    }];

2、地理编码信息

  • Objective-C

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    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"]]; // 城市相关信息
文章目录
  1. 1. 前言
  2. 2. 1、GeoCoder 地理编码
    1. 2.1. 1.1 配置
    2. 2.2. 1.2 地理编码
    3. 2.3. 1.3 反地理编码
  3. 3. 2、地理编码信息
隐藏目录