Ios 需要:如何可靠、一致地选择MKMapView注释
调用MKMapView的setCenterCoordinate:animated:method(无动画)后,我想调用selectAnnotation:animated:(带动画),以便从新居中的图钉中弹出注释 现在,我只需查看mapViewDidFinishLoadingMap:并选择注释。然而,这是有问题的。例如,当不需要加载其他地图数据时,不会调用此方法。在这些情况下,不会选择“我的批注”<代码>:( 很好。我可以在设置中心坐标后立即调用它。啊,但在这种情况下,可能有地图数据要加载(但它还没有完成加载)。我会冒险调用它太快,动画最多也会变得不稳定 因此,如果我理解正确,这不是知道我的坐标是否可见的问题,因为它有可能偏离几乎满屏的距离,必须加载新的地图数据。相反,这是知道是否需要加载新的地图数据,然后相应地采取行动的问题 关于如何实现这一点,或者如何在地图视图位于注释所在的坐标上重新居中后(可靠地)选择注释,有何想法Ios 需要:如何可靠、一致地选择MKMapView注释,ios,mapkit,mkpinannotationview,Ios,Mapkit,Mkpinannotationview,调用MKMapView的setCenterCoordinate:animated:method(无动画)后,我想调用selectAnnotation:animated:(带动画),以便从新居中的图钉中弹出注释 现在,我只需查看mapViewDidFinishLoadingMap:并选择注释。然而,这是有问题的。例如,当不需要加载其他地图数据时,不会调用此方法。在这些情况下,不会选择“我的批注”:( 很好。我可以在设置中心坐标后立即调用它。啊,但在这种情况下,可能有地图数据要加载(但它还没有完成加
感谢您的提示!谢谢!我也遇到了类似的困难,但实际上根本无法使这种方法起作用,更不用说前后不一致了 这是我发现的有用的东西。也许对你也有用:
对我有效的方法是从
mapView:didAddAnnotationViews:
方法调用selectAnnotation:animated:
- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views;
{
[mapView selectAnnotation:[[mapView annotations] lastObject] animated:YES];
}
- (void)selectAnnotation:(id < MKAnnotation >)annotation
{
[self.mapView selectAnnotation:annotation animated:YES];
}
苹果的文档在同一个页面上
请注意,我在地图上只有一个注释,因此就我而言,
[[mapView annotations]lastObject]
没有问题。您的里程数可能会有所不同。我遇到了类似的问题。我使用默认的MKPinAnnotationView和animatesDrop:YES。将注释添加到MKMapView后,我正在执行以下操作:
[mapView selectAnnotation:[mapView.annotations objectAtIndex:1] animated:YES]
在我的程序逻辑中,它应该选择最近的注释。这不起作用。我找出了原因:在这次选择调用时,注释视图不在屏幕上,因为插针掉落动画。所以我所做的只是设置了一个计时器,在一秒钟后选择注释。这是一个破解,但它起作用了。我不确定它是否会我在任何情况下都能工作,比如3G和3Gs。最好能找到合适的回调函数
selectTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self
selector:@selector(selectClosestAnnotation) userInfo:nil
repeats:NO];
- (void)selectClosestAnnotation {
[mapView selectAnnotation:[mapView.annotations objectAtIndex:1]
animated:YES];
}
我遇到了同样的问题,但找到了一个看似可靠合理的解决方案:
-(void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views {
[self performSelector:@selector(selectInitialAnnotation)
withObject:nil afterDelay:0.5];
}
-(void)selectInitialAnnotation {
[self.mapView selectAnnotation:self.initialAnnotation animated:YES];
}
selectAnnotation:animated:
。与比较:
如果指定的注释不在屏幕上,因此不在屏幕上
如果具有关联的注释视图,则此方法无效
与使用固定超时相比,更一致的方法是侦听
regionDidChange
回调。将地图的中心坐标设置为所需的标注,然后在调用regionDidChange方法时,选择中心的标注,以打开标注
这是我在5针之间随机运行的东西的一点
首先,转到注释的中心坐标。假设注释对象名为thePin
- (void)someMethod {
[map setCenterCoordinate:thePin.coordinate animated:YES];
}
然后在regionDidChange
方法中,选择此注释
- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated {
[map selectAnnotation:thePin animated:YES];
}
我发现我针对我的问题看到的所有解决方案都存在一些问题(我想在从ViewDidDisplay添加注释时选择注释):
mapView:didAddAnnotationViews:
delegate方法
这对我不起作用,因为如果指定的注释不在屏幕上,因此没有关联的注释视图,则此方法无效mapViewDidFinishLoadingMap:
delegate方法
这对我也不起作用,因为映射现在被缓存,所以该方法只被调用一次,第一次mapView:didAddAnnotationViews:
方法。在添加注释之前,我只调用自己的selectAnnotation:
方法,但有一点延迟:
[self.mapView addAnnotation:ann];
[self performSelector:@selector(selectAnnotation:) withObject:ann afterDelay:0.5];
这就是我在selectAnnotation:
方法中所做的:
- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views;
{
[mapView selectAnnotation:[[mapView annotations] lastObject] animated:YES];
}
- (void)selectAnnotation:(id < MKAnnotation >)annotation
{
[self.mapView selectAnnotation:annotation animated:YES];
}
-(void)选择注解:(id)注解
{
[self.mapView选择注释:注释动画:是];
}
在两台iPhone3GS(iOS 5和iOS 6)和一台iPhone5上进行了测试。仅供参考,这是文档所说的 如果指定的注释不在屏幕上,因此不在屏幕上 如果具有关联的注释视图,则此方法无效 因此,如果我想调用
setCenterCoordinate:animated:
或setRegion:animated:
,然后我想通过调用selectAnnotation:animated:
来选择注释,注释将不会被选中,并且标注也不会出现,原因与上述文档中提到的完全相同,因此这种方式非常好要有类似于,setCenterCoordinate:animated:ComletionBlock
但它不在那里..!我的工作方式如下:
[UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionBeginFromCurrentState|UIViewAnimationOptionCurveEaseInOut animations:^{
[self.mapView setCenterCoordinate:location.coordinate animated:YES];
} completion:^(BOOL finished) {
[self.mapView selectAnnotation:location animated:YES];
}];
这将为您提供一个完成块,您可以使用它来选择批注。您好!非常感谢您的回复。当我读到这篇文章时,解决方案似乎是调用selectAnnotation:animated:…只有我已经在尝试了(请参阅上面的描述)它不起作用。也许我遗漏了一些额外的东西?哦!我的错。你说的是你发布的部分。:)
- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated {
[map selectAnnotation:thePin animated:YES];
}