创建一个继承与NSObject的类
.h中的代码如下: 引入框架 遵循协议 重写协议中的三个属性
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
@interface MyAnnotation : NSObject<MKAnnotation>
//重写协议中的三个属性 coordinate title subtitle
//坐标
@property (nonatomic) CLLocationCoordinate2D coordinate;
//标题
@property (nonatomic, copy) NSString *title;
//子标题
@property (nonatomic, copy) NSString *subtitle;
//自定义大头针的图片
@property (nonatomic, strong) UIImage *image;
@end
接着回到viewController.m 具体操作如下:
#import "ViewController.h"
//引入框架
#import <CoreAudioKit/CoreAudioKit.h>
#import "MyAnnotation.h"
//地图使用的框架是MapKit
/*
大头针:
在iOS开发中经常会标记某个位置,需要使用地图标注,也就是大家俗称的“大头针”。只要一个NSObject类实现MKAnnotation协议就可以作为一个大头针,通常会重写协议中coordinate(标记位置)、title(标题)、subtitle(子标题)三个属性,然后在程序中创建大头针对象并调用addAnnotation:方法添加大头针即可(之所以iOS没有定义一个基类实现这个协议供开发者使用,多数原因应该是MKAnnotation是一个模型对象,对于多数应用模型会稍有不同,例如后面的内容中会给大头针模型对象添加其他属性)。
*/
@interface ViewController ()<CLLocationManagerDelegate, MKMapViewDelegate>
//定位管理器
@property (nonatomic, strong) CLLocationManager *manager;
//显示地图的视图
@property (nonatomic, strong) MKMapView *mapView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self createMapView];
}
#pragma mark- 创建视图
- (void)createMapView{
self.mapView = [[MKMapView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:self.mapView];
//设置代理
self.mapView.delegate = self;
//定位
self.manager = [[CLLocationManager alloc] init];
if ([CLLocationManager authorizationStatus] != kCLAuthorizationStatusAuthorizedWhenInUse) {
[self.manager requestAlwaysAuthorization];
}
//设置地图的定位追踪
_mapView.userTrackingMode = MKUserTrackingModeFollow;
//设置地图的显示类型
_mapView.mapType = MKMapTypeStandard;
//添加大头针
[self addAnnotation];
}
#pragma mark - 添加大头针
- (void)addAnnotation{
//设置位置
CLLocationCoordinate2D location1 = CLLocationCoordinate2DMake(29.5, 116);
CLLocationCoordinate2D location2 = CLLocationCoordinate2DMake(40, 116);
CLLocationCoordinate2D location3 = CLLocationCoordinate2DMake(20, 110.5);
MyAnnotation *annotation1 = [[MyAnnotation alloc] init];
annotation1.coordinate = location1;
annotation1.title = @"九江";
annotation1.subtitle = @"呵呵哒";
annotation1.image = [UIImage imageNamed:@"1.png"];
[self.mapView addAnnotation:annotation1];
MyAnnotation *annotation2 = [[MyAnnotation alloc] init];
annotation2.coordinate = location2;
annotation2.title = @"北京";
annotation2.subtitle = @"北京欢迎您";
[self.mapView addAnnotation:annotation2];
MyAnnotation *annotation3 = [[MyAnnotation alloc] init];
annotation3.coordinate = location3;
annotation3.title = @"呵呵";
annotation3.subtitle = @"呵呵";
[self.mapView addAnnotation:annotation3];
}
//显示大头针的时候才会调用
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
//判断是否是当前自定义的大头针类
if ([annotation isKindOfClass:[MyAnnotation class]]) {
//先定义一个重用标识
static NSString *identifier = @"AnnotationOne";
MKAnnotationView *annotationView = [self.mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
if (!annotationView) {
annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
//允许用户交互
annotationView.canShowCallout = YES;
//设置详情和大头针的头偏移量
annotationView.calloutOffset = CGPointMake(0, 1);
//设置详情的左视图
annotationView.leftCalloutAccessoryView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"1.png"]];
}
//修改大头针视图
annotationView.annotation = annotation;
annotationView.image = ((MyAnnotation *)annotation).image;
return annotationView;
}
else{
return nil;
}
}