I want to sort an array such that each element is in the shortest distance from previous location.
array is such like that
locations=[{"loc1",lat,long},{"loc2",lat,long},{"loc3",lat,long},{"loc4",lat,long},{"loc5",lat,long}]
the function to calculate the distance is this:
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;
}
This function when passed the value provide the distance between two location.
The starting point is the first element of locations array now i want a function that will take array and return the sorted array.