如何保存选定的MKAnnotation?

我有一个具有mapview的应用程序,它在地图上显示20个引脚(来自数组)。 当用户点击它时,它可以显示带有右侧附件按钮的气泡。 这就是我的问题:我怎么知道哪个引脚被按下了? 我听说过mapView:didSelectAnnotationView方法,但我真的不明白你如何得到pin / callout索引,以便我可以在我的Array的同一索引处获取对象的信息? 谢谢你的帮助!

当调用该方法时 – 因为您的viewController类已采用MKMapViewDelegate ,您可以在数组上调用-indexOfObject并获取该引脚的索引(注释)。 这是假设您的数组包含那种注记类的对象。

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

如果您需要更多解释,我们需要知道您是如何添加这些引脚的,即实现- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id )annotation

这是Canopus的类似解决方案。

 - (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view { if ([view.annotation isKindOfClass:[MyAnnotation class]]) { NSInteger index = [mapView.annotations indexOfObject:view.annotation]; NSLog(@"%d",index); } 

注意:我手动向地图添加注释,然后查看各自的索引。 它们不会按照添加到mapView的顺序编制索引。 也就是说,仅仅因为你添加了一个注释作为第四个注释,它可能有一个索引为1或3或其他。 有人可能会对此有一个解释,但直到现在我还是没有找到。 希望这可以帮助。