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

摘要:
1、 CLGeocoder:Geocoder,其中Geo是英语单词Geography的缩写。

一、简单说明

CLGeocoder:地理编码器,其中Geo是地理的英文单词Geography的简写。

1.使用CLGeocoder可以完成“地理编码”和“反地理编码”

  地理编码:根据给定的地名,获得具体的位置信息(比如经纬度、地址的全称等)

  反地理编码:根据给定的经纬度,获得具体的位置信息

(1)地理编码方法

  - (void)geocodeAddressString:(NSString *)addressString completionHandler:(CLGeocodeCompletionHandler)completionHandler; 

(2)反地理编码方法

  - (void)reverseGeocodeLocation:(CLLocation *)location completionHandler:(CLGeocodeCompletionHandler)completionHandler;

2.CLGeocodeCompletionHandler

  当地理反地理编码完成时,就会调用CLGeocodeCompletionHandler

  iOS开发拓展篇—CoreLocation地理编码第1张

  这个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中搭建界面如下:

  iOS开发拓展篇—CoreLocation地理编码第2张

实现代码:

  ViewController.m文件

  1 //
  2 //  YYViewController.m
  3 //  地理编码
  4 //
  5 //  Created by XiaoZeng on 14-8-11.
  6 //  Copyright (c) 2014年 github. All rights reserved.
  7 //
  8 
  9 #import "ViewController.h"
 10 #import <CoreLocation/CoreLocation.h>
 11 
 12 @interface ViewController ()
 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)地理编码:(地名->经纬度坐标)

  iOS开发拓展篇—CoreLocation地理编码第3张

打印输出:

  iOS开发拓展篇—CoreLocation地理编码第4张

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

   iOS开发拓展篇—CoreLocation地理编码第5张

(3)注意:调整键盘

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

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

  iOS开发拓展篇—CoreLocation地理编码第7张

(4)注意:搜索的所有结果都是在中国境内的,因为苹果在中国的地图服务商是高德地图。  

免责声明:文章转载自《iOS开发拓展篇—CoreLocation地理编码》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇awk脚本asl 和 lgpl的区别下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

iOS 开发之照片框架详解(1)

http://kayosite.com/ios-development-and-detail-of-photo-framework.html/comment-page-1 一. 概要 在 iOS 设备中,照片和视频是相当重要的一部分。最近刚好在制作一个自定义的 iOS 图片选择器,顺便整理一下 iOS 中对照片框架的使用方法。在 iOS 8 出现之前,开发...

iOS开发之APP导入添加自定义字体

我们平常项目开发用的字体基本都是系统默认的,但有时候设计为了追求完美,会使用自定义字体(当然得公司有钱买了版权哈),下面给大家讲讲怎么集成添加第三方字体。 1、导入三方字体文件进工程 我们就行平常添加文件一样,将字体文件导入xcode工程内,一般字体文件是ttc/ttf/otf 如果测试需要可以去下载方正字体练练手https://ziti8.cc/list...

iOS开发UI篇—Quartz2D简单使用(三)

iOS开发UI篇—Quartz2D简单使用(三) 一、通过slider控制圆的缩放 1.实现过程 新建一个项目,新建一个继承自UIview的类,并和storyboard中自定义的view进行关联。 界面搭建,如图: 代码示例: YYViewController.m文件 1 // 2 // YYViewController.m 3...

iOS开发UI篇—九宫格坐标计算

一、要求 完成下面的布局 二、分析 寻找左边的规律,每一个uiview的x坐标和y坐标。 三、实现思路 (1)明确每一块用得是什么view (2)明确每个view之间的父子关系,每个视图都只有一个父视图,拥有很多的子视图。 (3)可以先尝试逐个的添加格子,最后考虑使用for循环,完成所有uiview的创建 (4)加载app数据,根据数据长度创建对应个数...

iOS开发证书和配置文件的使用

提示:之前只是根据网上的流程完成了iOS项目的发布过程,对于证书和配置文件仍是一头雾水,这里参照别人的文章,简单了解一下,毕竟以后用的着。 这里有几个问题: 什么是iOS证书,它是如何与app应用关联的? iOS开发证书和生产证书有何区别,如何使用的? 证书与配置文件(Provisioning Profiles)是什么关系,配置文件在Xcode中如何使用...

IOS开发---菜鸟学习之路--(十四)-将BASE64图片转换成Image

本文基本全部都是代码 首先是.H文件 1 #import <Foundation/Foundation.h> 2 3 @interface Base64AndImageHelp : NSObject 4 - (NSString*)encodeURL:(NSString *)string; 5 +(id)mydataWithBase64Enc...