2012-03-24 17 views
3

我有一個具有mapview的應用程序,它顯示地圖上的20個引腳(來自陣列)。當用戶點擊它時,它可以用正確的附件按鈕顯示氣泡。 這裏來我的問題:我怎麼知道哪個引腳被按下? 我聽說過有關mapView:didSelectAnnotationView方法,但我不太瞭解如何獲得pin/callout索引,以便我可以在我的Array的相同索引處獲取對象的信息?謝謝你的幫助!如何保存所選的MKAnnotation?

回答

6

當該方法被調用 - 因爲你的viewController類採用了MKMapViewDelegate,您可以撥打陣列上​​並獲得該引腳(註釋)的索引。這是假設你的數組擁有這種註解類的對象。

- (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

+0

哇,這正是我所需要的!謝謝! – RobRoyRyan 2012-03-28 18:38:03

+0

它應該是'didDeselectAnnotationView'嗎?或didselectedAnnotationView? – RobRoyRyan 2012-03-28 21:47:27

+0

@RobRoyRyan實際上都有,但我的意思是didSelectAnnotationView。當你第一次選擇一個註釋時,didSelectAnnotationView被調用。當選擇註釋並點擊地圖上的其他地方時,didDeselectAnnotationView會被調用。當選擇註釋並選擇另一個註釋時,先調用DeSelectAnnotationView,然後調用didSelectAnnotationView。 – Canopus 2012-03-28 22:06:38

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或其他任何內容。有人可能會對此有一個解釋,但直到現在它一直沒有。希望這可以幫助。

相關問題