为当前位置注释设置canShowCallOut = NO,iPhone

cha*_*tur 15 objective-c mkannotation ios currentlocation

我正在使用当前位置图标的自定义调出(标题和副标题).我尝试了以下来禁用默认注释,但它不起作用.

- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation
{
    NSLog(@"viewForAnnotation");
    if ([annotation isKindOfClass:[MKUserLocation class]])
    {
        MKAnnotationView *userLocationView = [mapView viewForAnnotation:annotation];
        userLocationView.canShowCallout = NO;
        NSLog(@"[annotation isKindOfClass:[MKUserLocation class]");
        return nil;
    }

}
Run Code Online (Sandbox Code Playgroud)

只有它的工作方式是

-(void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)ann
{
    if([ann.annotation isKindOfClass:[MKUserLocation class]] )
    {
       [mymap deselectAnnotation:ann.annotation animated:NO];
    }
}
Run Code Online (Sandbox Code Playgroud)

但它有时会滞后.是否有其他方法可以禁用当前位置注释的默认标注视图?任何帮助将不胜感激.

cha*_*tur 28

要完成这项工作,需要获得当前位置的参考MKAnnotationView.可以在任何地方获得此引用,但最好在确定用户位置后立即获取该引用.

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation 
{
 MKAnnotationView* annotationView = [mapView viewForAnnotation:userLocation];
annotationView.canShowCallout = NO;

}
Run Code Online (Sandbox Code Playgroud)

或使用以下方法

 - (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views {
MKAnnotationView *aV; 
     for (aV in views) {
            if ([aV.annotation isKindOfClass:[MKUserLocation class]]) {
                MKAnnotationView* annotationView = aV;
                 annotationView.canShowCallout = NO;

            }
    }
Run Code Online (Sandbox Code Playgroud)

如果想在运行时更改canShowCallout属性,那么可以使用以下内容

for (AnnotationClass* annotation in mapView.annotations) 
    {
        if([annotation isKindOfClass:[MKUserLocation class]] )
        {
             MKAnnotationView* annotationView = [mapView viewForAnnotation:annotation];
             annotationView.canShowCallout = NO;
        }
    }
Run Code Online (Sandbox Code Playgroud)

  • 这已停止在iOS7中工作; 解决方法(也支持早期版本)是在方法`mapView:didUpdateUserLocation`中设置`userLocation.title = @"";`将标题设置为空字符串可防止出现标注. (10认同)

Ban*_*cid 6

使用swift更新(基于chatur的答案):

func mapView(mapView: MKMapView!, didAddAnnotationViews views: [MKAnnotationView]!) {

    for view in views {
        if view.annotation.isKindOfClass(MKUserLocation) {
            view.canShowCallout = false
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

注意:使用这个,我不需要任何其他东西来使它工作