I need to organize an array so that each element is in the closest proximity to its previous location.
The array looks like this:
locations=[{"loc1",lat,long},{"loc2",lat,long},{"loc3",lat,long},{"loc4",lat,long},{"loc5",lat,long}]
Here's the function for calculating the distance:
var distance = function(lat1, lon1, lat2, lon2)
{
var radlat1 = Math.PI * lat1/180;
var radlat2 = Math.PI * lat2/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;
dist = dist * 1.609344 ;
return dist;
}
When you input values into this function, it provides the distance between two locations.
The starting point is the first element of the locations array. Now, I am looking for a function that can take an array and return the sorted array based on proximity.