iOS中CoreLocatio框架中的CLGeocoder为我们提供了地理编码方法:

首先需要导入框架

#import <CoreLocation/CoreLocation.h>

地理编码方法有三种:

- (void)geocodeAddressDictionary:(NSDictionary *)addressDictionary completionHandler:(CLGeocodeCompletionHandler)completionHandler;
- (void)geocodeAddressString:(NSString *)addressString completionHandler:(CLGeocodeCompletionHandler)completionHandler;
- (void)geocodeAddressString:(NSString *)addressString inRegion:(nullable CLRegion *)region completionHandler:(CLGeocodeCompletionHandler)completionHandler;

下面简单的介绍其中一种常用的方法<上面提到的第二种方法>:步骤:

1、获取用户输入的地理位置

2、创建地理编码对象<CLGeocoder对象>

3、利用地理编码对象编码

3.1、当编码完成时,会调用 completionHandler 对象,该对象类型为 CLGeocodeCompletionHandler,实际上是一个 block 对象

这个对象中传递了2个参数,其中placemark:里面装了CLPlacemark对象

根据文档,可以知道CLPlacemark 对象中包含了 该位置的经纬度以及城市/区域/国家代码/邮编等等...信息

由此我们可以根据返回参数placemark获取到我们需要的数据

< Demo如下 >:

@interface ViewController ()
#pragma mark - 地理编码
//监听地理编码点击事件
- (IBAction)geocodeBtnClick; // 需要编码的地址
@property (weak, nonatomic) IBOutlet UITextField *addressField; //经度
@property (weak, nonatomic) IBOutlet UILabel *longitudeLabel; //纬度
@property (weak, nonatomic) IBOutlet UILabel *latitudeLabel; //详情
@property (weak, nonatomic) IBOutlet UILabel *detailAddressLabel; //地理编码对象
@property (nonatomic ,strong) CLGeocoder *geocoder; @end
#pragma mark - 地理编码响应
- (void)geocodeBtnClick
{ // 0.获取用户输入的位置
NSString *addressStr = self.addressField.text; if (addressStr == nil || addressStr.length == 0) {
NSLog(@"请输入地址");
return;
} // 1.创建地理编码对象
// 2.利用地理编码对象编码
// 根据传入的地址获取该地址对应的经纬度信息
[self.geocoder geocodeAddressString:addressStr completionHandler:^(NSArray *placemarks, NSError *error) { if (placemarks.count == 0 || error != nil) {
return ;
}
// placemarks地标数组, 地标数组中存放着地标, 每一个地标包含了该位置的经纬度以及城市/区域/国家代码/邮编等等...
// 获取数组中的第一个地标
CLPlacemark *placemark = [placemarks firstObject]; self.latitudeLabel.text = [NSString stringWithFormat:@"%f", placemark.location.coordinate.latitude];
self.longitudeLabel.text = [NSString stringWithFormat:@"%f", placemark.location.coordinate.longitude];
NSArray *address = placemark.addressDictionary[@"FormattedAddressLines"];
NSMutableString *strM = [NSMutableString string];
for (NSString *str in address) {
[strM appendString:str];
}
self.detailAddressLabel.text = strM;
}];
} #pragma mark - 懒加载, 1.创建地理编码对象
- (CLGeocoder *)geocoder
{ if (!_geocoder) {
_geocoder = [[CLGeocoder alloc] init];
}
return _geocoder;
}

效果图如下:


基于CLGeocoder - 地理编码的更多相关文章

  1. iOS地图 -- 地理编码和反地理编码

    地理编码和反地理编码 用到的类和方法 CLGeocoder --> 地理编码管理器 - (void)geocodeAddressString:(NSString *)addressString ...

  2. 基于CLGeocoder - 反地理编码

    iOS中CoreLocatio框架中的CLGeocoder 类不但为我们提供了地理编码方法,而且还提供了反地理编码: 同样需要导入框架: #import <CoreLocation/CoreLo ...

  3. iOS 原生地图地理编码与反地理编码

    当我们要在App实现功能:输入地名,编码为经纬度,实现导航功能. 那么,我需要用到原生地图中的地理编码功能,而在Core Location中主要包含了定位.地理编码(包括反编码)功能. 在文件中导入 ...

  4. iOS之获取经纬度并通过反向地理编码获取详细地址

    _locationManager = [[CLLocationManager alloc] init]; //期望的经度 _locationManager.desiredAccuracy = kCLL ...

  5. iOS - GeoCoder 地理编码

    前言 NS_CLASS_AVAILABLE(10_8, 5_0) @interface CLGeocoder : NSObject 地理编码 地名 -> 经纬度 等具体位置数据信息.根据给定的位 ...

  6. iOS开发拓展篇—CoreLocation地理编码

    iOS开发拓展篇—CoreLocation地理编码 一.简单说明 CLGeocoder:地理编码器,其中Geo是地理的英文单词Geography的简写. 1.使用CLGeocoder可以完成“地理编码 ...

  7. CoreLocation框架的使用---地理编码

    #import "ViewController.h" #import <CoreLocation/CoreLocation.h> @interface ViewCont ...

  8. 【iOS】7.4 定位服务->2.1.3.2 定位 - 官方框架CoreLocation 功能2:地理编码和反地理编码

    本文并非最终版本,如果想要关注更新或更正的内容请关注文集,联系方式详见文末,如有疏忽和遗漏,欢迎指正. 本文相关目录: ================== 所属文集:[iOS]07 设备工具 === ...

  9. python地理处理包——geopy使用之地理编码与反地理编码

    由于专业需要,经常接触一些地理处理的工具包,文档都是英文的,自己看的同时将其翻译一下,一方面自己学习的同时有个记录,要是能同时给一起的学习的童鞋们一些帮助,想想也是极好的.以下的文档内容主要翻译自官方 ...

随机推荐

  1. javascript fundamental concept

    http://hzjavaeyer.group.iteye.com/group/wiki?category_id=283 上面这个url非常适合javascript初学者阅读,感谢原作者的分享 什么是 ...

  2. IOS生成同时支持armv7,armv7s,i386的静态库.a文件

    许多第三方提供的.a文件(一般是那些SDK),嵌入到我们的xcode项目后,生成不会报错. 一部分粗心的SDK提供方,或者我们自己做的.a文件,就会有报错,常见的就是不是armv7结构,或者不是arm ...

  3. 【Leetcode】【Medium】3Sum Closest

    Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...

  4. dailiaojie new

    http://imushan.com/categories/Java/ 编译优化手段.

  5. redis知识树

  6. Windows+linux命令大集合

    net use \\ip\ipc$ " " /user:" " 建立IPC空链接 net use \\ip\ipc$ "密码" /user: ...

  7. 阿里云免费ssl,https证书的申请和校验

    其实写这个之前一直在考虑要不要写出来 ,真的官方文档实在太强大了,连视频都给你录好了,配不好的,是不是可以考虑不用写程序了, 忽然想到第一次使用微信测试号,进行域名认证的时候,因为后台返回“echar ...

  8. UVa 10003 - Cutting Sticks(区间DP)

    链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  9. UVa 1625 - Color Length(线性DP + 滚动数组)

    链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  10. Perl 修改文件内容

    把test.txt文件中的字符aaa替换成bbb perl -pi -e "s/aaa/bbb/gi" test.txt 把test.txt文件中的字符aaa替换成bbb,并生成一 ...