简体   繁体   English

如何在 ios 中的 map 上显示从当前位置到其他位置的航向方向?

[英]How to show heading direction from Current location to other location on map in ios?

I have a requirement where i have to show the heading direction towards any location on map from user current location.我有一个要求,我必须从用户当前位置向 map 上的任何位置显示航向。 Let say if we have 4 location annotation on map apart from current location and i want to show heading towards any of the location after tapping on it.假设我们在 map 上除了当前位置之外有 4 个位置注释,并且我想在点击它后显示朝向任何位置的方向。 How can we achieve it.我们怎样才能实现它。 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. 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. I not getting idea how can we externally get the heading data between two locations.我不知道我们如何从外部获取两个位置之间的航向数据。 Please help me out.请帮帮我。 Thanks in advance.提前致谢。

This will calculate the initial bearing to get from lat1,lon1 to lat2,lon2 on a straight line. 这将计算初始轴承以直线从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 version: 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM