Calculating heading of compass to a specific coordinate instead of to north

守給你的承諾、 提交于 2019-12-05 13:59:33
double lon = location.longitude - otherLocation.longitude;
double y = sin(lon) * cos(otherLocation.latitude);
double x = cos(location.latitude) * sin(otherLocation.latitude) - sin(location.latitude) * cos(otherLocation.latitude) * cos(lon);
double angle = atan2(y, x);

angle is the bearing between the two locations.

“Map Coordinates Systems” in the Location Awareness Programming Guide says “Specifically, on a Mercator map projection, a straight line drawn between any two points on the map yields a course heading that can be used in actual navigation on the surface of the Earth.” This makes me think you should convert your map coordinates to map points (MKMapPointForCoordinate) and call atan on the difference between map points. (Actually, you should probably use atan2, not atan.) Have you tried that?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!