将当前位置标注的 canShowCallOut 属性设置为 NO,iPhone。

15

我正在使用自定义的Callout(标题和副标题)来显示当前位置图标。我尝试了以下方法来禁用默认注释,但没有起作用。

- (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;
    }

}

它只能以这种方式工作

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

有时候会卡顿,是否有其他方法可以禁用当前位置标注的默认呼出视图?任何帮助将不胜感激。


你是说默认注释当前位置的蓝点吗? - Ravin
@Ravin,是的。所谓的当前位置注释就是那个蓝色的不断闪烁的圆点。 - chatur
6个回答

28
要做到这一点,需要获取当前位置 MKAnnotationView 的引用。可以在任何地方获取此引用,但最好在确定用户位置后尽快获取它。
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation 
{
 MKAnnotationView* annotationView = [mapView viewForAnnotation:userLocation];
annotationView.canShowCallout = NO;

}

或使用以下方法

 - (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;

            }
    }

如果想要在运行时更改canShowCallout属性,则可以使用以下方法:

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

我发现中间的解决方案不正确,因为你在视图上进行迭代,所以类将是MKUserLocationView而不是MKUserLocation,这似乎是一个私有类。我建议您比较反向:当注释的类不是您自己的注释类(例如MKAnnotation)时,应关闭canShowCallout - gklka
10
在iOS7中,这已经停止工作了;解决方法(也支持早期版本)是在方法mapView:didUpdateUserLocation中设置userLocation.title =@"";。将标题设置为空字符串可防止标注弹出。 - Carlos P
1
这些方法在iOS 7上对我都不起作用。虽然气泡已被禁用,但蓝点仍然可选 - 如果它与另一个地图注释重叠,你可以看得出来。 - joel.d

6

根据chatur的回答,这是使用Swift进行更新的:

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

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

}

注意:使用此功能,我不需要任何其他东西来使其工作


2

Swift:

这对我有用:只需添加此委托方法即可。

func mapView(_ mapView: MKMapView, didAdd views: [MKAnnotationView]) {
    for var view in views {
        view.canShowCallout = false
    }
}

1

这对我在Swift 3.0 / iOS 10上有效。

func mapView(_ mapView: MKMapView, didAdd views: [MKAnnotationView]) {
    (views.filter { $0.isKind(of: MKUserLocation.self) }).first?.canShowCallout = false
}

与其循环遍历所有视图,我只依赖于filter命令,紧接着是可选的调用first,因为应该只有一个位置,然后将值设置为false。

1
// Objective-C

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

// Swift 4.2

func mapView(_ mapView: MKMapView, didUpdate userLocation: MKUserLocation) {
        let userAnnotationView = mapView.view(for: userLocation)
        userAnnotationView?.canShowCallout = false
}

0

编辑

很抱歉误解了你。

我在这个问题上碰了壁。

这是我成功完成它的唯一方法。这种方法的问题是你必须更改UserLocation视图。因此,可能不太用户友好。

无论如何:

 - (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation{
if ([annotation isKindOfClass:[MKUserLocation class]]){

        MKAnnotationView *annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation                                                                           reuseIdentifier:nil];
        annotationView.image = [UIImage imageNamed:@"your-icon.png"];
        annotationView.enabled=NO;
         return annotationView;
    };
     ........}

我得去幼儿园接孩子 :)

祝好运


我刚刚看了你的编辑,并感谢你抽出时间。但是这样做会失去蓝点动画的准确性。如果我找到更好的方法,我会告诉你的。 - chatur
请查看我发布的答案。 - chatur

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接