了解 MKCoordinateFromMapPoint 行为
在基于位置的应用程序中,我们使用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
MKMapPoint
不是纬度和经度。如果是的话,您就不需要一个函数将其“转换”为坐标。正如《位置感知编程指南》中所述 了解地图几何部分:
MKMapPoint 的文档 更清晰:
地图点
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:
The documentation for MKMapPoint is clearer:
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 aCLLocationCoordinate2D
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.)