2017-08-30 317 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是那些点,你想比较简单地将其应用于循环,并通过比较位置和最后的距离是最低的是你的答案。