纬度/经度坐标到屏幕位置的奇怪转换

发布于 2024-12-12 15:40:50 字数 1552 浏览 4 评论 0原文

我的应用程序上有一张地图,显示两种注释:位置和位置集群。当我放大集群时,集群会展开以显示集群中包含的位置。当这些位置添加到地图时,其父簇的坐标将存储在注释对象中。我想要做的是制作一个动画,以便在添加这些位置时,它们从父集群的位置展开。这是我的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 是正确的值,因为这些点最终到达我想要的位置,它们只是来自很远的地方。我在代码的许多其他部分使用了convertCooperative:toPointToView:方法,没有出现任何问题。我还尝试将 startPoint 的 X 和 Y 值乘以各种值,但单个系数并不适用于 startPoint 的每个值。知道发生了什么事吗?

I have a map on my app that displays 2 kinds of annotations: locations and clusters of locations. When I zoom in on the a cluster the cluster is expanded to show the locations contained within the cluster. When these locations are added to the map the coordinate of their parent cluster is stored in the annotation object. What I want to do is have an animation so that, when these locations are added, they spread out from their parent cluster's location. Here is my code for 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;
        }
    }
}

This seems to make the map points fly in from some location far outside the focus of the view. Through debugging I have found that there is a massive discrepancy between the values for endFrame.origin and startPoint - for one particular aV, endFrame.origin comes up as (32657,21781) (both CGFloats) and startPoint comes up as (159.756256,247.213226) (again, both CGFloats). I am assuming that the endFrame.origin is the correct value as the points are ending up where I want them, they're just coming from somewhere far away. I've used the convertCoordinate:toPointToView: method in a lot of other parts of my code and had no problems. I've also tried multiplying the X and Y values of startPoint by various values but a single coefficient doesn't hold for every value of startPoint. Any idea what's going on?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

空宴 2024-12-19 15:40:51

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

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

The annotation view's frame seems to be based on the current zoom level and then offset from the screen coordinates. To compensate for this, offset the startPoint by the annotationVisibleRect.origin.

Also, when calling convertCoordinate:toPointToView:, I think it's safer to convert to the map view's frame instead of self.view since the map view might be a different size than the container view.

Try the following changes:

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文