添加框架CoreLocation、MapKit框架
一、首先了解大头针
要添加大头针需实现协议MKAnnotation ,由下图可以看到,一定要实现coordinate属性。
二、实现大头针协议
添加MyAnnotation类
#import <Foundation/Foundation.h> #import <MapKit/MapKit.h> //MKAnnotation是一个协议 ,添加大头针久需要实现该协议 //coordinate 属性是必须的,所以需要实现该属性 @interface MyAnnotation : NSObject<MKAnnotation> -(id)initWithCoordinate:(CLLocationCoordinate2D)coordinate title:(NSString *)title subtitle:(NSString *)subtitle; @end
// // MyAnnotation.m // MapView // // Created by City--Online on 15/4/20. // Copyright (c) 2015年 CYW. All rights reserved. // #import "MyAnnotation.h" @interface MyAnnotation () { CLLocationCoordinate2D _coordinate; NSString *_title; NSString *_subTitle; } @end @implementation MyAnnotation -(id)initWithCoordinate:(CLLocationCoordinate2D)coordinate title:(NSString *)title subtitle:(NSString *)subtitle { if (self==[super init]) { _coordinate=coordinate; _title=title; _subTitle=subtitle; } return self; } // 必选 -(CLLocationCoordinate2D)coordinate { return _coordinate; } //可选 -(NSString *)title { return _title; } -(NSString *)subtitle { return _subTitle; } @end
三、地图和添加大头针
#import "ViewController.h" #import <CoreLocation/CoreLocation.h> #import <MapKit/MapKit.h> #import "MyAnnotation.h" @interface ViewController ()<CLLocationManagerDelegate,MKMapViewDelegate> @property(nonatomic,strong)MKMapView *mapView; @property(nonatomic,strong)CLLocationManager *locationManager; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; _locationManager=[[CLLocationManager alloc]init]; BOOL enable=[CLLocationManager locationServicesEnabled]; NSLog(@"%d",[CLLocationManager authorizationStatus]); if (!enable) { NSLog(@"用户未开启定位服务,无法定位"); return; } else if ([CLLocationManager authorizationStatus]<3) { [_locationManager requestWhenInUseAuthorization]; } CLLocationDistance distance=10.0; _locationManager.distanceFilter=distance; _locationManager.delegate=self; _locationManager.desiredAccuracy=kCLLocationAccuracyBest; [_locationManager startUpdatingLocation]; self.mapView=[[MKMapView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)]; //地图类型 // typedef NS_ENUM(NSUInteger, MKMapType) { // MKMapTypeStandard = 0, //标准地图 // MKMapTypeSatellite, //卫星地图 // MKMapTypeHybrid //混合地图 // } self.mapView.mapType=MKMapTypeStandard; //是否定位当前位置 否显示当前位置 self.mapView.showsUserLocation=YES; //代理设置 self.mapView.delegate=self; // typedef struct { // CLLocationCoordinate2D center; // MKCoordinateSpan span; // } MKCoordinateRegion; //中心点经纬度 CLLocationCoordinate2D locationCoordinate2D; locationCoordinate2D.latitude=22.544349; locationCoordinate2D.longitude= 113.94787; //范围比例尺 MKCoordinateSpan coordinateSpan; //纬度 coordinateSpan.latitudeDelta=0.05; //经度 coordinateSpan.longitudeDelta=0.05; //设置显示区域 MKCoordinateRegion coordinateRegion; coordinateRegion.center=locationCoordinate2D; coordinateRegion.span=coordinateSpan; [self.mapView setRegion:coordinateRegion animated:YES]; // self.mapView.region=coordinateRegion; // typedef NS_ENUM(NSInteger, MKUserTrackingMode) { // MKUserTrackingModeNone = 0, // the user's location is not followed // MKUserTrackingModeFollow, // the map follows the user's location // MKUserTrackingModeFollowWithHeading, // the map follows the user's location and heading // } self.mapView.userTrackingMode=MKUserTrackingModeFollow; [self.view addSubview:self.mapView]; UISegmentedControl *segmentedControl=[[UISegmentedControl alloc]initWithItems:@[@"普通地图",@"卫星地图",@"混合地图"]]; segmentedControl.selectedSegmentIndex=0; [segmentedControl addTarget:self action:@selector(selectSegmentIndex:) forControlEvents:UIControlEventValueChanged]; self.navigationItem.titleView=segmentedControl; //定义大头针类 MyAnnotation *annotation=[[MyAnnotation alloc]initWithCoordinate:locationCoordinate2D title:@"主标题" subtitle:@"子标题"]; // annotation.image=[UIImage imageNamed:@"annotation.png"]; [self.mapView addAnnotation:annotation]; } -(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations { if (locations.count>0) { CLLocation *location=[locations firstObject]; CLLocationCoordinate2D locationCoordinate2D =location.coordinate; [self.mapView setRegion:MKCoordinateRegionMake(locationCoordinate2D, self.mapView.region.span) animated:YES]; NSLog(@"%lf ,%lf",locationCoordinate2D.longitude,locationCoordinate2D.longitude); MyAnnotation *annotation=[[MyAnnotation alloc]initWithCoordinate:locationCoordinate2D title:@"定位位置" subtitle:@"子标题"]; // annotation.image=[UIImage imageNamed:@"annotation.png"]; [self.mapView addAnnotation:annotation]; } } //默认返回nil,采用复用的思想 //显示大头针时调用,注意方法中的annotation参数是即将显示的大头针对象 -(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation { if ([annotation isKindOfClass:[MKUserLocation class]]) { return nil; } static NSString *key=@"myannotation"; MKPinAnnotationView *pinAnnotation=(MKPinAnnotationView *)[self.mapView dequeueReusableAnnotationViewWithIdentifier:key]; if (!pinAnnotation) { pinAnnotation=[[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:key]; } //动画加载 pinAnnotation.animatesDrop=true; // typedef NS_ENUM(NSUInteger, MKPinAnnotationColor) { // MKPinAnnotationColorRed = 0, // MKPinAnnotationColorGreen, // MKPinAnnotationColorPurple // } //大头针颜色 pinAnnotation.pinColor=MKPinAnnotationColorPurple; //是否显示气泡(交互) pinAnnotation.canShowCallout=true; return pinAnnotation; } //大头针选中 -(void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view { MyAnnotation *annotation=view.annotation; NSLog(@"%@",annotation.title); } //定位失败 -(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error { NSLog(@"定位失败"); NSLog(@"%@",error); } - (void)selectSegmentIndex:(UISegmentedControl *)control { NSInteger index = control.selectedSegmentIndex; /* MKMapTypeStandard 普通地图(标准地图), MKMapTypeSatellite 卫星地图, MKMapTypeHybrid 混合地图 */ MKMapType mapType; switch (index) { //普通地图 case 0: mapType = MKMapTypeStandard; break; //卫星地图 case 1: mapType = MKMapTypeSatellite; break; //普通和卫星的混合地图 case 2: mapType = MKMapTypeHybrid; break; default: break; } //改变地图类型 self.mapView.mapType = mapType; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
四、注意
1.在info.plist中添加Key:NSLocationWhenInUseUsageDescription
2.在AppDelegate中添加导航控制器