首页 > 移动平台 > 详细

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

时间:2019-03-28 13:24:11      阅读:153      评论:0      收藏:0      [点我收藏+]

 

场景:
  在处理Marker点击事件时,此时地图上有Marker点A及Marker点B,当选中Marker点A后,SDK方法
"didSelectAnnotationView"响应了点击事件,并进行了对应的逻辑处理(我在此进行了弹窗操作).
当关闭弹窗想再次选中Marker点A,此时"didSelectAnnotationView"不再响应.需选中Maker点B
后方法才会再次响应.也就是说当连续选中同一个Marker点时会导致"didSelectAnnotationView"
出现不响应的情况.

 

方案一:
  
///如果已经是选中状态,再次点击不会触发此回调。取消选中需调用
-(void)mapView:(MAMapView *)mapView didSelectAnnotationView:(MAAnnotationView *)view{
    [mapView deselectAnnotation:view.annotation animated:YES];
}
- (MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id<MAAnnotation>)annotation
{
//    [self.aOverlays addObject:annotation];
    if ([annotation isKindOfClass:[MAUserLocation class]]) {
        NSLog(@"用户定位点");
        if (self.startCoordinate.latitude == 0) {
            //            [self requestData:((MAUserLocation *)annotation).location.coordinate filed:@"priority"];
            self.startCoordinate = ((MAUserLocation *)annotation).location.coordinate;
            self.filed = @"2";//priority
            [self loadData];
        }
        self.startCoordinate = ((MAUserLocation *)annotation).location.coordinate;
        
        return nil;
    }
    if ([annotation isKindOfClass:[MAPointAnnotation class]])
    {
        static NSString *customReuseIndetifier = @"customReuseIndetifier";
        
        SDCustomAnnotationView *annotationView = (SDCustomAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:customReuseIndetifier];
        
        if (annotationView == nil)
        {
            annotationView = [[SDCustomAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:customReuseIndetifier];
            
        }
        kWeakSelf(self);
        annotationView.secletAnnotation = ^{
            NSInteger row = [weakself.annotations indexOfObject:annotation];
            NSLog(@"点击了---->%ld",(long)row);
        };
    annotationView.canShowCallout = YES;
        annotationView.image = nil;
        
        if ([annotation isKindOfClass:[MANaviAnnotation class]])
        {
            switch (((MANaviAnnotation*)annotation).type)
            {
                    //                case MANaviAnnotationTypeRailway:
                    //                    poiAnnotationView.image = [UIImage imageNamed:@"railway_station"];
                    //                    break;
                    //
                    //                case MANaviAnnotationTypeBus:
                    //                    poiAnnotationView.image = [UIImage imageNamed:@"bus"];
                    //                    break;
                    //
                    //                case MANaviAnnotationTypeDrive:
                    //                    poiAnnotationView.image = [UIImage imageNamed:@"car"];
                    //                    break;
                    //
                case MANaviAnnotationTypeWalking:
                    [self.aOverlays addObject:annotation];
                    annotationView.image = [UIImage imageNamed:@"man_map"];
                    break;
                    //                case MANaviAnnotationTypeRiding:
                    //                    poiAnnotationView.image = [UIImage imageNamed:@"ride"];
                    //                    break;
                    //
                default:
                    break;
            }
        }
        else
        {
            
            annotationView.image = [UIImage imageNamed:@"addr_map"];
            annotationView.count = annotation.subtitle;
        }
        
        return annotationView;
        
    }
  return nil;  
}

 

方案二:
  针对上述问题,可在创建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];
    }
}

 

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

原文:https://www.cnblogs.com/gongyuhonglou/p/10614221.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!