了解 MKCoordinateFromMapPoint 行为

发布于 2025-01-07 20:28:49 字数 811 浏览 7 评论 0原文

在基于位置的应用程序中,我们使用 MKMapPoints 来存储位置,例如当前用户位置。

当我们尝试在 MKMapView 上使用此位置时,为了设置最初显示的区域(在用户上放大),我们将其转换为 CLLocationCooperative2D

有一个方便的方法:即:MKCooperativeForMapPoint,但在测试过程中这会给出奇怪的结果。

  MKMapPoint mapPoint = MKMapPointMake(51.96, 6.3); // My area ;)
  CLLocationCoordinate2D automagicCoordinate = MKCoordinateForMapPoint(mapPoint);
  CLLocationCoordinate2D manualCoordinate = CLLocationCoordinate2DMake(mapPoint.x, mapPoint.y);

我希望 automagicCo整理 和manualCo整理 完全相同。 但是当我在调试器中检查它时,我得到以下结果:

automagicCoordinate.latitude = (CLLocationDegrees) 85.05
automagicCoordinate.longitude = (CLLocationDegrees) -179.99

manualCoordinate.latitude = (CLLocationDegrees) 51.96
manualCoordinate.longitude = (CLLocationDegrees) 6.3

为什么用该方法创建的坐标不正确?

In a location based app we use MKMapPoints to store locations, for example the current user location.

When we try use this location on a MKMapView, to set the region that is initially displayed (zoomed in on the user) we convert this to a CLLocationCoordinate2D

There's a convernience method for that: namenly: MKCoordinateForMapPoint, but during testing this gives strange results.

  MKMapPoint mapPoint = MKMapPointMake(51.96, 6.3); // My area ;)
  CLLocationCoordinate2D automagicCoordinate = MKCoordinateForMapPoint(mapPoint);
  CLLocationCoordinate2D manualCoordinate = CLLocationCoordinate2DMake(mapPoint.x, mapPoint.y);

I would expect both the automagicCoordinate and the manualCoordinate to be exactply the same.
but when I inspect it in the debugger I get the following result:

automagicCoordinate.latitude = (CLLocationDegrees) 85.05
automagicCoordinate.longitude = (CLLocationDegrees) -179.99

manualCoordinate.latitude = (CLLocationDegrees) 51.96
manualCoordinate.longitude = (CLLocationDegrees) 6.3

How come the coordinate created with the method is incorrect?

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

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

发布评论

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

评论(1

原来分手还会想你 2025-01-14 20:28:49

MKMapPoint 不是纬度和经度。如果是的话,您就不需要一个函数将其“转换”为坐标。

正如《位置感知编程指南》中所述 了解地图几何部分:

地图点是墨卡托地图投影上的 x 和 y 值。地图点代替地图坐标用于许多与地图相关的计算,因为它们简化了计算中涉及的数学。

MKMapPoint 的文档 更清晰:

如果将地球仪的曲面投影到平面上,
你得到的是地图的二维版本,其中经度
线看起来是平行的。 ...

地图点的实际单位与所使用的基础单位相关
绘制 MKMapView 的内容,但您永远不需要
直接担心这些单位。 ...

将地图相关数据保存到文件时,应始终保存
坐标值(纬度和经度)而不是地图点。

地图点 51.96, 6.3 对应于地图投影左上角的坐标。如果您想使用坐标(纬度、经度),请使用 CLLocationCooperative2D 以避免混淆。

(从技术上讲,您可以使用 MKMapPoint 结构来存储坐标值,但它们不需要转换为坐标,并且错误的类型使用只会导致混乱。)

An MKMapPoint is not a latitude and longitude. If it was, you wouldn't need a function to "convert" it to coordinates.

As the Location Awareness Programming Guide explains in the Understanding Map Geometry section:

A map point is an x and y value on the Mercator map projection. Map points are used for many map-related calculations instead of map coordinates because they simplify the mathematics involved in the calculations.

The documentation for MKMapPoint is clearer:

If you project the curved surface of the globe onto a flat surface,
what you get is a two-dimensional version of a map where longitude
lines appear to be parallel. ...

The actual units of a map point are tied to the underlying units used
to draw the contents of an MKMapView, but you should never need to
worry about these units directly. ...

When saving map-related data to a file, you should always save
coordinate values (latitude and longitude) and not map points.

The map point 51.96, 6.3 corresponds to a coordinate at the top-left of the map projection. If you want to work with coordinates (latitude, longitude), use a CLLocationCoordinate2D to avoid confusion.

(You can technically use an MKMapPoint struct to store your coordinate values but then they don't need to be converted to coordinates and the wrong type usage will just lead to confusion.)

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