如何使用JavaScript显示离我当前位置最近的纬度和经度?

7

我是stackoverflow上的新手,想知道是否有人可以帮助我处理与javascript相关的问题,如何获取最接近我的当前位置的纬度和经度。

我有一个API存储了纬度和经度坐标,并将其保存在javascript变量中,同时也将我的当前位置保存在变量中。

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

这能理解吗?


你好,欢迎来到 Stack Overflow。请提供更多关于你尝试过的内容以及代码组织方式的细节。目前为止,你还没有提供足够的信息来获得具体的答案。通常情况下,SO旨在回答特定可回答的问题,而不是调试会话,因此我们需要更多的信息才能给你一个答案。 - Ben McCormick
你是指像这个(http://www.geocodezip.com/v3_MW_example_map3_closestMarker.html)一样的吗?(地图上点击位置最近的点)还是像这个(http://www.geocodezip.com/v3_MW_example_map3_closestMarker_geocode.html)一样的吗?(最接近地理编码地址的点) - geocodezip
嗨,感谢您的快速回复!我不想浪费太多您的时间,所以我会尽可能简洁,并感谢您的帮助。 - user3078406
好的..所以..我从API中提取了纬度和经度数据坐标,我想通过最接近我的当前位置在谷歌地图上显示它们。到目前为止,我已经添加了一个带有我的当前位置的地图,并显示了我的坐标数据库,但它不是基于最近的位置列出的。它列出了随机位置,而不管我现在在哪里。我知道我需要创建一个函数,使我能够获得最接近我的当前位置的位置。我只是无法弄清楚它背后的逻辑。非常感谢您的任何帮助 :) - user3078406
2个回答

8
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
}        

传递给函数的参数:

lat1,lon1 = 点1的纬度和经度(以十进制度表示)
lat2,lon2 = 点2的纬度和经度(以十进制度表示)
unit = 您所需结果的单位
其中:'M'代表英里
'K'代表千米(默认值)
'N'代表海里


请使用您当前位置和最近位置坐标调用函数,考虑函数返回的最小距离? - user2486495
我将重新发布我的回复。我很抱歉。 - user3078406
你好,这是我的代码:$("#jobnews").html("<h2>结果:</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;var map = "</p><img src='http://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>显示信息:"+lis.name+link+nam+lat+lng+cou+map+"</div>"; - user3078406
代码看起来很混乱,但我不能超过stackoverflow的文本限制。我想知道你的代码是否可以与我的代码一起运行。非常感谢! - user3078406
编辑您的问题,包括此代码,以便更容易回答您的问题! - sabotero
显示剩余2条评论

4

您可能想要包含Geolib库。

第一个示例演示了getDistance函数。如果将其与mapsort结合使用,您可以在几行代码中获得所需内容:

var current = {lat: 10, lng: 20}
var coords = [{lat: 5, lng: 5}, {lat: 10.2, lng: 19.6}, {lat: 60, lng: 10}];

var distFromCurrent = function(coord) {
    return {coord: coord, dist: geolib.getDistance(current, coord)};
}

var closest = coords.map(distFromCurrent).sort(function(a,b)  { return a.dist - b.dist })[0];

console.log("Closest: " + closest.coord.lat + ", " + closest.coord.lng);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接