使用以下教程中的代碼http://www.zenbrains.com/blog/en/2010/05/detectar-cuando-se-selecciona-una-anotacion-mkannotation-en-mapa-mkmapview/,我能夠爲每個MKAnnotation添加一個觀察者並接收選定/取消選定狀態的通知。iOS MapKit:選定的MKAnnotation座標
我試圖在選擇註釋頂部添加一個UIView來顯示有關位置的相關信息。該信息不能在允許的標題/小標題的2行中傳遞,以便標註標註。
- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context {
Annotation *a = (Annotation *)object;
// Alternatively attempted using:
//Annotation *a = (Annotation *)[mapView.selectedAnnotations objectAtIndex:0];
NSString *action = (NSString *)context;
if ([action isEqualToString:ANNOTATION_SELECTED_DESELECTED]) {
BOOL annotationSelected = [[change valueForKey:@"new"] boolValue];
if (annotationSelected) {
// Actions when annotation selected
CGPoint origin = a.frame.origin;
NSLog(@"origin (%f, %f) ", origin.x, origin.y);
// Test
UIView *v = [[UIView alloc] init];
[v setBackgroundColor:[UIColor orangeColor]];
[v setFrame:CGRectMake(origin.x, origin.y , 300, 300)];
[self.view addSubview:v];
[v release];
}else {
// Accions when annotation deselected
}
}
}
結果使用Annotation *a = (Annotation *)object
origin (154373.000000, 197135.000000)
origin (154394.000000, 197152.000000)
origin (154445.000000, 197011.000000)
結果使用Annotation *a = (Annotation *)[mapView.selectedAnnotations objectAtIndex:0];
origin (0.000000, 0.000000)
origin (0.000000, 0.000000)
origin (0.000000, 0.000000)
的數字是大的。它們與視圖無關(1024 x 768)。我相信他們是相對於整個地圖。我如何能夠檢測相對於整個視圖的確切座標,以便適當地定位我的視圖?
注:
剛發現這兩種方式這或許可以完成同樣的事情,上面的代碼在一個更簡單的實現。
選擇註釋視圖
– mapView:didSelectAnnotationView:
– mapView:didDeselectAnnotationView:
我已經看到了WeatherMap應用程序,它自定義了註釋本身而不是它的標註泡泡。我試圖添加一個UIView來模擬泡泡。引腳本身不會改變。感謝您花時間回答。 – 2010-12-21 21:23:02
啊,偉大的一點,對此感到遺憾。更糟的是,在@ donkim的回答中,我意識到我知道這一點,並正在使用它在我現在正在進行的一個項目中。 D'哦! – 2010-12-21 22:47:05