2011-10-27 50 views
3

我在我的应用程序中显示了2种注释的地图:位置和位置群集。当我放大某个群集时,群集将展开以显示群集中包含的位置。将这些位置添加到映射中时,其父集群的坐标将存储在注释对象中。我想要做的就是制作一个动画,以便在添加这些位置时,从他们的母集群的位置展开。这里是我的MapView代码:didAddAnnotationViews:纬度/长度坐标到屏幕位置的奇怪翻译

- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views 
{ 
    MKAnnotationView *aV; 
    for (aV in views) 
    { 
     if (([aV.annotation isKindOfClass:[PostLocationAnnotation class]]) && (((PostLocationAnnotation *)aV.annotation).hasParent)) 
     { 
      CLLocationCoordinate2D startCoordinate = ((PostLocationAnnotation *)aV.annotation).parentCoordinate; 

      CGPoint startPoint = [ffMapView convertCoordinate:startCoordinate toPointToView:self.view]; 

      CGRect endFrame = aV.frame; 

      aV.frame = CGRectMake(startPoint.x, startPoint.y, aV.frame.size.width, aV.frame.size.height); 

      [UIView beginAnimations:nil context:NULL]; 
      [UIView setAnimationDuration:1.00]; 
      [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; 
      [aV setFrame:endFrame]; 
      [UIView commitAnimations]; 

      ((PostLocationAnnotation *)aV.annotation).hasParent = NO; 
     } 
    } 
} 

这似乎使地图点从某个位置飞到远远超出视图的焦点。通过调试,我发现endFrame.origin和startPoint的值有很大差异 - 对于一个特定的aV,endFrame.origin以(32657,21781)(两个CGFloats)和startPoint出现为(159.756256,247.213226 )(同样,都是CGFloats)。我假设endFrame.origin是正确的值,因为点在我想要的地方结束,他们只是从远处的某个地方来。我在代码的很多其他部分使用了convertCoordinate:toPointToView:方法,并没有任何问题。我也试着用不同的值乘以startPoint的X和Y值,但是对于startPoint的每个值,单个系数都不成立。任何想法发生了什么?

回答

2

注解视图的框架似乎基于当前的缩放级别,然后偏离屏幕坐标。为了弥补这一点,用annotationVisibleRect.origin抵消了startPoint

此外,当调用convertCoordinate:toPointToView:时,我认为转换为地图视图的框架而不是self.view更安全,因为地图视图的大小可能与容器视图的大小不同。

请尝试以下变化:

CGPoint startPoint = [ffMapView convertCoordinate:startCoordinate 
            toPointToView:ffMapView]; 
//BTW, using the passed mapView parameter instead of referencing the ivar 
//would make the above line easier to re-use. 

CGRect endFrame = aV.frame; 

aV.frame = CGRectMake(startPoint.x + mapView.annotationVisibleRect.origin.x, 
         startPoint.y + mapView.annotationVisibleRect.origin.y, 
         aV.frame.size.width, 
         aV.frame.size.height); 
+0

它的工作原理!谢谢。我一整天都被困在这里 – benwad