2010-12-21 68 views
1

使用以下教程中的代码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: 

http://developer.apple.com/library/ios/#documentation/MapKit/Reference/MKMapViewDelegate_Protocol/MKMapViewDelegate/MKMapViewDelegate.html

回答

3

而不是使用.frame.origin,请尝试获取MKAnnotationcoordinate属性。使用此coordinate,您可以使用MKMapViewconvertCoordinate:toPointToView:来获取注释的来源。

希望这会有所帮助!

0

Apple's sample app WeatherMap定制注释和说明如何将它们的位置。代码很简单,建议。

+0

我已经看到了WeatherMap应用程序,它自定义了注释本身而不是它的标注泡泡。我试图添加一个UIView来模拟泡泡。引脚本身不会改变。感谢您花时间回答。 – 2010-12-21 21:23:02

+0

啊,伟大的一点,对此感到遗憾。更糟的是,在@ donkim的回答中,我意识到我知道这一点,并正在使用它在我现在正在进行的一个项目中。 D'哦! – 2010-12-21 22:47:05