3

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

4

2 回答 2

6

当调用该方法时——因为您的 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 <MKAnnotation>)annotation.

于 2012-03-24T01:50:24.730 回答
0

这是与 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 或其他。有人可能对此有解释,但直到现在我都没有想到。希望这可以帮助。

于 2012-03-27T03:57:13.520 回答