想要的:如何可靠,一致地选择MKMapView批注
收藏

调用MKMapView的setCenterCoordinate:animated:方法(不带动画)后,我想调用selectAnnotation:animated :(带动画),以便从新居中的图钉弹出注释。

For now, I simply watch for mapViewDidFinishLoadingMap: and then select the annotation. However, this is problematic. For instance, this method isn't called when there's no need to load additional map data. In those cases, my annotation isn't selected. :(

很好。我可以在设置中心坐标后立即调用此方法。嗯,但是在这种情况下,可能有地图数据要加载(但尚未完成加载)。我冒着过早调用它的风险,动画充其量只能说是斑点。

因此,如果我理解正确,那么就不必知道坐标是否可见,因为有可能会迷离几乎一屏的距离并必须加载新的地图数据。而是要知道是否需要加载新的地图数据,然后采取相应措施。

关于如何完成此操作的任何想法,或者在将地图视图重新定位在该注释所在的坐标上之后,如何(可靠地)选择一个注释?

线索感激-谢谢!

最佳答案

我遇到了同样的问题,但发现了一个似乎可靠且合理的解决方案:

  1. Implement the delegate method mapView:didAddAnnotationViews:. When I tried selecting the annotation directly within the delegate method, the callout dropped with the pin! That looked odd, so I add a slight delay of a half-second.

    -(void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views {
        [self performSelector:@selector(selectInitialAnnotation)
              withObject:nil afterDelay:0.5];
    }
    
  2. Select the initial annotation as you'd expect, but calling selectAnnotation:animated;

    -(void)selectInitialAnnotation {
        [self.mapView selectAnnotation:self.initialAnnotation animated:YES];
    }
    

It seems that selectAnnotation:animated: is not called under some conditions. Compare with MKMapView docs:

如果指定的注释不在屏幕上,因此不会   具有关联的注释视图,此方法无效。

    公众号
    关注公众号订阅更多技术干货!