iOS - 两个坐标的中心位置

发布于 2024-11-29 08:19:59 字数 131 浏览 3 评论 0原文

我想从 Google 给出的东北点和西南点创建一个 MKCooperativeRegion (缩放到地图上的好区域)。为此,我需要计算这两个坐标之间的中心坐标。有什么线索吗?我可以做简单的数学,但我会遇到赤道问题......

谢谢!

I would like to create a MKCoordinateRegion (to zoom to the good region on the map) from the northeast and southwest points given by Google. For that I need to compute the coordinate of the center between these two coordinates. Any clue? I could do simple math but I will have problems with the equator...

Thanks!!!

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

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

发布评论

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

评论(3

离鸿 2024-12-06 08:19:59

假设您指的是反子午线而不是赤道(虽然所有这些都在平坦的地图上工作并且应该足以满足您的目的,但它完全是在球体上。请参阅底部的注释)。

在其他情况下,我所做的就是从任一点开始,如果下一个点向东超过 180 度,我会将其转换为向西小于 180 度,如下所示

if(pointa.lon - pointb .长> 180)
点b.lon += 360:
else if (pointa.lon - pointb.lon < -180)
pointb.lon -= 360

此时 pointb.lon 可能是一个无效的经度,例如 190,但您至少可以计算出 pointa 和点 b 之间的中点,因为它们将采用连续的比例,因此您可能有点 175和 190。然后只需获取它们之间的中点 182.5,然后将其转换以确保其在通常的限制内,您将得到 -177.5 作为两点之间的纬度。计算纬度很容易。

当然,在球体上这是错误的,因为 (-180,89) 和 (180,89) 之间的中点是 (0*,90) 而不是 (0,89)。
* = 可以是任何东西

Assuming you mean anti-meridian and not the equator then here goes (While all this works on a flattened map and should be good enough for your purpose, it's completely bung on a sphere. see note at the bottom).

What I've done in other cases is start at either point, and if the next point is more than 180 degrees to the east, I convert it so that it is less than 180 to the west like so

if(pointa.lon - pointb.lon > 180)
pointb.lon += 360:
else if (pointa.lon - pointb.lon < -180)
pointb.lon -= 360

At this time pointb.lon might be an invalid longitude like 190 but you can at least work out the mid-point between pointa and point b because they will be on a continuous scale, so you might have points 175 and 190. Then just get the mid-point between them as 182.5, then convert that to make sure it is within the usual limits and you get -177.5 as the latitude between the two points. Working out the latitude is easy.

Of course on a sphere this is wrong because the midpoint between (-180,89) and (180,89) is (0*,90) not (0,89).
* = could be anything

暖风昔人 2024-12-06 08:19:59

另外,你不能只用定义的角来制作zoomToRect吗?它可以节省您进行此计算,然后进行下一个计算,即计算出以该点为中心以包含您所知道的两个角时需要达到的缩放级别。由于地图应用程序似乎不会滚动到反子午线,我认为 MKMapview 也不能滚动,因此您的矩形必须将东北坐标作为右上角,将西南坐标作为左下角。

Also, couldn't you just zoomToRect made with the defined corners? It'd save you doing this calculation and then next one which would be to work out what zoom level you need to be at when centered on that point to include the two corners you know about. Since the Maps app doesn't appear to scroll over the anti-meridian I assume MKMapview can't either so your rectangle is going to have to have the northeast coord as the top right and the southwest as the bottom left.

月亮坠入山谷 2024-12-06 08:19:59

这篇文章包含缩放地图视图以适应其所有注释的代码。

This SO post has the code to zoom a map view to fit all its annotations.

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