简体   繁体   English

自动显示MKAnnotation标注

[英]Displaying MKAnnotation callout automatically

Why doesn't this work? 为什么这不起作用?

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

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

    return;
}

Thanks, Z@K! 谢谢,Z @ K!

Because you must select annotation object, not the view that corresponds to it. 因为您必须选择注释对象,而不是与其对应的视图。

I'm 100% not sure, but I think the following should work: 我100%不确定,但我认为以下应该有效:

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

If you store your annotations somewhere it might be better to get annotation object you need directly from that storage. 如果将注释存储在某处,最好直接从该存储获取注释对象。 Note that all these methods have affect only if you try to select annotation that is currently visible on screen. 请注意,仅当您尝试选择当前在屏幕上可见的注记时,所有这些方法才会生效。

If you only have one annotation, this is the easiest way: 如果您只有一个注释,这是最简单的方法:

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

Note: if you have the pin drop animation enabled, this disables it. 注意:如果启用了引脚放置动画,则会禁用它。 Basically, it selects the annotation as soon as its added to the map view so it cuts off the animation. 基本上,它会在添加到地图视图后立即选择注释,以便切断动画。

If you want to wait for the pin drop animation to finish before selecting it, heres a semi-hacky way to make it happen. 如果你想在选择它之前等待引脚下降动画完成,那么这是一种半黑客的方式来实现它。 First, make sure your view controller is set up as the MKMapViewDelegate then: 首先,确保您的视图控制器设置为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];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM