自动显示MKAnnotation标注

时间:2010-08-20 13:50:30

标签: iphone mkannotationview

为什么这不起作用?

- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views {
    if (TRACE_LOG) NSLog(@"%s", __FUNCTION__);

    [mapView selectAnnotation:[views lastObject] animated:YES];

    return;
}

谢谢, ž@ķ!

2 个答案:

答案 0 :(得分:7)

因为您必须选择注释对象,而不是与其对应的视图。

我100%不确定,但我认为以下情况应该有效:

MKAnnotationView* annotationView = (MKAnnotationView*)[views lastObject];
[mapView selectAnnotation:annotationView.annotation animated:YES];

如果将注释存储在某个地方,最好直接从该存储中获取注释对象。请注意,仅当您尝试选择当前在屏幕上可见的注记时,所有这些方法才会生效。

答案 1 :(得分:2)

如果您只有一个注释,这是最简单的方法:

[mapView selectAnnotation:[mapView.annotations objectAtIndex:0] animated:true];

注意:如果启用了引脚放置动画,则会禁用它。基本上,它会在添加到地图视图后立即选择注释,以便切断动画。

如果你想在选择它之前等待引脚掉落动画完成,那就是一种半黑客的方式来实现它。首先,确保您的视图控制器设置为MKMapViewDelegate:

- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views {
    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:.7 target:self selector:@selector(pinDropped) userInfo:nil repeats:NO];

}

- (void) pinDropped {
    [mapView selectAnnotation:[mapView.annotations objectAtIndex:0] animated:true];
}