簡體   English   中英

如何在 ios 中的 map 上顯示從當前位置到其他位置的航向方向?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM