ios点击大头针气泡不弹出_地图大头针气泡点击事件

地图大头针气泡点击事件

项目中需要实现点击地图中大头针点击后出现气泡的点击事件,使用系统的方法一直没办法实现,最后发现一个很取巧的方案去解决了这个问题.

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id )annotation{

// If the annotation is the user location, just return nil.(如果是显示用户位置的Annotation,则使用默认的蓝色圆点)

if ([annotation isKindOfClass:[MKUserLocation class]])

return nil;

if ([annotation isKindOfClass:[MKPointAnnotation class]]) {

// Try to dequeue an existing pin view first.(这里跟UITableView的重用差不多)

MKAnnotationView *customPinView = (MKAnnotationView*)[mapView

dequeueReusableAnnotationViewWithIdentifier:@"CustomPinAnnotationView"];

if (!customPinView){

// If an existing pin view was not available, create one.

customPinView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"CustomPinAnnotationView"];

}

//大头针的图标改变

customPinView.image = [UIImage imageNamed:@"myimage"];

//是否允许插入气泡

customPinView.canShowCallout = YES;

//实现气泡点击需要设置右侧的点击详情按钮,但是需要将创建的button类型设置为除默认类型如UIButtonTypeInfoLight,核心代码

UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeInfoLight];

rightButton.frame = CGRectMake(0, 0, 2, 2);

customPinView.rightCalloutAccessoryView = rightButton;

customPinView.rightCalloutAccessoryView.backgroundColor = [UIColor whiteColor];

return customPinView;

}

return nil;//返回nil代表使用默认样式

}

//气泡的点击事件

-(void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control{

NSLog(@"点击了查看详情");

}

核心就是将rightCalloutAccessoryView设置的button创建类型修改为除了默认类型的其他类型.但是此处还有一个问题不能实现传值.通过运行时创建MKAnnotationView的分类动态添加属性解决传值的问题.当然也可以采用自定义气泡解决问题.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值