1. 创建之前的准备工作
1.1 在Info.plist文件中添加下面两句话
NSLocationAlwaysUsageDescription —> 确定定位吗?亲
//请求的授权,除了可以在APP打开时允许定位服务,也可以在APP进入后台仍然可以使用定位服务(永久) --> 与上边一个可以二选一
[_locationManager requestAlwaysAuthorization];
NSLocationWhenInUseUsageDescripyion —>需要定位吗?
//此方法请求的授权,仅限于用户在打开使用APP时允许使用系统的定位服务(在应用使用期间)
[_locationManager requestWhenInUseAuthorization];
1.2 导入库文件 MapKit.framework 和 CoreLocation.framework
2. 工程创建说明 ---> 使用系统自带的MKPointAnnotation、MKPinAnnotationView 及自定义 MKPointAnnotation、MKPinAnnotationView
以下是ViewController.m 文件
2.1 viewDidLoad 中的加载
1 #import "ViewController.h" 2 #import "MapKit/MapKit.h" 3 #import "MyPointAnnotation.h" 4 #import "MyAnnotationView.h" 5 6 @interface ViewController ()<MKMapViewDelegate> { 7 8 MKMapView *_mapView; //地图对象 9 UILabel *_userLocationLable; //查看用户坐标 10 } 11 12 @end 13 14 @implementation ViewController 15 16 - (void)viewDidLoad { 17 [super viewDidLoad]; 18 19 //调用创建地图视图的方法 20 [self createMapView]; 21 //调用创建大头针的方法 22 [self createAnnotations]; 23 //调用这个方法调用所有的覆盖层的方法 24 [self overLay]; 25 //调用创建UI的方法 26 [self createUI]; 27 } 28 29 @end
2.2 创建地图视图的方法
1 //创建地图视图的方法 2 - (void)createMapView { 3 //创建地图对象 4 _mapView = [[MKMapView alloc] initWithFrame:self.view.bounds]; 5 //设置map的类型 或地图模式 6 _mapView.mapType = MKMapTypeStandard; 7 /* 8 MKMapTypeStandard = 0, 纸张地图 标准地图 9 MKMapTypeSatellite, 纯卫星地图 10 MKMapTypeHybrid, 混合式地图 描述的卫星图 11 */ 12 //设置map的初始位置 13 //创建地理坐标2D 需要经度和纬度 如:经度:120.366486 纬度:36.083743 14 CLLocationCoordinate2D location = CLLocationCoordinate2DMake(36.083743, 120.366486); 15 //起始时 锁定一个矩形为1000 X 1000米的方位 ,坐标点location 16 _mapView.region = MKCoordinateRegionMakeWithDistance(location, 1000, 1000); 17 //设置地图能否放大缩小 18 _mapView.zoomEnabled = YES; 19 //设置地图能否滚动 20 _mapView.scrollEnabled = YES; 21 //设置显示用户的位置 先判断是否开始了定位服务 22 if ([CLLocationManager locationServicesEnabled] == YES) { 23 //显示用户的位置 24 _mapView.showsUserLocation = YES; 25 //设置用户的基本跟踪状态 26 [_mapView setUserTrackingMode:MKUserTrackingModeFollowWithHeading animated:YES]; 27 28 /* 29 MKUserTrackingModeNone = 0, 不尾随 一般不设 30 MKUserTrackingModeFollow, 尾随用户位置,地图保存正向(北方向) 31 MKUserTrackingModeFollowWithHeading 随着地图旋转而尾随(地图方向和设备方向同步) 32 */ 33 } 34 //设置代理 需遵守MKMapViewDelegate代理协议 35 _mapView.delegate = self; 36 //将地图加入到self.view上 37 [self.view addSubview:_mapView]; 38 } 39 40 #pragma mark - 地图协议中的方法 - 41 - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation { 42 43 //这里判断必须要加,这个方法在刷新时会将用户的位置也会传到这个方法里,所以需要判断,如果不是大头针就是用户坐标点(蓝点),如果是蓝点(用户坐标点) 直接返回nil 44 if ([annotation isKindOfClass:[MKPointAnnotation class]] == NO) { 45 return nil; 46 } 47 48 //调用系统的方法 49 // MKPinAnnotationView *annotationView = [self systemMethodWithMapView:mapView andAnnotation:annotation]; 50 51 //调用自定义的方法 52 MKPinAnnotationView *annotationView = [self customMethodWithMapView:mapView andAnnotation:annotation]; 53 54 return annotationView; 55 } 56 57 //自定义的方法 58 - (MKPinAnnotationView *)customMethodWithMapView:(MKMapView *)mapView andAnnotation:(MKPointAnnotation *)annotation { 59 MyAnnotationView *annotationView = (MyAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@"CustomAnnotationView"]; 60 61 if (annotationView == nil) { 62 63 64 annotationView = [[MyAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"CustomAnnotationView"]; 65 66 }else { 67 68 annotationView.annotation = annotation; 69 } 70