一:效果
输入经纬度,可以得到相应的地名
二:思路
跟地里编码差不多
1.获取用户输入的经纬度
2.根据用户输入的经纬度创建CLLocation对象
3.根据CLLocation对象获取对应的地标信息
三:代码
<code class="hljs objectivec has-numbering"><span class="hljs-preprocessor">#import <span class="hljs-title">"ViewController.h"</span></span> <span class="hljs-preprocessor">#import <span class="hljs-title"><CoreLocation/CoreLocation.h></span></span> <span class="hljs-class"><span class="hljs-keyword">@interface</span> <span class="hljs-title">ViewController</span> ()</span> <span class="hljs-comment">/** * 地理编码对象 */</span> <span class="hljs-keyword">@property</span> (<span class="hljs-keyword">nonatomic</span> ,<span class="hljs-keyword">strong</span>) CLGeocoder *geocoder; <span class="hljs-preprocessor">#pragma mark - 反地理编码</span> - (<span class="hljs-keyword">IBAction</span>)reverseGeocode; <span class="hljs-keyword">@property</span> (<span class="hljs-keyword">weak</span>, <span class="hljs-keyword">nonatomic</span>) <span class="hljs-keyword">IBOutlet</span> UITextField *longtitudeField; <span class="hljs-keyword">@property</span> (<span class="hljs-keyword">weak</span>, <span class="hljs-keyword">nonatomic</span>) <span class="hljs-keyword">IBOutlet</span> UITextField *latitudeField; <span class="hljs-keyword">@property</span> (<span class="hljs-keyword">weak</span>, <span class="hljs-keyword">nonatomic</span>) <span class="hljs-keyword">IBOutlet</span> <span class="hljs-built_in">UILabel</span> *reverseDetailAddressLabel; <span class="hljs-keyword">@end</span> <span class="hljs-class"><span class="hljs-keyword">@implementation</span> <span class="hljs-title">ViewController</span></span> - (<span class="hljs-keyword">void</span>)reverseGeocode { <span class="hljs-comment">// 1.获取用户输入的经纬度</span> <span class="hljs-built_in">NSString</span> *longtitude = <span class="hljs-keyword">self</span><span class="hljs-variable">.longtitudeField</span><span class="hljs-variable">.text</span>; <span class="hljs-built_in">NSString</span> *latitude = <span class="hljs-keyword">self</span><span class="hljs-variable">.latitudeField</span><span class="hljs-variable">.text</span>; <span class="hljs-keyword">if</span> (longtitude<span class="hljs-variable">.length</span> == <span class="hljs-number">0</span> || longtitude == <span class="hljs-literal">nil</span> || latitude<span class="hljs-variable">.length</span> == <span class="hljs-number">0</span> || latitude == <span class="hljs-literal">nil</span>) { <span class="hljs-built_in">NSLog</span>(@<span class="hljs-string">"请输入经纬度"</span>); <span class="hljs-keyword">return</span>; } <span class="hljs-comment">// 2.根据用户输入的经纬度创建CLLocation对象</span> CLLocation *location = [[CLLocation alloc] initWithLatitude:[latitude doubleValue] longitude:[longtitude doubleValue]]; <span class="hljs-comment">// 3.根据CLLocation对象获取对应的地标信息</span> [<span class="hljs-keyword">self</span><span class="hljs-variable">.geocoder</span> reverseGeocodeLocation:location completionHandler:^(<span class="hljs-built_in">NSArray</span> *placemarks, <span class="hljs-built_in">NSError</span> *error) { <span class="hljs-keyword">for</span> (CLPlacemark *placemark in placemarks) { <span class="hljs-built_in">NSLog</span>(@<span class="hljs-string">"%@ %@ %f %f"</span>, placemark<span class="hljs-variable">.name</span>, placemark<span class="hljs-variable">.addressDictionary</span>, placemark<span class="hljs-variable">.location</span><span class="hljs-variable">.coordinate</span><span class="hljs-variable">.latitude</span>, placemark<span class="hljs-variable">.location</span><span class="hljs-variable">.coordinate</span><span class="hljs-variable">.longitude</span>); <span class="hljs-keyword">self</span><span class="hljs-variable">.reverseDetailAddressLabel</span><span class="hljs-variable">.text</span> = placemark<span class="hljs-variable">.locality</span>; } }]; } <span class="hljs-preprocessor">#pragma mark - 懒加载</span> - (CLGeocoder *)geocoder { <span class="hljs-keyword">if</span> (!_geocoder) { _geocoder = [[CLGeocoder alloc] init]; } <span class="hljs-keyword">return</span> _geocoder; } <span class="hljs-keyword">@end</span></code>
四:知识扩充CLGeocoder
使用CLGeocoder可以完成“地理编码”和“反地理编码”
地理编码:根据给定的地名,获得具体的位置信息(比如经纬度、地址的全称等)
反地理编码:根据给定的经纬度,获得具体的位置信息
>地理编码方法
<code class="hljs erlang has-numbering"><span class="hljs-pp">- <span class="hljs-params">(void)</span>geocodeAddressString:<span class="hljs-params">(<span class="hljs-variable">NSString</span> *)</span>addressString completionHandler:<span class="hljs-params">(<span class="hljs-variable">CLGeocodeCompletionHandler</span>)</span>completionHandler;</span></code>
>反地理编码方法
<code class="hljs erlang has-numbering"><span class="hljs-pp">- <span class="hljs-params">(void)</span>reverseGeocodeLocation:<span class="hljs-params">(<span class="hljs-variable">CLLocation</span> *)</span>location completionHandler:<span class="hljs-params">(<span class="hljs-variable">CLGeocodeCompletionHandler</span>)</span>completionHandler;</span></code>
CLGeocodeCompletionHandler
当地理\反地理编码完成时,就会调用
<code class="hljs objectivec has-numbering">CLGeocodeCompletionHandler <span class="hljs-keyword">typedef</span> <span class="hljs-keyword">void</span> (^CLGeocodeCompletionHandler)(<span class="hljs-built_in">NSArray</span> *placemarks, <span class="hljs-built_in">NSError</span> *error);</code>
这个block传递2个参数
error :当编码出错时(比如编码不出具体的信息)有值
placemarks :里面装着CLPlacemark对象
CLPlacemark
CLPlacemark的字面意思是地标,封装详细的地址位置信息
地理位置
<code class="hljs objectivec has-numbering"><span class="hljs-keyword">@property</span> (<span class="hljs-keyword">nonatomic</span>, <span class="hljs-keyword">readonly</span>) CLLocation *location;</code>
区域
<code class="hljs objectivec has-numbering"><span class="hljs-keyword">@property</span> (<span class="hljs-keyword">nonatomic</span>, <span class="hljs-keyword">readonly</span>) CLRegion *region;</code>
详细的地址信息
<code class="hljs objectivec has-numbering"><span class="hljs-keyword">@property</span> (<span class="hljs-keyword">nonatomic</span>, <span class="hljs-keyword">readonly</span>) <span class="hljs-built_in">NSDictionary</span> *addressDictionary;</code>
地址名称
<code class="hljs objectivec has-numbering"><span class="hljs-keyword">@property</span> (<span class="hljs-keyword">nonatomic</span>, <span class="hljs-keyword">readonly</span>) <span class="hljs-built_in">NSString</span> *name;</code>
城市
<code class="hljs objectivec has-numbering"><span class="hljs-keyword">@property</span> (<span class="hljs-keyword">nonatomic</span>, <span class="hljs-keyword">readonly</span>) <span class="hljs-built_in">NSString</span> *locality;</code>
结构图
时间: 2024-10-19 07:48:44