【问题标题】:setting canShowCallOut = NO for current location annotation, iPhone设置 canShowCallOut = NO 用于当前位置注释,iPhone
【发布时间】:2012-01-16 14:07:31
【问题描述】:

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

- (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,是的。当前位置注释是指蓝色脉动点。

标签: ios objective-c mkannotation currentlocation


【解决方案1】:

要完成这项工作,需要获取当前位置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,这似乎是一个私有类。我建议你比较一下:当注解的类不是你自己注解的类时,你应该关闭canShowCallout(例如MKAnnotation)。
  • 这在 iOS7 中已停止工作;解决方法(也支持早期版本)是在方法mapView:didUpdateUserLocation 中设置userLocation.title = @""; 将标题设置为空字符串可防止标注出现。
  • 这些方法都不适用于 iOS 7。标注被禁用,但蓝点仍然可选择 - 您可以判断它是否与另一个地图注释重叠。
【解决方案2】:

对此的更新,使用 swift(基于 chatur 的回答):

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

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

}

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

【讨论】:

    【解决方案3】:

    斯威夫特:

    这对我有用:只需添加这个委托方法。

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

    【讨论】:

      【解决方案4】:

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

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

      我没有遍历所有视图,而是仅依赖filter 命令,然后是可选调用,因为应该只有一个位置,然后将值设置为 false。

      【讨论】:

        【解决方案5】:
        // 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
        }
        

        【讨论】:

          【解决方案6】:

          编辑

          很抱歉弄错了。

          我对此一头雾水。

          这是我成功的唯一方法。这种方法的问题是您必须更改 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;
              };
               ........}
          

          必须跑去幼儿园带我的孩子:)

          祝你好运

          【讨论】:

          • 我刚刚阅读了您的编辑并感谢您的宝贵时间。但是这样我会失去蓝点的准确性动画。如果我找到更好的方法,我会告诉你的。
          • 请看我发的答案。
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-09-07
          • 2014-08-29
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多