30

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

现在,我只关注mapViewDidFinishLoadingMap:然后选择注释。然而,这是有问题的。例如,当不需要加载额外的地图数据时,不会调用此方法。在这些情况下,我的注释未被选中。:(

很好。我可以在设置中心坐标后立即调用它。啊,但在那种情况下,可能有地图数据要加载(但它还没有完成加载)。我会冒着过早调用它的风险,因为动画充其量会变得参差不齐。

因此,如果我理解正确,就不必知道我的坐标是否可见,因为可能会偏离几乎一屏的距离并且必须加载新的地图数据。相反,这是一个知道是否需要加载新的地图数据,然后采取相应行动的问题。

关于如何实现这一点的任何想法,或者在将地图视图重新居中于注释所在的坐标上之后如何(可靠地)选择注释?

线索赞赏 - 谢谢!

4

7 回答 7

36

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

  1. 实现委托方法 mapView:didAddAnnotationViews:。当我尝试直接在委托方法中选择注释时,标注与引脚一起下降!这看起来很奇怪,所以我添加了半秒的轻微延迟。

    -(void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views {
        [self performSelector:@selector(selectInitialAnnotation)
              withObject:nil afterDelay:0.5];
    }
    
  2. 如您所愿选择初始注释,但调用 selectAnnotation:animated;

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

在某些情况下似乎selectAnnotation:animated:没有调用。与MKMapView 文档比较:

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

于 2010-02-16T00:06:42.900 回答
7

比使用固定超时更一致的方法是监听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];
}
于 2010-11-12T05:23:13.800 回答
5

仅供参考,这就是文档所说的,

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

因此,如果我想调用setCenterCoordinate:animated:orsetRegion: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];
    }];

这将为您提供一个完成块,您可以使用它来选择注释。

于 2013-08-14T05:43:10.747 回答
4

对我有用的是selectAnnotation:animated:mapView:didAddAnnotationViews:方法中调用:

- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views;
{
    [mapView selectAnnotation:[[mapView annotations] lastObject] animated:YES];
}

Apple 文档在这里也一样。

请注意,我在地图上只有一个注释,因此[[mapView annotations] lastObject]对我来说很好。你的旅费可能会改变。

于 2009-11-02T18:44:18.460 回答
2

I was having a similar problem. I was using the default MKPinAnnotationView with animatesDrop:YES. After I added the annotations to the MKMapView, I was doing this:

[mapView selectAnnotation:[mapView.annotations objectAtIndex:1] animated:YES]

which in the logic of my program, should select the nearest annotation. This wasn't working. I figured out the reason: the annotation view was not on the screen at the time of this select call, because of the pin drop animation. So all I did was set a timer to select the annotation a second later. It's a hack, but it works. I'm not sure if it'll work in every situation though, for instance on a 3G vs. 3Gs. It'd be better to figure out the right callback function to put it in.

selectTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self
               selector:@selector(selectClosestAnnotation) userInfo:nil
               repeats:NO];

- (void)selectClosestAnnotation {
    [mapView selectAnnotation:[mapView.annotations objectAtIndex:1]
             animated:YES];
}
于 2009-12-15T17:49:23.100 回答
1

我发现所有针对我的问题的解决方案都有几个问题(我想在从 viewDidAppear 添加注释时选择一个注释):

  1. 使用mapView:didAddAnnotationViews:委托方法。

    这对我不起作用,因为如果指定的注释不在屏幕上,因此没有关联的注释视图,则此方法无效。

  2. 使用mapViewDidFinishLoadingMap:委托方法。

    这对我也不起作用,因为地图现在被缓存了,所以该方法只被调用一次,第一次。

解决方案:

约翰布莱克本非常接近,但没有mapView:didAddAnnotationViews:方法。在添加注释之前,我只是调用自己的selectAnnotation:方法,但有延迟:

[self.mapView addAnnotation:ann];
[self performSelector:@selector(selectAnnotation:) withObject:ann afterDelay:0.5];

这就是我在我的selectAnnotation:方法中所做的:

- (void)selectAnnotation:(id < MKAnnotation >)annotation
{
   [self.mapView selectAnnotation:annotation animated:YES];
}

在两部 iPhone 3GS(iOS 5 和 6)和一部 iPhone 5 上测试。

于 2012-10-23T08:31:08.510 回答
0

我遇到了类似的困难,但实际上根本无法使这种方法起作用,更不用说不一致了。

这是我发现的工作。也许它也对你有用: 如何在不触摸 pin 的情况下触发 MKAnnotationView 的标注视图?

于 2009-08-24T02:53:40.583 回答