系统大头针
大头针基本操作
// 添加一个大头针
- (void)addAnnotation:(id <MKAnnotation>)annotation;
// 添加多个大头针
- (void)addAnnotations:(NSArray *)annotations;
// 移除一个大头针
- (void)removeAnnotation:(id <MKAnnotation>)annotation;
// 移除多个大头针
- (void)removeAnnotations:(NSArray *)annotations;
新建大头针模型
@interface MyAnnotation : NSObject <MKAnnotation>
// 坐标位置
@property (nonatomic, assign) CLLocationCoordinate2D coordinate;
// 标题
@property (nonatomic, copy) NSString *title;
// 子标题
@property (nonatomic, copy) NSString *subtitle;
@end
添加大头针
MyAnnotation *anno = [[MyAnnotation alloc] init];
anno.title = @"北京";
anno.subtitle = @"空气清新的地方";
anno.coordinate = CLLocationCoordinate2DMake(40, 116);
[self.mapView addAnnotation:anno];
自定义大头针
方式: 设置mapView的代理, 实现代理方法
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation;
注意:
1> 如果返回nil,显示出来的大头针就采取系统的默认样式
2> 标识用户位置的蓝色发光圆点,它也是一个大头针,当显示这个大头针时,也会调用代理方法
3> 因此,需要在代理方法中分清楚(id )annotation参数代表自定义的大头针还是蓝色发光圆点