简体   繁体   中英

How to display the nearest latitude and longitude to my location using javascript?

I am new in stack overflow and I was wondering if anyone could help me with javascript and getting the nearest latitude and longitude to my current location.

I have an API that stores latitude and longitude coordinates and I have saved those in javascript variables, and also have my current location saved in variables.

I need to somehow sort the list of latitude and longitude coordinates from my list to display the nearest one to my current location.

Does this make sense?

You may want to include the Geolib library.

The first example demonstrates the getDistance function. If you combine this with map and sort you can get what you want in a couple of lines:

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);

Javascript function to calculate distance

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

Passed to function:

lat1, lon1 = Latitude and Longitude of point 1 (in decimal degrees)
lat2, lon2 = Latitude and Longitude of point 2 (in decimal degrees)
unit = the unit you desire for results
where: 'M' is statute miles
'K' is kilometers (default)
'N' is nautical miles

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM