Calculate distance between 2 GPS locations (lat/long)
// adapted from http://www.movable-type.co.uk/scripts/latlong.html
function getDistanceInKm(lat1, long1, lat2, long2) {
function toRadians(deg) {
// note: replace Math.PI/180 with 0.0174532925199433 for better performance
return deg * (Math.PI / 180);
};
var RADIUS = 6371, // km
diffLat = toRadians(lat2 - lat1),
diffLon = toRadians(long2 - long1),
lat1 = toRadians(lat1),
lat2 = toRadians(lat2),
a = Math.sin(diffLat / 2) * Math.sin(diffLat / 2) +
Math.sin(diffLon / 2) * Math.sin(diffLon / 2) * Math.cos(lat1) * Math.cos(lat2),
c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)),
d = RADIUS * c; // distance between 2 geolocations (in units of RADIUS e.g. km)
return d;
}