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

 

场景:
  在处理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点重复点击无效问题

上一篇:mac下面 ,启动2个桌面版appium的方法


下一篇:怎么将安卓手机投屏到电脑上