地图和定位 、 iCloud
1 FindMe应用
1.1 问题
MapKit框架可以用于创建现场交互的地图来显示用户想要设备显示的任何位置,包括用户的当前位置,甚至可以进行标记并查看地图上的标注信息。CoreLocation框架主要用于确定物理位置,通过三种技术来实现:GPS、蜂窝基站定位和WPS,GPS是这三种中最精确。本案例使用MapKit和CoreLocation框架完成一款地图应用,显示自身的位置并添加视图标注,如图-1所示:

图-1
1.2 方案
首先创建一个SingleViewApplication应用,需要将CoreLocation和MapKit框架添加到项目中。
然后在Storyboard文件中搭建界面,从对象库中拖拽一个MapView控件到场景中,调整地图视图的尺寸使宽度占满整个大视图,通过连线的方式将MapView控件的delegate设置为ViewController。
场景的下方再拖放一个Label控件和Button控件。分别将场景中的MapView控件、Label控件以及Button控件关联成ViewController的输出口属性mapView、progressLabel和button,另外再将Button控件关联成ViewController的动作方法findMe:,当点击按钮时该方法被调用开始更新定位。
接下来创建一个类Annotation用来保存标注对象,该类有一个必须的属性CLLocationCoordinate2D类型的coordinate,用来记录标注的位置,然后通过重写属性title和subTitle的setter方法设置标题和子标题的内容。
完成以上步骤后在ViewController类扩展中定义一个CLLocationManager类型的locationManager属性,并且ViewController需要遵守MKMapViewDelegate和CLLocationManagerDelegate协议。在viewDidLoad方法中将self.mapView的showsUserLocation属性设置为YES,允许显示当前用户的位置。
然后实现findMe:方法,该方法中设置self.locationManager的相关属性并将delegate设置为ViewController,通过调用startUpdatingLocation方法开始更新地图数据,然后更新label的显示内容。
接下来实现locationManager:didUpdateLocations:协议方法,该方法在每次更新位置时调用,该方法中通过locations参数获取到当前位置,并通过当前位置创建一个显示区域,添加标注更新label的显示内容。
最后实现mapView:viewForAnnotation:协议方法,当mapView需要一个标注视图时将调用该方法,在该方法中创建一个标注视图,并添加到地图上。
1.3 步骤
实现此案例需要按照如下步骤进行。
步骤一:搭建界面
首先创建一个SingleViewApplication应用,需要将CoreLocation和MapKit框架添加到项目中。
然后在Storyboard文件中搭建界面,从对象库中拖拽一个MapView控件到场景中,调整地图视图的尺寸使宽度占满整个大视图,通过连线的方式将MapView控件的delegate设置为ViewController,如图-2所示:

图-2
场景的下方再拖放一个Label控件和Button控件。分别将场景中的MapView控件、Label控件以及Button控件关联成ViewController的输出口属性mapView、progressLabel和button,另外再将Button控件关联成ViewController的动作方法findMe:,当点击按钮时该方法被调用开始更新定位,代码如下所示:
- @interface ViewController ()
- @property (weak, nonatomic) IBOutlet MKMapView *mapView;
- @property (weak, nonatomic) IBOutlet UILabel *progressLabel;
- @property (weak, nonatomic) IBOutlet UIButton *button;
- @end
在Storyboard中完成的界面如图-3所示:

图-3
步骤二:创建标注类Annotation
创建一个类Annotation用来保存标注对象,该类继承至NSObjet,它有一个必须的属性CLLocationCoordinate2D类型的coordinate,用来记录标注的位置,然后通过重写属性title和subTitle的setter方法设置标题和子标题的内容,代码如下所示:
- //Annotation.h文件
- @interface Annotation : NSObject<MKAnnotation>
- @property (nonatomic,assign) CLLocationCoordinate2D coordinate;
- @end
- //Annotation.m文件
- @implementation Annotation
- -(NSString *)title {
- NSString *string = @"You are here";
- return string;
- }
- -(NSString *)subtitle {
- NSString *string = @"您当前位置";
- return string;
- }
- @end
步骤三:实现地图定位和添加标注视图
在ViewController类扩展中定义一个CLLocationManager类型的locationManager属性,并且ViewController需要遵守MKMapViewDelegate和CLLocationManagerDelegate协议,代码如下所示:
- @interface ViewController ()<MKMapViewDelegate,CLLocationManagerDelegate,UIAlertViewDelegate>
- @property (weak, nonatomic) IBOutlet MKMapView *mapView;
- @property (weak, nonatomic) IBOutlet UILabel *progressLabel;
- @property (weak, nonatomic) IBOutlet UIButton *button;
- @property (nonatomic,strong) CLLocationManager *locationManager;
- @end
- //初始化属性locationManager
- -(CLLocationManager *)locationManager {
- if (!_locationManager) {
- _locationManager = [[CLLocationManager alloc]init];
- }
- return _locationManager;
- }
在viewDidLoad方法中设置self.mapView的类型,并将self.mapView的showsUserLocation属性设置为YES,允许显示当前用户的位置,代码如下所示:
- - (void)viewDidLoad {
- [super viewDidLoad];
- self.mapView.mapType = MKMapTypeStandard;
- self.mapView.showsUserLocation = YES;
- }
然后实现findMe:方法,该方法中设置self.locationManager的相关属性并将delegate设置为ViewController,通过调用startUpdatingLocation方法开始更新地图数据,然后更新label的显示内容,代码如下所示:
- - (IBAction)findMe:(UIButton *)sender {
- //设置委托对象
- self.locationManager.delegate = self;
- //设置最佳精确度
- self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
- //开始更新
- [self.locationManager requestAlwaysAuthorization];
- [self.locationManager requestWhenInUseAuthorization];
- [self.locationManager startUpdatingLocation];
- //设置标签显示内容
- self.progressLabel.text = @"Delermining Current Location";
- sender.hidden = YES;
- }
接下来实现locationManager:didUpdateLocations:协议方法,该方法在每次更新位置时调用,该方法中通过locations参数获取到当前位置,并通过当前位置创建一个显示区域,添加标注更新label的显示内容,代码如下所示:
- -(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
- //获取当前位置
- CLLocation *newLocation = locations[0];
- NSLog(@"%@",newLocation);
- //设置显示区域
- MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(newLocation.coordinate, 2000, 2000);
- //调整区域以适合地图视图显示的宽高比
- MKCoordinateRegion adjustedRegion = [self.mapView regionThatFits:viewRegion];
- //将调整后的区域显示在地图视图上面
- [self.mapView setRegion:adjustedRegion animated:YES];
- manager.delegate = self;
- //由于更新位置是一个比较耗电量的操作,在不必要更新时可以关闭locationManager
- [manager stopUpdatingLocation];
- //更新标签显示
- self.progressLabel.text = @"Recerse Geocoding Location";
- //添加标注
- Annotation *annotation = [[Annotation alloc]init];
- annotation.coordinate = newLocation.coordinate;
- [self.mapView addAnnotation:annotation];
- self.progressLabel.text = @"Location Determined";
- }
最后实现mapView:viewForAnnotation:协议方法,当mapView需要一个标注视图时将调用该方法,在该方法中创建一个标注视图,并添加到地图上,代码如下所示:
- -(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
- static NSString *placemarkIdentifier = @"Map Location Identifier";
- if ([annotation isKindOfClass:[Annotation class]]) {
- MKPinAnnotationView *annotationView = (MKPinAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:placemarkIdentifier];
- if (!annotationView) {
- annotationView = [[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:placemarkIdentifier];
- }else {
- annotationView.annotation = annotation;
- }
- annotationView.enabled = YES;
- annotationView.animatesDrop = YES;
- annotationView.pinColor = MKPinAnnotationColorPurple;
- annotationView.canShowCallout = YES;
- //为了能够看到显示过程延迟5秒将标注视图添加到界面上
- [self performSelector:@selector(openCallout:) withObject:annotation afterDelay:5];
- self.progressLabel.text = @"Creating Annotation";
- return annotationView;
- }
- return nil;
- }
- //选中标注
- -(void)openCallout:(id<MKAnnotation>)annotation {
- self.progressLabel.text = @"Show Annotation";
- [self.mapView selectAnnotation:annotation animated:YES];
- }
当遇到错误和警告时同样可以通过实现MKMapViewDelegate协议和CLLocationManagerDelegate协议中的方法来提醒用户,代码如下所示:
- -(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
- NSString *errorType = (error.code == kCLErrorDenied)?@"Access Denied":@"Unknow Error";
- UIAlertView *av = [[UIAlertView alloc]initWithTitle:@"Error getting Location" message:errorType delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
- [av show];
- }
- -(void)mapView:(MKMapView *)mapView didFailToLocateUserWithError:(NSError *)error {
- UIAlertView *av = [[UIAlertView alloc]initWithTitle:@"Error loading map" message:[error description] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
- [av show];
- NSLog(@"%@",[error description]);
- }
- //UIAlertViewDelegate
- -(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
- self.progressLabel.text = @"";
- self.button.hidden = NO;
- }
完成效果如图-4、图-5、图-6所示:

图-4

图-5

图-6
1.4 完整代码
本案例中,ViewController.m文件中的完整代码如下所示:
- #import "ViewController.h"
- #import "Annotation.h"
- @interface ViewController ()<MKMapViewDelegate,CLLocationManagerDelegate,UIAlertViewDelegate>
- @property (weak, nonatomic) IBOutlet MKMapView *mapView;
- @property (weak, nonatomic) IBOutlet UILabel *progressLabel;
- @property (weak, nonatomic) IBOutlet UIButton *button;
- @property (nonatomic,strong) CLLocationManager *locationManager;
- @end
- @implementation ViewController
- - (void)viewDidLoad {
- [super viewDidLoad];
- self.mapView.mapType = MKMapTypeStandard;
- self.mapView.showsUserLocation = YES;
- }
- -(CLLocationManager *)locationManager {
- if (!_locationManager) {
- _locationManager = [[CLLocationManager alloc]init];
- }
- return _locationManager;
- }
- - (IBAction)findMe:(UIButton *)sender {
- //设置委托对象
- self.locationManager.delegate = self;
- //设置最佳精确度
- self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
- //开始更新
- [self.locationManager requestAlwaysAuthorization];
- [self.locationManager requestWhenInUseAuthorization];
- [self.locationManager startUpdatingLocation];
- //设置标签显示内容
- self.progressLabel.text = @"Delermining Current Location";
- sender.hidden = YES;
- }
- //CLLocationManagerDelegate协议方法
- -(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
- //获取当前位置
- CLLocation *newLocation = locations[0];
- NSLog(@"%@",newLocation);
- //设置显示区域
- MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(newLocation.coordinate, 2000, 2000);
- //调整区域以适合地图视图显示的宽高比
- MKCoordinateRegion adjustedRegion = [self.mapView regionThatFits:viewRegion];
- //将调整后的区域显示在地图视图上面
- [self.mapView setRegion:adjustedRegion animated:YES];
- manager.delegate = self;
- //由于更新位置是一个比较耗电量的操作,在不必要更新时可以关闭locationManager
- [manager stopUpdatingLocation];
- //更新标签显示
- self.progressLabel.text = @"Recerse Geocoding Location";
- //添加标注
- Annotation *annotation = [[Annotation alloc]init];
- annotation.coordinate = newLocation.coordinate;
- [self.mapView addAnnotation:annotation];
- self.progressLabel.text = @"Location Determined";
- }
- -(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
- NSString *errorType = (error.code == kCLErrorDenied)?@"Access Denied":@"Unknow Error";
- UIAlertView *av = [[UIAlertView alloc]initWithTitle:@"Error getting Location" message:errorType delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
- [av show];
- }
- //MapViewDelegate方法
- //当mapView需要一个标注视图时将调用该方法,在该方法中创建一个标注视图
- -(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
- static NSString *placemarkIdentifier = @"Map Location Identifier";
- if ([annotation isKindOfClass:[Annotation class]]) {
- MKPinAnnotationView *annotationView = (MKPinAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:placemarkIdentifier];
- if (!annotationView) {
- annotationView = [[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:placemarkIdentifier];
- }else {
- annotationView.annotation = annotation;
- }
- annotationView.enabled = YES;
- annotationView.animatesDrop = YES;
- annotationView.pinColor = MKPinAnnotationColorPurple;
- annotationView.canShowCallout = YES;
- //为了能够看到显示过程延迟5秒将标注视图添加到界面上
- [self performSelector:@selector(openCallout:) withObject:annotation afterDelay:5];
- self.progressLabel.text = @"Creating Annotation";
- return annotationView;
- }
- return nil;
- }
- //选中标注
- -(void)openCallout:(id<MKAnnotation>)annotation {
- self.progressLabel.text = @"Show Annotation";
- [self.mapView selectAnnotation:annotation animated:YES];
- }
- -(void)mapView:(MKMapView *)mapView didFailToLocateUserWithError:(NSError *)error {
- UIAlertView *av = [[UIAlertView alloc]initWithTitle:@"Error loading map" message:[error description] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
- [av show];
- NSLog(@"%@",[error description]);
- }
- //UIAlertViewDelegate
- -(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
- self.progressLabel.text = @"";
- self.button.hidden = NO;
- }
- @end
本案例中,Annotation.h文件中的完整代码如下所示:
- #import <Foundation/Foundation.h>
- #import <MapKit/MapKit.h>
- @interface Annotation : NSObject<MKAnnotation>
- @property (nonatomic,assign) CLLocationCoordinate2D coordinate;
- @end
本案例中,Annotation.m文件中的完整代码如下所示:
- #import "Annotation.h"
- @implementation Annotation
- -(NSString *)title {
- NSString *string = @"You are here";
- return string;
- }
- -(NSString *)subtitle {
- NSString *string = @"您当前位置";
- return string;
- }
- @end
地图和定位 、 iCloud的更多相关文章
- iOS开发系列--地图与定位
概览 现在很多社交.电商.团购应用都引入了地图和定位功能,似乎地图功能不再是地图应用和导航应用所特有的.的确,有了地图和定位功能确实让我们的生活更加丰富多彩,极大的改变了我们的生活方式.例如你到了一个 ...
- iOS开发之地图与定位
无论是QQ还是微信的移动客户端都少不了定位功能,之前在微信demo中没有添加定位功能,今天就写个定位的小demo来了解一下定位和地图的东西.地图和定位看上去是挺高大上一东西,其实用法比TableVie ...
- [OC][地图] 高德地图之定位初探(一)
使用前的说明 高德地图开放平台的iOS定位模块网址-->http://lbs.amap.com/api/ios-location-sdk/summary/ 高德地图有Web端.android平台 ...
- iOS进阶_地图上定位的标志——大头针
一.添加大头针 地图使用的框架是MapKit 大头针走的是MKAnnotation协议 /* 注意:因为是满足协议MKAnnotation,所以没有MKAnnotation的系统大头针类,必须自定义大 ...
- 地图、定位 CLLocationManager CLGeocoder CLPlacemark
地图.定位 一.基本知识点 定位: 1.info.plist文件设置 ios8以后,使用定位需要在info.plist文件中添加两个字段NSLocationAlwaysUsageDescription ...
- Android 百度地图API 定位 导航
看看这个利用百度地图定位并实现目的地导航的Demo. 首先看实现效果: 进 入后首先会得到当前位置,在地图上显示出来.在输入框中输入目的地后,就会在地 ...
- ios开发——实用技术OC篇&地图与定位
地图与定位 11.1 iOS定位服务 11.2 iOS地图 11.3 Web地图 1 iOS定位服务 iOS中有三个定位服务组件: Wifi定位,通过查询一个Wifi路由器的地理位置的信息.比较省电, ...
- 在Fragment中实现百度地图,定位到当前位置(基于SDKv2.1.0)
使用最新版本的百度地图需要注意的几个地方: 1.libs文件夹下要有android-support-v4.jar.baidumapapi_v2_1_0.jar.locSDK_3.1.jar三个jar包 ...
- iOS中的地图和定位
文章摘自http://www.cnblogs.com/kenshincui/p/4125570.html#location 如有侵权,请联系删除. 概览 现在很多社交.电商.团购应用都引入了地图和定 ...
- AngularJS进阶(十九)在AngularJS应用中集成百度地图实现定位功能
在AngularJS应用中集成百度地图实现定位功能 注:请点击此处进行充电! 前言 根据项目需求,需要实现手机定位功能,考虑到百度业务的强大能力,遂决定使用百度地图第三方服务. 添加第三方模块的步骤与 ...
随机推荐
- Android study first ----------安卓项目目录结构及adb指令
#Android项目的目录结构 * Activity:应用被打开时显示的界面 * src:项目代码 * R.java:项目中所有资源文件的资源id * Android.jar:Android的jar包 ...
- Spring并发访问的线程安全性问题
Spring并发访问的线程安全性问题 http://windows9834.blog.163.com/blog/static/27345004201391045539953/ 由于Spring MVC ...
- centos6.6安装配置jboss7.1.1
Centos6.6下安装配置Jboss7.1.1 在了解jboss牛逼特性之后,我决定安装下jboss7.1.1试试 下面是安装包百度云盘链接: http://pan.baidu.com/s/1o6O ...
- Python3基础 not in列表名 判断一个元素是否不在列表中列表中
镇场诗:---大梦谁觉,水月中建博客.百千磨难,才知世事无常.---今持佛语,技术无量愿学.愿尽所学,铸一良心博客.------------------------------------------ ...
- URL的格式
URL RFC: http://www.ietf.org/rfc/rfc1738.txt URI RFC: http://www.ietf.org/rfc/rfc2396.txt 转自: http ...
- html/CSS基础知识回顾
html部分 块级元素: 一般用来搭建网站架构,布局,装载内容...像这些大体力的活都属于块级元素.它包括以下标签: address,blockquote,center,dir, div, dl, d ...
- 使用 Jasmine 进行测试驱动的 JavaScript 开发
Jasmine 为 JavaScript 提供了 TDD (测试驱动开发)的框架,对于前端软件开发提供了良好的质量保证,这里对 Jasmine 的配置和使用做一个说明. 目前,Jasmine 的最新版 ...
- nginx 负载均衡策略
nginx 负载均衡策略 1. 轮询轮询方式是nginx负载均衡的默认策略,根据每个server的权重值来轮流发送请求,例如:upstream backend {server backend1.e ...
- JS历史
JavaScript伴随着互联网的发展一起发展.互联网周边技术的快速发展,刺激和推动了JavaScript语言的发展. 2006年,jQuery函数库诞生,作者为John Resig.jQuery为操 ...
- BitMap存储jpg到手机sd卡中
/** * 将BitMap转成Jpeg图片保存到sdcard(便于以后debug调试查看和裁切调试) */ private void saveBitmap(Bitmap bitmap, String ...