标签:
<code class="hljs java has-numbering"></code><h2 id="一效果">一:效果</h2><p>输入地名,可以得到相应的经纬度,知识为了学习写的小Demo <img src="http://img.blog.csdn.net/20150822150215458" alt="这里写图片描述" title="" /></p><h2 id="二实现步骤">二:实现步骤</h2><p>一 :首先获取用户输入的位置。 二 :创建地理编码对象。 三 :利用地理编码对象编码,根据传入的地址获取该地址对应的经纬度信息。 四 :最后将他们分别显示出来就可以了。</p><h2 id="三代码">三:代码</h2> #<span class="hljs-keyword">import</span> <span class="hljs-string">"ViewController.h"</span> #<span class="hljs-keyword">import</span> <CoreLocation/CoreLocation.h> <span class="hljs-annotation">@interface</span> ViewController () #pragma mark - 地理编码 <span class="hljs-javadoc">/** * 监听地理编码点击事件 */</span> - (IBAction)geocodeBtnClick; <span class="hljs-javadoc">/** * 需要编码的地址容器 */</span> <span class="hljs-annotation">@property</span> (weak, nonatomic) IBOutlet UITextField *addressField; <span class="hljs-javadoc">/** * 经度容器 */</span> <span class="hljs-annotation">@property</span> (weak, nonatomic) IBOutlet UILabel *longitudeLabel; <span class="hljs-javadoc">/** * 纬度容器 */</span> <span class="hljs-annotation">@property</span> (weak, nonatomic) IBOutlet UILabel *latitudeLabel; <span class="hljs-javadoc">/** * 详情容器 */</span> <span class="hljs-annotation">@property</span> (weak, nonatomic) IBOutlet UILabel *detailAddressLabel; <span class="hljs-javadoc">/** * 地理编码对象 */</span> <span class="hljs-annotation">@property</span> (nonatomic ,strong) CLGeocoder *geocoder; <span class="hljs-annotation">@end</span> <span class="hljs-annotation">@implementation</span> ViewController - (<span class="hljs-keyword">void</span>)geocodeBtnClick { <span class="hljs-comment">// 0.获取用户输入的位置</span> NSString *addressStr = self.addressField.text; <span class="hljs-keyword">if</span> (addressStr == nil || addressStr.length == <span class="hljs-number">0</span>) { NSLog(@<span class="hljs-string">"请输入地址"</span>); <span class="hljs-keyword">return</span>; } <span class="hljs-comment">// 1.创建地理编码对象</span> <span class="hljs-comment">// 2.利用地理编码对象编码</span> <span class="hljs-comment">// 根据传入的地址获取该地址对应的经纬度信息</span> [self.geocoder geocodeAddressString:addressStr completionHandler:^(NSArray *placemarks, NSError *error) { <span class="hljs-keyword">if</span> (placemarks.count == <span class="hljs-number">0</span> || error != nil) { <span class="hljs-keyword">return</span> ; } <span class="hljs-comment">// placemarks地标数组, 地标数组中存放着地标, 每一个地标包含了该位置的经纬度以及城市/区域/国家代码/邮编等等...</span> <span class="hljs-comment">// 获取数组中的第一个地标</span> CLPlacemark *placemark = [placemarks firstObject]; <span class="hljs-comment">// for (CLPlacemark *placemark in placemarks) {</span> <span class="hljs-comment">// NSLog(@"%@ %@ %f %f", placemark.name, placemark.addressDictionary, placemark.location.coordinate.latitude, placemark.location.coordinate.longitude);</span> self.latitudeLabel.text = [NSString stringWithFormat:@<span class="hljs-string">"%f"</span>, placemark.location.coordinate.latitude]; self.longitudeLabel.text = [NSString stringWithFormat:@<span class="hljs-string">"%f"</span>, placemark.location.coordinate.longitude]; NSArray *address = placemark.addressDictionary[@<span class="hljs-string">"FormattedAddressLines"</span>]; NSMutableString *strM = [NSMutableString string]; <span class="hljs-keyword">for</span> (NSString *str in address) { [strM appendString:str]; } self.detailAddressLabel.text = strM; <span class="hljs-comment">// }</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">void</span>)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { <span class="hljs-comment">//隐藏键盘</span> [self.view endEditing:YES]; } <span class="hljs-annotation">@end</span>
标签:
原文地址:http://blog.csdn.net/shuju345/article/details/47977725