CoreLocation框架的使用---地理编码
#import "ViewController.h"
#import <CoreLocation/CoreLocation.h> @interface ViewController ()
@property (nonatomic, strong) CLGeocoder *geoCoder;
@end @implementation ViewController - (CLGeocoder *)geoCoder
{
if (!_geoCoder) {
// 1.创建解码器对象
_geoCoder = [[CLGeocoder alloc]init]; }
return _geoCoder;
} //地理编码 (地方名-->经纬度坐标)
- (IBAction)geoCoder:(UIButton *)sender {//使用sb连线button,点击方法 NSString *str = @"李店";
//2.地理编码
[self.geoCoder geocodeAddressString:str completionHandler:^(NSArray *placemarks, NSError *error) {
//参数placemarks是一个数组,里面放着地表对象
for (CLPlacemark *pl in placemarks) { //2.获取CLLocation
CLLocation *loc = pl.location;
NSLog(@"地址:%@ 纬度:%f 经度:%f ",pl.name,loc.coordinate.latitude,loc.coordinate.longitude);
} /*1.获取第一个地标对象 里面封装了地理位置信息
CLPlacemark *pl = [placemarks firstObject];
2.获取CLLocation
CLLocation *loc = pl.location;
NSLog(@"%f %f ",loc.coordinate.latitude,loc.coordinate.longitude);*/ }]; } //反地理编码(经纬度坐标-->地方名)
- (IBAction)reverseCoder { //随便创建一个经纬度坐标.
CLLocation *loc = [[CLLocation alloc]initWithLatitude: longitude:]; [self.geoCoder reverseGeocodeLocation:loc completionHandler:^(NSArray *placemarks, NSError *error) {
//1.获取地标对象 里面封装了地理位置信息
CLPlacemark *pl = [placemarks firstObject];
NSLog(@"地方名:%@ ",pl.name);
}];
}
以下内容转自:iOS开发拓展篇—CoreLocation地理编码 - 文顶顶 - 博客园
一、简单说明
CLGeocoder:地理编码器,其中Geo是地理的英文单词Geography的简写。
1.使用CLGeocoder可以完成“地理编码”和“反地理编码”
地理编码:根据给定的地名,获得具体的位置信息(比如经纬度、地址的全称等)
反地理编码:根据给定的经纬度,获得具体的位置信息
(1)地理编码方法
- (void)geocodeAddressString:(NSString *)addressString completionHandler:(CLGeocodeCompletionHandler)completionHandler;
(2)反地理编码方法
- (void)reverseGeocodeLocation:(CLLocation *)location completionHandler:(CLGeocodeCompletionHandler)completionHandler;
2.CLGeocodeCompletionHandler
当地理\反地理编码完成时,就会调用CLGeocodeCompletionHandler

这个block传递2个参数
error :当编码出错时(比如编码不出具体的信息)有值
placemarks :里面装着CLPlacemark对象
3.CLPlacemark
说明:CLPlacemark的字面意思是地标,封装详细的地址位置信息
地理位置 @property (nonatomic, readonly) CLLocation *location;
区域 @property (nonatomic, readonly) CLRegion *region;
详细的地址信息 @property (nonatomic, readonly) NSDictionary *addressDictionary;
地址名称 @property (nonatomic, readonly) NSString *name;
城市 @property (nonatomic, readonly) NSString *locality;
二、代码示例:
在storyboard中搭建界面如下:

实现代码:
YYViewController.m文件

1 //
2 // YYViewController.m
3 // 19-地理编码
4 //
5 // Created by apple on 14-8-11.
6 // Copyright (c) 2014年 yangyong. All rights reserved.
7 //
8
9 #import "YYViewController.h"
10 #import <CoreLocation/CoreLocation.h>
11
12 @interface YYViewController ()
13 @property(nonatomic,strong)CLGeocoder *geocoder;
14 #pragma mark-地理编码
15 - (IBAction)geocode;
16 @property (weak, nonatomic) IBOutlet UITextField *addressField;
17 @property (weak, nonatomic) IBOutlet UILabel *longitudeLabel;
18 @property (weak, nonatomic) IBOutlet UILabel *latitudeLabel;
19 @property (weak, nonatomic) IBOutlet UILabel *detailAddressLabel;
20
21 #pragma mark-反地理编码
22
23 - (IBAction)reverseGeocode;
24 @property (weak, nonatomic) IBOutlet UITextField *longitudeField;
25 @property (weak, nonatomic) IBOutlet UITextField *latitudeField;
26 @property (weak, nonatomic) IBOutlet UILabel *reverdeDetailAddressLabel;
27 @end
28
29 @implementation YYViewController
30
31 #pragma mark-懒加载
32 -(CLGeocoder *)geocoder
33 {
34 if (_geocoder==nil) {
35 _geocoder=[[CLGeocoder alloc]init];
36 }
37 return _geocoder;
38 }
39 - (void)viewDidLoad
40 {
41 [super viewDidLoad];
42 }
43 /**
44 * 地理编码:地名—>经纬度坐标
45 */
46 - (IBAction)geocode {
47 //1.获得输入的地址
48 NSString *address=self.addressField.text;
49 if (address.length==0) return;
50
51 //2.开始地理编码
52 //说明:调用下面的方法开始编码,不管编码是成功还是失败都会调用block中的方法
53 [self.geocoder geocodeAddressString:address completionHandler:^(NSArray *placemarks, NSError *error) {
54 //如果有错误信息,或者是数组中获取的地名元素数量为0,那么说明没有找到
55 if (error || placemarks.count==0) {
56 self.detailAddressLabel.text=@"你输入的地址没找到,可能在月球上";
57 }else // 编码成功,找到了具体的位置信息
58 {
59 //打印查看找到的所有的位置信息
60 /*
61 name:名称
62 locality:城市
63 country:国家
64 postalCode:邮政编码
65 */
66 for (CLPlacemark *placemark in placemarks) {
67 NSLog(@"name=%@ locality=%@ country=%@ postalCode=%@",placemark.name,placemark.locality,placemark.country,placemark.postalCode);
68 }
69
70 //取出获取的地理信息数组中的第一个显示在界面上
71 CLPlacemark *firstPlacemark=[placemarks firstObject];
72 //详细地址名称
73 self.detailAddressLabel.text=firstPlacemark.name;
74 //纬度
75 CLLocationDegrees latitude=firstPlacemark.location.coordinate.latitude;
76 //经度
77 CLLocationDegrees longitude=firstPlacemark.location.coordinate.longitude;
78 self.latitudeLabel.text=[NSString stringWithFormat:@"%.2f",latitude];
79 self.longitudeLabel.text=[NSString stringWithFormat:@"%.2f",longitude];
80 }
81 }];
82 }
83
84 /**
85 * 反地理编码:经纬度坐标—>地名
86 */
87 - (IBAction)reverseGeocode {
88 //1.获得输入的经纬度
89 NSString *longtitudeText=self.longitudeField.text;
90 NSString *latitudeText=self.latitudeField.text;
91 if (longtitudeText.length==0||latitudeText.length==0) return;
92
93 CLLocationDegrees latitude=[latitudeText doubleValue];
94 CLLocationDegrees longitude=[longtitudeText doubleValue];
95
96 CLLocation *location=[[CLLocation alloc]initWithLatitude:latitude longitude:longitude];
97 //2.反地理编码
98 [self.geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {
99 if (error||placemarks.count==0) {
100 self.reverdeDetailAddressLabel.text=@"你输入的地址没找到,可能在月球上";
101 }else//编码成功
102 {
103 //显示最前面的地标信息
104 CLPlacemark *firstPlacemark=[placemarks firstObject];
105 self.reverdeDetailAddressLabel.text=firstPlacemark.name;
106 //经纬度
107 CLLocationDegrees latitude=firstPlacemark.location.coordinate.latitude;
108 CLLocationDegrees longitude=firstPlacemark.location.coordinate.longitude;
109 self.latitudeField.text=[NSString stringWithFormat:@"%.2f",latitude];
110 self.longitudeField.text=[NSString stringWithFormat:@"%.2f",longitude];
111 }
112 }];
113 }
114
115 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
116 {
117 [self.view endEditing:YES];
118 }
119 @end

实现效果:
(1)地理编码:(地名->经纬度坐标)

打印输出:

(2)反地理编码:(经纬度—>地名)

(3)注意:调整键盘

点击经纬度textField进行输入的时候,弹出的键盘如下

(4)注意:搜索的所有结果都是在中国境内的,因为苹果在中国的地图服务商是高德地图。
CoreLocation框架的使用---地理编码的更多相关文章
- 猫猫学iOS 之CoreLocation反地理编码小Demo输入经纬度得到城市
猫猫分享,必须精品 原创文章,欢迎转载.转载请注明:翟乃玉的博客 地址:http://blog.csdn.net/u013357243 一:效果 输入经纬度,能够得到相应的地名 二:思路 跟地里编码差 ...
- AJ学IOS 之CoreLocation反地理编码小Demo输入经纬度得到城市
AJ分享,必须精品 一:效果 输入经纬度,可以得到相应的地名 二:思路 跟地里编码差不多 1.获取用户输入的经纬度 2.根据用户输入的经纬度创建CLLocation对象 3.根据CLLocation对 ...
- iOS地图 -- 地理编码和反地理编码
地理编码和反地理编码 用到的类和方法 CLGeocoder --> 地理编码管理器 - (void)geocodeAddressString:(NSString *)addressString ...
- 【iOS】7.4 定位服务->2.1.3.2 定位 - 官方框架CoreLocation 功能2:地理编码和反地理编码
本文并非最终版本,如果想要关注更新或更正的内容请关注文集,联系方式详见文末,如有疏忽和遗漏,欢迎指正. 本文相关目录: ================== 所属文集:[iOS]07 设备工具 === ...
- IOS CoreLocation框架的使用(用于地理定位)
● 在移动互联网时代,移动app能解决用户的很多生活琐事,比如 ● 导航:去任意陌生的地方 ● 周边:找餐馆.找酒店.找银行.找电影院 ● 在上述应用中,都用到了地图和定位功能,在iOS开发中 ...
- iOS地理反地理编码--CoreLocation
.sidebar{float:left;width:220px;} .container-fluid>.content{margin-left:240px;} a{color:#0069d6;t ...
- iOS开发拓展篇—CoreLocation地理编码
iOS开发拓展篇—CoreLocation地理编码 一.简单说明 CLGeocoder:地理编码器,其中Geo是地理的英文单词Geography的简写. 1.使用CLGeocoder可以完成“地理编码 ...
- 地图定位CoreLocation框架,地理位置编码与反编码
在现代互联网时代,越来越多的应用,都用到了地图定位功能,在iOS开发中,想要加入这种功能,必须基于两个框架进行开发: 1.Map Kit:用于显示地图, 2.CoreLocation:用于显示地理位置 ...
- AJ学IOS 之CoreLocation地理编码小Demo输入城市得到经纬度
AJ分享,必须精品 一:效果 输入地名,可以得到相应的经纬度,知识为了学习写的小Demo 二:实现步骤 一 :首先获取用户输入的位置. 二 :创建地理编码对象. 三 :利用地理编码对象编码,根据传入的 ...
随机推荐
- 什么是BOM头,及PHP解决办法
类似WINDOWS自带的记事本等软件,在保存一个以UTF-8编码的文件时,会在文件开始的地方插入三个不可见的字符(0xEF 0xBB 0xBF,即BOM).它是一串隐藏的字符,用于让记事本等编辑器识别 ...
- 转:Android Webview 加载外部html时选择加载本地的js,css等资源文件
原文地址:http://m.blog.csdn.net/blog/qduningning/43196819 在使用WebView加载网页的时候,有一些固定的资源文件如js的jquery包,css,图片 ...
- XE6移动开发环境搭建之IOS篇(7):在Mac OSX 10.8中安装Xcode4.6.3(有图有真相)
网上能找到的关于Delphi XE系列的移动开发环境的相关文章甚少,本文尽量以详细的图文内容.傻瓜式的表达来告诉你想要的答案. 原创作品,请尊重作者劳动成果,转载请注明出处!!! 在安装Xcode前, ...
- 黑马程序员_JAVA之交通灯管理系统
------Java培训.Android培训.iOS培训..Net培训.期待与您交流! ------- 1.一.需求:模拟实现十字路口的交通灯管理系统逻辑,具体需求如下: 1.异步随机生成按照各个路 ...
- 黑马程序员_JAVA基础知识总结3
------- android培训.java培训.期待与您交流! ---------- Java源文件的扩展名是.java,编译之后生成.class的文件.所有的类都有一个共同的继承祖先Object类 ...
- 59. Spiral Matrix && Spiral Matrix II
Spiral Matrix Given a matrix of m x n elements (m rows, n columns), return all elements of the matri ...
- CentOS的安装与克隆
CentOS的安装与克隆 环境说明 win7 x64位:VMware12:CentOS-6.5-x86_64-minimal 安装与初始配置 安装 在WM主页--“创建新的虚拟机”--自定义--... ...
- express 手动删除session状态(即登出功能)
在退出按钮被点击后,发送请求/logout,服务端做如下处理: app.get('/logout', function() { delete req.session.user; return res. ...
- HTTP的GET/POST细节
HTTP的GET/POST方式有何区别?这是一个老生常谈的问题,但老生常谈的问题往往有一些让人误解的结论.本文将带您浅尝HTTP协议,在了 解HTTP协议的同时将会展示许多被人们忽视的内容.在掌握了H ...
- 基于PHP生成静态页的实现方法
t1.php 复制代码 代码如下: <?php// 方法一根据模版生成静态页面// replaceTemplateString函数用于替换模板中指定字符串function replaceTemp ...