地图 大头针

MyAnnotation.h

 1 #import <Foundation/Foundation.h>
 2 #import <MapKit/MapKit.h>
 3 
 4 @interface MyAnnotation : NSObject <MKAnnotation>
 5 
 6 // 重写协议中的三个属性coordinate(标记位置)、title(标题)、subtitle(子标题)
 7 /**  坐标  */
 8 @property (nonatomic) CLLocationCoordinate2D coordinate;
 9 
10 /**  标题  */
11 @property (nonatomic, copy) NSString *title;
12 
13 /**  子标题  */
14 @property (nonatomic, copy) NSString *subtitle;
15 
16 
17 // 自定义大头针
18 /**  自定义大头针显示的图片  */
19 @property (nonatomic, strong) UIImage *image;
20 
21 @end

ViewController.m

  1 #import "ViewController.h"
  2 
  3 // 地图使用的是MapKit框架
  4 #import <MapKit/MapKit.h>
  5 #import <CoreLocation/CoreLocation.h>
  6 #import "MyAnnotation.h"
  7 
  8 @interface ViewController () <MKMapViewDelegate>
  9 
 10 /**  定位管理器  */
 11 @property (nonatomic, strong) CLLocationManager *locationManager;
 12 
 13 /**  显示地图的视图  */
 14 @property (nonatomic, strong) MKMapView *mapView;
 15 
 16 @end
 17 
 18 @implementation ViewController
 19 
 20 - (void)viewDidLoad {
 21     [super viewDidLoad];
 22     
 23 /*
 24  大头针:在iOS开发中经常会标记某个位置,需要使用地图标注,也就是大家俗称的“大头针”。只要一个NSObject类实现MKAnnotation协议就可以作为一个大头针,通常会重写协议中coordinate(标记位置)、title(标题)、subtitle(子标题)三个属性,然后在程序中创建大头针对象并调用addAnnotation:方法添加大头针即可(之所以iOS没有定义一个基类实现这个协议供开发者使用,多数原因应该是MKAnnotation是一个模型对象,对于多数应用模型会稍有不同,例如后面的内容中会给大头针模型对象添加其他属性)。
 25 */
 26     
 27     // 创建视图
 28     [self createMapView];
 29 }
 30 
 31 
 32 #pragma mark - 创建视图
 33 - (void)createMapView {
 34     
 35     // 创建地图,并添加到当前视图上
 36     self.mapView = [[MKMapView alloc] initWithFrame:[UIScreen mainScreen].bounds];
 37     [self.view addSubview:self.mapView];
 38     
 39     // 设置代理
 40     _mapView.delegate = self;
 41     
 42     // 定位
 43     self.locationManager = [[CLLocationManager alloc] init];
 44     
 45     if (![CLLocationManager locationServicesEnabled]) {
 46         NSLog(@"当前设备定位不可用");
 47     }
 48     
 49     if ([CLLocationManager authorizationStatus] != kCLAuthorizationStatusAuthorizedWhenInUse) {
 50         [self.locationManager requestWhenInUseAuthorization];
 51     }
 52     
 53     // 设置地图的定位追踪
 54     _mapView.userTrackingMode = MKUserTrackingModeFollow;
 55     
 56     // 设置地图的类型
 57     _mapView.mapType = MKMapTypeStandard;
 58     
 59     // 添加大头针
 60     [self addAnnotation];
 61     
 62 }
 63 
 64 
 65 #pragma mark - 添加大头针
 66 - (void)addAnnotation {
 67     
 68     // 设置位置
 69     CLLocationCoordinate2D location1 = CLLocationCoordinate2DMake(40, 116);
 70     CLLocationCoordinate2D location2 = CLLocationCoordinate2DMake(39, 121);
 71     CLLocationCoordinate2D location3 = CLLocationCoordinate2DMake(31, 121);
 72     
 73     
 74     MyAnnotation *annotation1 = [[MyAnnotation alloc] init];
 75     annotation1.coordinate = location1;
 76     annotation1.title = @"北京";
 77     annotation1.subtitle = @"大风起兮云飞扬";
 78 #warning 添加代码
 79     annotation1.image = [UIImage imageNamed:@"1.jpg"];
 80     
 81     MyAnnotation *annotation2 = [[MyAnnotation alloc] init];
 82     annotation2.coordinate = location2;
 83     annotation2.title = @"大连";
 84     annotation2.subtitle = @"哈哈哈";
 85 #warning 添加代码
 86     annotation2.image = [UIImage imageNamed:@"1.jpg"];
 87     
 88     MyAnnotation *annotation3 = [[MyAnnotation alloc] init];
 89     annotation3.coordinate = location3;
 90     annotation3.title = @"上海";
 91     annotation3.subtitle = @"嘿嘿嘿";
 92 #warning 添加代码
 93     annotation3.image = [UIImage imageNamed:@"1.jpg"];
 94     
 95     [_mapView addAnnotations:@[annotation1, annotation2, annotation3]];
 96 }
 97 
 98 
 99 #warning 增加代码
100 #pragma mark - 实现自定义大头针的代理方法
101 // 显示大头针的时候才会调用
102 - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
103     
104     // 判断是否是当前自定义的大头针类
105     if ([annotation isKindOfClass:[MyAnnotation class]]) {
106         
107         // 先定义一个重用标识
108         static NSString *identifier = @"AnnotationOne";
109         MKAnnotationView *annotationView = [_mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
110         if (!annotationView) {
111             annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
112             // 允许用户交互
113             annotationView.canShowCallout = YES;
114             // 设置详情和大头针的头偏移量
115             annotationView.calloutOffset = CGPointMake(0, 1);
116             // 设置详情的左视图
117             annotationView.leftCalloutAccessoryView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"1.jpg"]];
118         }
119         // 修改大头针视图
120         annotationView.annotation = annotation;
121         annotationView.image = ((MyAnnotation *)annotation).image;
122         return annotationView;
123     } else {
124         return nil;
125     }
126 }
127 
128 
129 #pragma mark - 代理方法
130 - (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation {
131     
132     NSLog(@"%@", userLocation);
133 }
134 
135 @end
原文地址:https://www.cnblogs.com/zhizunbao/p/5547286.html