如何使用javascript显示离我所在地点最近的纬度和经度?

use*_*406 6 javascript google-maps

我是堆栈溢出的新手,我想知道是否有人可以帮助我使用javascript并获得最近的纬度和经度到我当前的位置.

我有一个存储纬度和经度坐标的API,我已经将它们保存在javascript变量中,并且还将我当前的位置保存在变量中.

我需要以某种方式从我的列表中排序纬度和经度坐标列表,以显示最接近我当前位置的坐标.

这有意义吗?

use*_*495 5

计算距离的Javascript函数

$nearest=1; //Limit within 1 KM.

$("#jobnews").html("<h2>Results:</h2>"); 
$.each(info.companies.company, function(index, job)
{ 
    var lat = list.location.lat; 
    var lng = list.location.lng; 
    var nam = current.location.lat; 
    var cou = current.location.lng; 
    //In below line we will get the distance between current and given coordinates.
    var d=distance(nam, cou, lat, lng, "K");
    //Check the d is within 1 KM, If so then add it to map.
    if(nearest>d)
    {
        var map = "</p><img src='maps.google.com/maps/api/staticmap?center="; + lat + "," +lng +"&zoom=17&size=400x300&key="+key+"&maptype=roadmap&visual_refresh=true&markers=??color:red|"+lat+","+lng+"&sensor=true'width='300' height='280'alt='Map of your location.'/>"; 
        var nearestList = "<div><h3>DISPLAY INFO: "+lis.name+link+nam+lat+lng+cou+map+"</div>"; 
    }
}  

function distance(lat1, lon1, lat2, lon2, unit) {
    var radlat1 = Math.PI * lat1/180
    var radlat2 = Math.PI * lat2/180
    var radlon1 = Math.PI * lon1/180
    var radlon2 = Math.PI * lon2/180
    var theta = lon1-lon2
    var radtheta = Math.PI * theta/180
    var dist = Math.sin(radlat1) * Math.sin(radlat2) + Math.cos(radlat1) * Math.cos(radlat2) * Math.cos(radtheta);
    dist = Math.acos(dist)
    dist = dist * 180/Math.PI
    dist = dist * 60 * 1.1515
    if (unit=="K") { dist = dist * 1.609344 }
    if (unit=="N") { dist = dist * 0.8684 }
    return dist
}        
Run Code Online (Sandbox Code Playgroud)

传递给函数:

lat1, lon1 = 点 1 的纬度和经度(十进制度数)
lat2, lon2 = 点 2 的纬度和经度(十进制度数)
unit = 您希望得到的结果的单位
其中:“M”是法定英里
“K”是公里(默认)
'N' 是海里