2013-05-06 45 views
4

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

我看不到所选的像(GET)属性。

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

非常感谢!

回答

6

结账-[MKMapView selectedAnnotations]

+0

太好了,那就是我在找的东西 – Tom 2013-05-06 18:17:44

6

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

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

希望它会帮助你。

+0

我知道,谢谢,但我真的很震惊也没有一个简单的IsSelected属性为注释... – Tom 2013-05-06 17:15:26

+1

有可能是简单的selected属性添加到MKAnnotationView自定义按钮,否则,你需要采取的帮助这个委托方法。 – 2013-05-06 17:18:00

1

只是一个更新到这一点 - 在iOS 4的存在是可以用来跟踪注释选择和取消选择MKMapViewDelegate方法:

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

您可以使用一个观测器的选择的注释事件:

[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 
     } 
    } 
} 
相关问题