【问题标题】:Check if an MKAnnotation is selected on a map?检查是否在地图上选择了 MKAnnotation?
【发布时间】:2013-05-06 17:03:56
【问题描述】:

我有一个非常简单的问题:如何检查是否在地图上选择了 MKAnnotation?

我看不到已选择的类似 (GET) 属性。

我希望解决方案不是通过触发选择/取消选择事件并将其结果存储在属性中并在需要时检查它们。一定有更直接的。

非常感谢!

【问题讨论】:

    标签: ios mapkit mkannotationview


    【解决方案1】:

    查看-[MKMapView selectedAnnotations]

    【讨论】:

    • 太好了,这就是我要找的东西
    【解决方案2】:

    利用MKMapView的委托方法didSelectAnnotationView:使用可以得到事件MKAnnotation Selected

    -(void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view {
        // Annotation is your custom class that holds information about the annotation
        if ([view.annotation isKindOfClass:[Annotation class]]) {
            Annotation *annot = view.annotation;
            NSInteger index = [self.arrayOfAnnotations indexOfObject:annot];
        }
    }
    

    希望对你有帮助。

    【讨论】:

    • 我知道,谢谢,但我真的很震惊,没有一个简单的 isSelected 属性用于注释...
    • 可能有一个简单的 Selected 属性是您在 MKAnnotationView 上添加一个自定义按钮,否则您需要借助此委托方法。
    【解决方案3】:

    只是对此的更新 -- 在 iOS 4 中,MKMapViewDelegate 方法可用于跟踪注释选择和取消选择:

    - (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
    - (void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKAnnotationView *)view
    

    您可以使用 Observer 进行 Selected annotation 事件:

    [pin addObserver:self
          forKeyPath:@"selected" 
             options:NSKeyValueObservingOptionNew
             context:@"ANSELECTED"];
    - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{
    
        NSString *action = (NSString*)context;
    
        if([action isEqualToString:@"ANSELECTED"]){
    
            BOOL annotationAppeared = [[change valueForKey:@"new"] boolValue];
            if (annotationAppeared) {
                // clicked on an Annotation
            }
            else {
                // Annotation disselected
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-01-07
      • 1970-01-01
      • 2014-03-28
      • 2012-12-28
      • 2021-07-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多