一、大头针
MapKit没有自带的大头针,只有大头针协议MKAnnotation,我们需要自定义大头针:
步骤:
1)创建一个继承NSObject的类
2)实现MKAnnotation协议
3)必须创建一个属性,用于存储大头针位置@property (nonatomic) CLLocationCoordinate2D coordinate;
二、示例代码
1、自定义的大头针类
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
@interface LTAnnotation : NSObject <MKAnnotation>
/* 必须创建的属性 */
@property (nonatomic) CLLocationCoordinate2D coordinate;
/* 可选的属性 */
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *subtitle;
/* 自定义的属性 */
@property (nonatomic, strong) UIImage *icon;
@end
2、应用
- (void)viewDidLoad {
[super viewDidLoad];
//请求定位授权
[self requestUserLocationAuthor];
//初始化MKMapView
[self initMapView];
//添加大头针
[self addAnnotationsToMapView];
}
- (void)addAnnotationsToMapView{
CLLocationCoordinate2D location1 = CLLocationCoordinate2DMake(22.54, 114.02);
//创建大头针
LTAnnotation *annotation = [[LTAnnotation alloc] init];
annotation.title = @"执着";
annotation.subtitle = @"执着哥开的店";
annotation.coordinate = location1;
annotation.icon = [UIImage imageNamed:@"red"];
//添加大头针
[self.mapView addAnnotation:annotation1];
}