2012-03-20 72 views
0

我想要一個IBAction中心我的mapview的地區的註釋。註釋添加到的MapView這樣的:如何檢索地圖註記座標?

myAnnotation *pinLoc=[[myAnnotation alloc] init]; 
pinLoc.coordinate = mapview.centerCoordinate; 
pinLoc.title = @"Title"; 
pinLoc.subtitle = @"subtitle"; 
[mapview addAnnotation:pinLoc]; 

我想中檢索,並以某種方式移動該地區的中心是這樣的:

- (IBAction)findPin:(id)sender { 

    MKCoordinateRegion region = { {0.0, 0.0 }, { 0.0, 0.0 } }; 

    region.center.latitude = pinLoc.coordinate.latitude; 
    region.center.longitude = pinloc.coordinate.longitude; 
    region.span.latitudeDelta = 0.001f; 
    region.span.longitudeDelta = 0.001f; 
    [mapview setRegion:region animated:YES]; 
    [mapview setDelegate:self]; 
} 

我也在尋找一種方式來告訴我們,如果該針在多邊形內。我已經在使用用戶位置工作了,但同樣,我需要檢索引腳的座標以使其與引腳一起工作。

回答

1

最簡單的方法是在添加註釋時存儲對註釋的引用。

例如,你可以聲明稱爲myAnnotationpinToRemember並添加註釋後保留的財產,這樣做是爲了保存以後:

[mapview addAnnotation:pinLoc]; 
self.pinToRemember = pinLoc; 

然後在findPin:,你會怎麼做:

if (pinToRemember == nil) 
    return; 

region.center = pinToRemember.coordinate; 


或者,您可以搜索地圖視圖的annotations數組屬性,然後通過查找您感興趣的註釋其title或其他一些自定義屬性。這可以通過使用簡單的for循環完成。


你說你有「辦法告訴引腳是一個多邊形內」的工作,但這個答案也可能是有用的:
How to determine if an annotation is inside of MKPolygonView (iOS)

+0

有時候,我看不出是正確的答案在我面前,謝謝安娜。您在StackOverflow上的存在爲我在早期的iOS開發職業生涯中爲我節省了無數次。我實際上習慣使用你的代碼來確定一個點是否在一個多邊形內! – 2012-03-20 09:23:20