2015-06-17 40 views
2

当我执行以下代码片段时,地图会正确缩放以包含所有注释,但标注部分偏离屏幕。解决这个问题最优雅的方法是什么?当我缩放我的地图视图以显示我的注释时,所选注释部分偏离屏幕

func mapView(mapView: MKMapView!, didAddAnnotationViews views: [AnyObject]!) { 
// once annotationView is added to the map, get the last one added unless it is the user's location: 
    if let annotationView = views.last as? MKAnnotationView where !(annotationView.annotation is MKUserLocation) { 
    // show callout programmatically: 
    mapView.selectAnnotation(annotationView.annotation, animated: false) 
    // zoom to all annotations on the map: 
    mapView.showAnnotations(mapView.annotations, animated: true) 
} 

回答

0

如果我理解正确,那么我有同样的问题。我不确定这是否是最“优雅”的解决方案,但是简单的延迟对我来说是个诀窍。你可以尝试类似的东西,像这样:

//Assuming this is how you center 
    mapView.setCenterCoordinate(annotation.coordinate, animated: true) 

    //Delaying the showing of annotation because otherwise the popover appears at "old" location before zoom finishes 
    let delayTime = dispatch_time(DISPATCH_TIME_NOW,Int64(0.75 * Double(NSEC_PER_SEC))) 
    dispatch_after(delayTime, dispatch_get_main_queue()) { 

    mapView.selectAnnotation(annotationView.annotation, animated: false) 
    } 

请注意,我使用0.75秒的任意时间;这可能比你需要的更多或更少 - 如果你真的想要(我是懒惰的),你可以使这个数字取决于你必须放大的距离;或者,更加聪明,并找出如何及时获取缩放。

享受!

+0

谢谢。我最终用UIEdgeInsets进行了一个自定义函数。我想任何一种方法都有点离合。 – Zoef

0

我有这个确切的问题,我试过@Jona方法的延迟,但我发现,在某些设备上它的作品和其他人没有。这一切都归功于设备设置/性能,所以使用延迟并不是最好的解决方案。

我的问题是,我试图移动/缩放地图(动画),然后打开我想要显示的特定引脚上的标注视图(带动画)。这样做会导致性能不佳,标注部分在屏幕外。

我决定使用UIView animateWithDuration方法,这个方法有它自己的完成块,因此不需要延迟。注意:使用UIView animateWithDuration方法时,可以将animated方法设置为NO(或Swift中的false)。由于UIView动画块将为我们照顾动画。

-(void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views { 

    // Loop through the pins on the 
    // map and find the added pin. 

    for (MKAnnotationView *annotationView in views) { 

     // If the current pin is the 
     // added pin zoom in on it. 

     if (annotationView.annotation != mapView.userLocation) { 

      // Set the map region to be 
      // close to the added pin. 
      MKCoordinateSpan span = MKCoordinateSpanMake(0.15, 0.15); 
      MKCoordinateRegion region = MKCoordinateRegionMake(annotationView.annotation.coordinate, span); 
      [mapView regionThatFits:region]; 

      // Only perform the annotation popup 
      // when the animation has been completed. 
      [UIView animateWithDuration:0.4 delay:0.0 options:(UIViewAnimationOptionAllowUserInteraction | UIViewAnimationOptionCurveEaseOut) animations:^{ 

       // Set the map region. 
       [mapView setRegion:region]; 

      } completion:^(BOOL finished) { 

       // Force open the annotation popup. 
       [usersMap selectAnnotation:annotationView.annotation animated:YES]; 
      }]; 
     } 
    } 
} 
相关问题