解决iOS集成高德SDK,Marker点重复点击无效问题.

近期在写一个关于共享汽车的项目,其主要功能依赖于地图进行展示,在集成高德SDK时遇上了一些坑,作为记录和分享.

场景:
  在处理Marker点击事件时,此时地图上有Marker点A及Marker点B,当选中Marker点A后,SDK方法
"didSelectAnnotationView"响应了点击事件,并进行了对应的逻辑处理(我在此进行了弹窗操作).
当关闭弹窗想再次选中Marker点A,此时"didSelectAnnotationView"不再响应.需选中Maker点B
后方法才会再次响应.也就是说当连续选中同一个Marker点时会导致"didSelectAnnotationView"
出现不响应的情况.
方案:
  针对上述问题,可在创建Annotationview时给这个Annotationview添加Tap手势
自己对事件进行处理不依赖SDK提供的方法.
(如有更为便捷有效的方法,欢迎留言建议)
核心代码
/** 
  * @brief 根据anntation生成对应的View(设置标准样式) 
  * @param mapView 地图View 
  * @param annotation 指定的标注
  * @return 生成的标注View 
*/
- (MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id)annotation
{
    if ([annotation isKindOfClass:[MAPointAnnotation class]])
    {
       static NSString *pointDefaultIndentifier = @"pointDefaultIndentifier";
        AnnotationViewManager *annotationView = (AnnotationViewManager *)[mapView dequeueReusableAnnotationViewWithIdentifier:pointDefaultIndentifier];
        if (annotationView == nil)
        {
            annotationView = [[AnnotationViewManager alloc] initWithAnnotation:annotation reuseIdentifier:pointDefaultIndentifier];
            // 给Marker点添加手势
            [annotationView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onMarkerClick:)]];
        }
        annotationView.centerOffset = CGPointMake(0, -18);
        annotationView.image = [UIImage imageNamed:@"map_tubiao_zuche_icon"];
        return annotationView;
    }
    return nil;
}
// Marker选中事件
- (void)onMarkerClick:(UITapGestureRecognizer *)gesture
{
    // 这里做你想的事情
    MAAnnotationView *annoView = (MAAnnotationView*)gesture.view;
    NSLog(@"选中了: %@",annoView.annotation.title);
 
    // 解决5.0.0上Annotation选中后重用的bug.
    if(annoView.annotation == self.mapView.selectedAnnotations.firstObject)
    {
        if(annoView.selected == NO)
        {   
            [annoView setSelected:YES animated:YES];
        }
        return;
    }
    else
    {
        [self.mapView selectAnnotation:annoView.annotation animated:YES];
    }
}

参考:
高德官方Demo GitHub
安利: 在集成高德SDK时若如遇上问题,解决无果可以直接和官方技术客服联系,大多数问题都能得到解决.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容