MKAnnotation
系统默认的大头针属性
//位置
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
@optional
//主标题和子标题
@property (nonatomic, readonly, copy, nullable) NSString *title;
@property (nonatomic, readonly, copy, nullable) NSString *subtitle;
自定义大头针
- 需要遵循大头针MKAnnotation协议
//在下面这个方法中实现自定义的大头针
-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{}
注意
过程类似于tableView中自定义cell
部分代码
-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{
//取出可以循环利用的大头针
static NSString * ID = @"anno";
MKPinAnnotationView * annotationView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:ID];
if (annotationView == nil) {
//没有循环利用的则自己创建新的
annotationView = [[MKPinAnnotationView alloc]initWithAnnotation:nil reuseIdentifier:ID];
//设置大头针的颜色iOS9.0以前的方法
//annotationView.pinColor = MKPinAnnotationColorGreen;
//iOS9.0以后的方法
annotationView.pinTintColor = [UIColor blackColor];
//从天而降
annotationView.animatesDrop = YES;
//是否显示标题
annotationView.canShowCallout = YES;
//标题显示的位置
annotationView.calloutOffset = CGPointMake(0, -10);//向上移动10的距离
//右边显示的View
annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeContactAdd];
//左边显示的View
annotationView.leftCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeContactAdd];
//底部显示细节View,会覆盖subtitle-----------通过这种方法可以插入图片
annotationView.detailCalloutAccessoryView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"IMG_0380" ]];
}
//传递模型覆盖掉之前的数据
annotationView.annotation = annotation;
return annotationView;
}
部分截图: