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:,当点击按钮时该方法被调用开始更新定位,代码如下所示:

  1. @interface ViewController ()
  2. @property (weak, nonatomic) IBOutlet MKMapView *mapView;
  3. @property (weak, nonatomic) IBOutlet UILabel *progressLabel;
  4. @property (weak, nonatomic) IBOutlet UIButton *button;
  5. @end

在Storyboard中完成的界面如图-3所示:

图-3

步骤二:创建标注类Annotation

创建一个类Annotation用来保存标注对象,该类继承至NSObjet,它有一个必须的属性CLLocationCoordinate2D类型的coordinate,用来记录标注的位置,然后通过重写属性title和subTitle的setter方法设置标题和子标题的内容,代码如下所示:

 
  1. //Annotation.h文件
  2. @interface Annotation : NSObject<MKAnnotation>
  3. @property (nonatomic,assign) CLLocationCoordinate2D coordinate;
  4. @end
  5. //Annotation.m文件
  6. @implementation Annotation
  7. -(NSString *)title {
  8. NSString *string = @"You are here";
  9. return string;
  10. }
  11. -(NSString *)subtitle {
  12. NSString *string = @"您当前位置";
  13. return string;
  14. }
  15. @end

步骤三:实现地图定位和添加标注视图

在ViewController类扩展中定义一个CLLocationManager类型的locationManager属性,并且ViewController需要遵守MKMapViewDelegate和CLLocationManagerDelegate协议,代码如下所示:

  1. @interface ViewController ()<MKMapViewDelegate,CLLocationManagerDelegate,UIAlertViewDelegate>
  2. @property (weak, nonatomic) IBOutlet MKMapView *mapView;
  3. @property (weak, nonatomic) IBOutlet UILabel *progressLabel;
  4. @property (weak, nonatomic) IBOutlet UIButton *button;
  5. @property (nonatomic,strong) CLLocationManager *locationManager;
  6. @end
  7. //初始化属性locationManager
  8. -(CLLocationManager *)locationManager {
  9. if (!_locationManager) {
  10. _locationManager = [[CLLocationManager alloc]init];
  11. }
  12. return _locationManager;
  13. }

在viewDidLoad方法中设置self.mapView的类型,并将self.mapView的showsUserLocation属性设置为YES,允许显示当前用户的位置,代码如下所示:

  1. - (void)viewDidLoad {
  2. [super viewDidLoad];
  3. self.mapView.mapType = MKMapTypeStandard;
  4. self.mapView.showsUserLocation = YES;
  5. }

然后实现findMe:方法,该方法中设置self.locationManager的相关属性并将delegate设置为ViewController,通过调用startUpdatingLocation方法开始更新地图数据,然后更新label的显示内容,代码如下所示:

  1. - (IBAction)findMe:(UIButton *)sender {
  2. //设置委托对象
  3. self.locationManager.delegate = self;
  4. //设置最佳精确度
  5. self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
  6. //开始更新
  7. [self.locationManager requestAlwaysAuthorization];
  8. [self.locationManager requestWhenInUseAuthorization];
  9. [self.locationManager startUpdatingLocation];
  10.      //设置标签显示内容
  11. self.progressLabel.text = @"Delermining Current Location";
  12. sender.hidden = YES;
  13. }

接下来实现locationManager:didUpdateLocations:协议方法,该方法在每次更新位置时调用,该方法中通过locations参数获取到当前位置,并通过当前位置创建一个显示区域,添加标注更新label的显示内容,代码如下所示:

  1. -(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
  2. //获取当前位置
  3. CLLocation *newLocation = locations[0];
  4. NSLog(@"%@",newLocation);
  5. //设置显示区域
  6. MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(newLocation.coordinate, 2000, 2000);
  7. //调整区域以适合地图视图显示的宽高比
  8. MKCoordinateRegion adjustedRegion = [self.mapView regionThatFits:viewRegion];
  9. //将调整后的区域显示在地图视图上面
  10. [self.mapView setRegion:adjustedRegion animated:YES];
  11. manager.delegate = self;
  12. //由于更新位置是一个比较耗电量的操作,在不必要更新时可以关闭locationManager
  13. [manager stopUpdatingLocation];
  14. //更新标签显示
  15. self.progressLabel.text = @"Recerse Geocoding Location";
  16. //添加标注
  17. Annotation *annotation = [[Annotation alloc]init];
  18. annotation.coordinate = newLocation.coordinate;
  19. [self.mapView addAnnotation:annotation];
  20. self.progressLabel.text = @"Location Determined";
  21. }

最后实现mapView:viewForAnnotation:协议方法,当mapView需要一个标注视图时将调用该方法,在该方法中创建一个标注视图,并添加到地图上,代码如下所示:

  1. -(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
  2. static NSString *placemarkIdentifier = @"Map Location Identifier";
  3. if ([annotation isKindOfClass:[Annotation class]]) {
  4. MKPinAnnotationView *annotationView = (MKPinAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:placemarkIdentifier];
  5. if (!annotationView) {
  6. annotationView = [[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:placemarkIdentifier];
  7. }else {
  8. annotationView.annotation = annotation;
  9. }
  10. annotationView.enabled = YES;
  11. annotationView.animatesDrop = YES;
  12. annotationView.pinColor = MKPinAnnotationColorPurple;
  13. annotationView.canShowCallout = YES;
  14. //为了能够看到显示过程延迟5秒将标注视图添加到界面上
  15. [self performSelector:@selector(openCallout:) withObject:annotation afterDelay:5];
  16. self.progressLabel.text = @"Creating Annotation";
  17. return annotationView;
  18. }
  19. return nil;
  20. }
  21. //选中标注
  22. -(void)openCallout:(id<MKAnnotation>)annotation {
  23. self.progressLabel.text = @"Show Annotation";
  24. [self.mapView selectAnnotation:annotation animated:YES];
  25. }

当遇到错误和警告时同样可以通过实现MKMapViewDelegate协议和CLLocationManagerDelegate协议中的方法来提醒用户,代码如下所示:

  1. -(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
  2. NSString *errorType = (error.code == kCLErrorDenied)?@"Access Denied":@"Unknow Error";
  3. UIAlertView *av = [[UIAlertView alloc]initWithTitle:@"Error getting Location" message:errorType delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
  4. [av show];
  5. }
  6. -(void)mapView:(MKMapView *)mapView didFailToLocateUserWithError:(NSError *)error {
  7. UIAlertView *av = [[UIAlertView alloc]initWithTitle:@"Error loading map" message:[error description] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
  8. [av show];
  9. NSLog(@"%@",[error description]);
  10. }
  11. //UIAlertViewDelegate
  12. -(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
  13. self.progressLabel.text = @"";
  14. self.button.hidden = NO;
  15. }

完成效果如图-4、图-5、图-6所示:

图-4

图-5

图-6

1.4 完整代码

本案例中,ViewController.m文件中的完整代码如下所示:

 
  1. #import "ViewController.h"
  2. #import "Annotation.h"
  3. @interface ViewController ()<MKMapViewDelegate,CLLocationManagerDelegate,UIAlertViewDelegate>
  4. @property (weak, nonatomic) IBOutlet MKMapView *mapView;
  5. @property (weak, nonatomic) IBOutlet UILabel *progressLabel;
  6. @property (weak, nonatomic) IBOutlet UIButton *button;
  7. @property (nonatomic,strong) CLLocationManager *locationManager;
  8. @end
  9. @implementation ViewController
  10. - (void)viewDidLoad {
  11. [super viewDidLoad];
  12. self.mapView.mapType = MKMapTypeStandard;
  13. self.mapView.showsUserLocation = YES;
  14. }
  15. -(CLLocationManager *)locationManager {
  16. if (!_locationManager) {
  17. _locationManager = [[CLLocationManager alloc]init];
  18. }
  19. return _locationManager;
  20. }
  21. - (IBAction)findMe:(UIButton *)sender {
  22. //设置委托对象
  23. self.locationManager.delegate = self;
  24. //设置最佳精确度
  25. self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
  26. //开始更新
  27. [self.locationManager requestAlwaysAuthorization];
  28. [self.locationManager requestWhenInUseAuthorization];
  29. [self.locationManager startUpdatingLocation];
  30. //设置标签显示内容
  31. self.progressLabel.text = @"Delermining Current Location";
  32. sender.hidden = YES;
  33. }
  34. //CLLocationManagerDelegate协议方法
  35. -(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
  36. //获取当前位置
  37. CLLocation *newLocation = locations[0];
  38. NSLog(@"%@",newLocation);
  39. //设置显示区域
  40. MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(newLocation.coordinate, 2000, 2000);
  41. //调整区域以适合地图视图显示的宽高比
  42. MKCoordinateRegion adjustedRegion = [self.mapView regionThatFits:viewRegion];
  43. //将调整后的区域显示在地图视图上面
  44. [self.mapView setRegion:adjustedRegion animated:YES];
  45. manager.delegate = self;
  46. //由于更新位置是一个比较耗电量的操作,在不必要更新时可以关闭locationManager
  47. [manager stopUpdatingLocation];
  48. //更新标签显示
  49. self.progressLabel.text = @"Recerse Geocoding Location";
  50. //添加标注
  51. Annotation *annotation = [[Annotation alloc]init];
  52. annotation.coordinate = newLocation.coordinate;
  53. [self.mapView addAnnotation:annotation];
  54. self.progressLabel.text = @"Location Determined";
  55. }
  56. -(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
  57. NSString *errorType = (error.code == kCLErrorDenied)?@"Access Denied":@"Unknow Error";
  58. UIAlertView *av = [[UIAlertView alloc]initWithTitle:@"Error getting Location" message:errorType delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
  59. [av show];
  60. }
  61. //MapViewDelegate方法
  62. //当mapView需要一个标注视图时将调用该方法,在该方法中创建一个标注视图
  63. -(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
  64. static NSString *placemarkIdentifier = @"Map Location Identifier";
  65. if ([annotation isKindOfClass:[Annotation class]]) {
  66. MKPinAnnotationView *annotationView = (MKPinAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:placemarkIdentifier];
  67. if (!annotationView) {
  68. annotationView = [[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:placemarkIdentifier];
  69. }else {
  70. annotationView.annotation = annotation;
  71. }
  72. annotationView.enabled = YES;
  73. annotationView.animatesDrop = YES;
  74. annotationView.pinColor = MKPinAnnotationColorPurple;
  75. annotationView.canShowCallout = YES;
  76. //为了能够看到显示过程延迟5秒将标注视图添加到界面上
  77. [self performSelector:@selector(openCallout:) withObject:annotation afterDelay:5];
  78. self.progressLabel.text = @"Creating Annotation";
  79. return annotationView;
  80. }
  81. return nil;
  82. }
  83. //选中标注
  84. -(void)openCallout:(id<MKAnnotation>)annotation {
  85. self.progressLabel.text = @"Show Annotation";
  86. [self.mapView selectAnnotation:annotation animated:YES];
  87. }
  88. -(void)mapView:(MKMapView *)mapView didFailToLocateUserWithError:(NSError *)error {
  89. UIAlertView *av = [[UIAlertView alloc]initWithTitle:@"Error loading map" message:[error description] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
  90. [av show];
  91. NSLog(@"%@",[error description]);
  92. }
  93. //UIAlertViewDelegate
  94. -(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
  95. self.progressLabel.text = @"";
  96. self.button.hidden = NO;
  97. }
  98. @end
 

本案例中,Annotation.h文件中的完整代码如下所示:

 
  1. #import <Foundation/Foundation.h>
  2. #import <MapKit/MapKit.h>
  3. @interface Annotation : NSObject<MKAnnotation>
  4. @property (nonatomic,assign) CLLocationCoordinate2D coordinate;
  5. @end
 

本案例中,Annotation.m文件中的完整代码如下所示:

 
  1. #import "Annotation.h"
  2. @implementation Annotation
  3. -(NSString *)title {
  4. NSString *string = @"You are here";
  5. return string;
  6. }
  7. -(NSString *)subtitle {
  8. NSString *string = @"您当前位置";
  9. return string;
  10. }
  11. @end
posted on 2015-12-16 19:31  A蜗牛为梦想而生A  阅读(511)  评论(0编辑  收藏  举报