获取iOS中注释标注的位置

发布于 2024-10-19 22:52:18 字数 1737 浏览 5 评论 0原文

情况如下:我正在使用 MapKit 在地图上显示多个位置。我有代码来计算允许地图上所有点适合窗口的边界。我最近刚刚添加了代码,以允许出现最近位置的标注气泡(正确的术语?)。不幸的是,标注气泡不适合窗口的边界。理想情况下,我希望调整缩放以适应所有注释以及最近位置的标注气泡。关于如何执行此操作有什么建议吗?这是我的缩放方法:

-(void)zoomToFitMapAnnotations:(MKMapView*)mv
{   
    if([mv.annotations count] == 0)
            return;

    CLLocationCoordinate2D topLeftCoord;
    topLeftCoord.latitude = -90;
    topLeftCoord.longitude = 180;

    CLLocationCoordinate2D bottomRightCoord;
    bottomRightCoord.latitude = 90;
    bottomRightCoord.longitude = -180;

    for(WaybackAnnotation *annotation in [mv annotations])
    {
        if ([annotation isKindOfClass:[WaybackAnnotation class]])
        {
            topLeftCoord.longitude = fmin(topLeftCoord.longitude, annotation.coordinate.longitude);
            topLeftCoord.latitude = fmax(topLeftCoord.latitude, annotation.coordinate.latitude);

            bottomRightCoord.longitude = fmax(bottomRightCoord.longitude, annotation.coordinate.longitude);
            bottomRightCoord.latitude = fmin(bottomRightCoord.latitude, annotation.coordinate.latitude);
        }
    }

    MKCoordinateRegion region;
    region.center.latitude = topLeftCoord.latitude - (topLeftCoord.latitude - bottomRightCoord.latitude) * 0.5;
    region.center.longitude = topLeftCoord.longitude + (bottomRightCoord.longitude - topLeftCoord.longitude) * 0.5;
    region.span.latitudeDelta = fabs(topLeftCoord.latitude - bottomRightCoord.latitude) * 1.1; // Add a little extra space on the sides
    region.span.longitudeDelta = fabs(bottomRightCoord.longitude - topLeftCoord.longitude) * 1.1; // Add a little extra space on the sides

    region = [mv regionThatFits:region];
    [mv setRegion:region animated:YES];
}

提前致谢! 詹姆斯

here's the situation: I am using MapKit to display a number of locations on a map. I have code to calculate the bounds that allow all the points on the map to fit the window. I just recently added code to allow the closest location's callout bubble (proper term?) to appear. Unfortunately, the callout bubble does not fit in the bounds of the window. Ideally, I'd like the zoom to adjust to fit all the annotations as well as the closest location's callout bubble. Any suggestions on how to do this? Here is my zoom method:

-(void)zoomToFitMapAnnotations:(MKMapView*)mv
{   
    if([mv.annotations count] == 0)
            return;

    CLLocationCoordinate2D topLeftCoord;
    topLeftCoord.latitude = -90;
    topLeftCoord.longitude = 180;

    CLLocationCoordinate2D bottomRightCoord;
    bottomRightCoord.latitude = 90;
    bottomRightCoord.longitude = -180;

    for(WaybackAnnotation *annotation in [mv annotations])
    {
        if ([annotation isKindOfClass:[WaybackAnnotation class]])
        {
            topLeftCoord.longitude = fmin(topLeftCoord.longitude, annotation.coordinate.longitude);
            topLeftCoord.latitude = fmax(topLeftCoord.latitude, annotation.coordinate.latitude);

            bottomRightCoord.longitude = fmax(bottomRightCoord.longitude, annotation.coordinate.longitude);
            bottomRightCoord.latitude = fmin(bottomRightCoord.latitude, annotation.coordinate.latitude);
        }
    }

    MKCoordinateRegion region;
    region.center.latitude = topLeftCoord.latitude - (topLeftCoord.latitude - bottomRightCoord.latitude) * 0.5;
    region.center.longitude = topLeftCoord.longitude + (bottomRightCoord.longitude - topLeftCoord.longitude) * 0.5;
    region.span.latitudeDelta = fabs(topLeftCoord.latitude - bottomRightCoord.latitude) * 1.1; // Add a little extra space on the sides
    region.span.longitudeDelta = fabs(bottomRightCoord.longitude - topLeftCoord.longitude) * 1.1; // Add a little extra space on the sides

    region = [mv regionThatFits:region];
    [mv setRegion:region animated:YES];
}

Thanks in advance!
James

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

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

发布评论

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

评论(1

桃气十足 2024-10-26 22:52:18

由于气泡始终显示在图钉上方,因此您需要在顶部添加一些额外的填充。您知道填充的高度(以屏幕像素为单位)。使用 MKMapView 的转换方法(-convertRect:toRegionFromView: 等)将它们转换为地理坐标。

Since the bubble is always displayed above the pin, you need some extra padding at the top. You know the height of the padding in screen pixels. Use MKMapViews conversion methods (-convertRect:toRegionFromView: and friends) to convert these into geo coordinates.

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