Mapview自定义大头针

本文介绍了如何在Mapview中自定义大头针视图。通过创建继承自MKAnnotationView的CJAnnotationView,实现大头针的复用和自定义。大头针模型CJAnnotation用于存储属性,通过addAnnotation方法添加到地图上。通过重写set方法,可以在CJAnnotationView中设置自定义图片。此外,如果只需要更改默认大头针颜色,可以在代理方法中使用MKPinAnnotationView并调整其属性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.MKAnnotationView是大头针的view,他像cell一样是复用的,所以我们可以自定义一个大头针的view(CJAnnotationView),使他继承MKAnnotationView

2.这个大头针的view创建就像cell一样是从缓存池中取出的,所以我们用以下代码来创建即可,

+ (instancetype)annotationViewWithMap:(MKMapView *)mapView
{
    static NSString *identifier = @"anno";

    // 1.从缓存池中取
    HMAnnotationView *annoView = (HMAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
    // 2.如果缓存池中没有, 创建一个新的
    if (annoView == nil) {

        annoView = [[HMAnnotationView alloc] initWithAnnotation:nil reuseIdentifier:identifier];
    }

    return annoView;
}

在init方法里面设置一些初始化操作

- (instancetype)initWithAnnotation:(id<MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier]) {
        // 初始化
        // 设置大头针标题是否显示
        self.canShowCallout = YES;

        // 设置大头针左边的辅助视图
        self.leftCalloutAccessoryView = [[UISwitch alloc] init];

        // 设置大头针右边的辅助视图
        self.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeContactAdd];


    }
    return self;
}

另外,大头针模型是自定义的CJAnnotation,这里面我们可以存放我们需要的大头针的属性,例如:

 /**
 *  大头针的位置
 */
@property (nonatomic, assign) CLLocationCoordinate2D coordinate;
/**
 *  大头针标题
 */
@property (nonatomic, copy) NSString *title;
/**
 *  大头针的子标题
 */
@property (nonatomic, copy) NSString *subtitle;

/**
 *  图标
 */
@property (nonatomic, copy) NSString *icon;

在控制其中,我们创建好大头针模型设置好各种属性,利用addAnnotation将大头针模型传递进去

    //创建大头针模型
    CJAnnotation *annotation1 = [[CJAnnotation alloc] init];
    annotation1.title = @"浙江";
    annotation1.subtitle = @"衢州";
    CGFloat latitude = 36.821199 + arc4random_uniform(20);
    CGFloat longitude = 116.858776 + arc4random_uniform(20);
    annotation1.coordinate = CLLocationCoordinate2DMake(latitude, longitude);
    annotation1.icon = @"category_1";
    // 添加大头针
    [self.mapView addAnnotation:annotation1];

这个时候就会添加大头针,在添加打头阵的时候,会调用一下代理方法

/**
 *  每次添加大头针就会调用这哥方法
 *
 *  @param mapView    地图
 *  @param annotation 大头针模型
 *
 *  @return 大头针的view
 */
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
    // 对用户当前的位置的大头针特殊处理(已经定位的也是一个大头针,我们要把这个大头针给排除掉)
    // 注意: 如果返回nil, 系统会按照自己默认的方式显示
    if (![annotation isKindOfClass:[CJAnnotation class]])
        return nil;

   CJAnnotationView *annotationView = [CJAnnotationView annotationViewWithMapView:mapView];
   //这里面的问题就来了,我们是怎么样将大头针设置为一张自定义的图片
    annotationView.annotation = annotation;
    return annotationView;
}

//这里面的问题就来了,我们是怎么样将大头针设置为一张自定义的图片,annotation只是一个模型,我们就可以重写这个set方法进行设置
annotationView.annotation = annotation;

在自定义的CJAnnotationView中重写set方法,这样就把图片设置进去了

- (void)setAnnotation:(CJAnnotation *)annotation
{
    [super setAnnotation:annotation];

    //     处理自己特有的操作
    self.image = [UIImage imageNamed:annotation.icon];
}

—————————————————-华丽的分割线—————————————————
以上是一种自定义图片的大头针的创建,如果不需要自定义图片,只是在按照原来的默认的大头针的样式来显示,只是改变一下颜色而已,有以下方法

在代理方法中
- (MKAnnotationView )mapView:(MKMapView )mapView viewForAnnotation:(id)annotation
不是使用MKAnnotationView,而是MKPinAnnotationView,但还是从缓存池中取,并设置一些属性,详情见代码例子


        // 设置大头针的颜色
        annoView.pinColor = MKPinAnnotationColorPurple;

        // 设置大头针从天而降
        annoView.animatesDrop = YES;

        // 设置大头针标题是否显示
        annoView.canShowCallout = YES;

        // 设置大头针标题显示的偏移位
        annoView.calloutOffset = CGPointMake(-50, 0);

        // 设置大头针左边的辅助视图
        annoView.leftCalloutAccessoryView = [[UISwitch alloc] init];
        // 设置大头针右边的辅助视图
        annoView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeContactAdd];
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值