2017-08-30 158 views
-1

我想比較我目前的緯度&經度到另一緯度&經度和發現附近的位置 我該如何計算?近緯度經緯度

my location : 
51.510836, -0.127579 

another : 

51.547385, 0.020022 
51.482876, -0.036542 
51.511894, -0.248477 
+0

發佈您的試用代碼。 – Priyal

+0

@Priyal我不知道我怎麼能找到附近的位置,但我的語言是PHP或JavaScript我想要計算算法 –

回答

3

據我所知 你的問題是通過經緯度長

得到最接近的位置,而且要計算哪一個是從您的位置

最接近的,如果是,那麼逐個比較所有這些3 lat,與您的位置lat長。 最近的位置是哪一個以km爲單位的最低距離。 試試這個。

function getDistanceFromLatLonInKm(lat1,lon1,lat2,lon2) { 
    var R = 6371; // Radius of the earth in km 
    var dLat = deg2rad(lat2-lat1); // deg2rad below 
    var dLon = deg2rad(lon2-lon1); 
    var a = 
    Math.sin(dLat/2) * Math.sin(dLat/2) + 
    Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) * 
    Math.sin(dLon/2) * Math.sin(dLon/2) 
    ; 
    var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 
    var d = R * c; // Distance in km 
    return d; 
} 

function deg2rad(deg) { 
    return deg * (Math.PI/180) 
} 

其中LAT1,lon1是您的位置和LAT2,lon2是那些點,你想比較簡單地將其應用於循環,並通過比較位置和最後的距離是最低的是你的答案。