[英]How to show heading direction from Current location to other location on map in ios?
我有一个要求,我必须从用户当前位置向 map 上的任何位置显示航向。 假设我们在 map 上除了当前位置之外有 4 个位置注释,并且我想在点击它后显示朝向任何位置的方向。 我们怎样才能实现它。 I have gone trough Map API documentation & Locaiotn API, I found that we get the heading value in the delegate method which provided by API when we called the startupdatingheading method. 我不知道我们如何从外部获取两个位置之间的航向数据。 请帮帮我。 提前致谢。
这将计算初始轴承以直线从lat1,lon1到lat2,lon2。
double lat1=48.0; // source latitude, example data
double lon1=17.0; // source longitude
double lat2=49.0; // destination latitude
double lon2=18.0; // destination longitude
double lat1Rad = lat1 * M_PI / 180;
double lat2Rad = lat2 * M_PI / 180;
double dLon = (lon2 - lon1) * M_PI / 180;
double y = sin(dLon) * cos(lat2Rad);
double x = cos(lat1Rad) * sin(lat2Rad) - sin(lat1Rad) * cos(lat2Rad) * cos(dLon);
double bearingRad = atan2(y, x);
// this is the bearing from source to destination in degrees, normalized to 0..360
double bearing = fmod((bearingRad * 180 / M_PI + 360),360);
Swift版本:
func getHeading(fromLoc: CLLocationCoordinate2D, toLoc: CLLocationCoordinate2D) -> Double {
let lat1Rad = fromLoc.latitude * Double.pi / 180
let lat2Rad = toLoc.latitude * Double.pi / 180
let dLon = (toLoc.longitude - fromLoc.longitude) * Double.pi / 180
let y = sin(dLon) * cos(lat2Rad)
let x = cos(lat1Rad) * sin(lat2Rad) - sin(lat1Rad) * cos(lat2Rad) * cos(dLon)
let bearingRad = atan2(y, x)
// this is the bearing from/to in degrees, normalized to 0..360
return fmod((bearingRad * 180 / Double.pi + 360),360);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.