【问题标题】:Strange translations of lat/long coordinates to screen position纬度/经度坐标到屏幕位置的奇怪转换
【发布时间】:2011-10-27 11:25:05
【问题描述】:

我的应用上有一张地图,其中显示了 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 的每个值。知道发生了什么吗?

【问题讨论】:

    标签: iphone mkmapview mapkit mkannotation


    【解决方案1】:

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

    另外,当调用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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-04-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多