高德地图api自定义标注点重复点击问题
1.在使用高德地图api以后,我们可以接入创建一个地图,并且将地图添加在我们的可视区域内
-(void)createMAMapView{
self.mapView = [[MAMapView alloc] init];
self.mapView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.mapView.delegate = self;
//普通样式
[self.mapView setZoomLevel:14 animated:YES];
self.mapView.mapType = MAMapTypeStandard;
self.mapView.scrollEnabled=YES;//是否支持平移
self.mapView.zoomEnabled = YES;//是否支持缩放
self.mapView.showsCompass= NO;//隐藏指南针
self.mapView.showsUserLocation = YES;
self.mapView.userTrackingMode = MAUserTrackingModeFollow;//
[self.view addSubview:self.mapVie
[self.mapView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(0);
make.right.mas_equalTo(0);
make.top.mas_equalTo(0);
make.bottom.mas_equalTo(0);
}];
2.我们在根据获取的数据,初始化标记点,在初始化时,需要先移除地图上的已有标记点
[self.mapView removeAnnotations:self.annotations];
再根据数据中的经纬度去添加这些标记点
for (int i = 0; i < self.mutableArray.count; ++i)
{
MAPointAnnotation *point = [[MAPointAnnotation alloc] init];
RedEnvelopModel *curentModel=self.mutableArray[i];
point.coordinate = CLLocationCoordinate2DMake([curentModel.lat doubleValue], [curentModel.lng doubleValue]);
[self.annotations addObject:point];
}
3.然后再把这些标记点展示在地图上
[self.mapView addAnnotations:self.annotations];
[self.mapView showAnnotations:self.annotations animated:YES];
4.在调用地图点击标记点的代理的时候,我们发现高德地图在选中标记点时经常无法重复点击问题,按照网上描述文档还是会出现一些问题,因此,我们舍弃了选中标记点的代理方法
- (void)mapView:(MAMapView *)mapView didSelectAnnotationView:(MAAnnotationView *)view;采用一中比较熟息的方法去调用
5.我们创建一个 CustomAnnotationView 继承于MAAnnotationView ,并且在 CustomAnnotationView 上添加一个按钮,通过按钮的点击事件,取代地图的标记点的点击事件
6,在生成标注视图的代理方法里我们可以这样写
- (MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id )annotation{
if ([annotation isKindOfClass:[MAUserLocation class]]) {
return nil;
}else if ([annotation isKindOfClass:[MAPointAnnotation class]]){
static NSString *customReuseIndetifier = @“customReuseIndetifier”;
CustomAnnotationView *annotationView = (CustomAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:customReuseIndetifier];
if (annotationView == nil)
{
annotationView = [[CustomAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:customReuseIndetifier];
// must set to NO, so we can show the custom callout view.
annotationView.canShowCallout = NO;
annotationView.draggable = NO;
RedEnvelopModel *redEnvelopModel;
for (int i=0; i<self.mutableArray.count; i++) {
redEnvelopModel=self.mutableArray[i];
if (annotation==self.annotations[i]) {//这里是生成对应标记点上的视图
[annotationView.portraitImageView addTarget:self action:@selector(markerAction:) forControlEvents:UIControlEventTouchUpInside];
annotationView.tag=2000+i;
annotationView.portraitImageView.tag=1000+i;
[annotationView.portraitImageView sd_setBackgroundImageWithURL:[NSURL URLWithString:imageURL(redEnvelopModel.img)] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"default_hongbao_bg"]];//标记点上添加对应图片
}
}
}
return annotationView;
}
7.最后再响应对应的点击事件就可以了
-(void)markerAction:(UIButton *)sender{
}