IT Certificaions

Google Cloud Platform

Thursday, April 5, 2012

Google map: calculate distance between two points(Marker)

rad = function(x) {
            return x*Math.PI/180;
         }

distanceUsingHaversine = function(sourcePoint, destinationPoint) {
     var RADIUS = 6371;    // earth's mean radius in km
    var diff_Lat  = rad(destinationPoint.lat() - sourcePoint.lat());
    var diff_Long = rad(destinationPoint.lng() - sourcePoint.lng());

    var a = Math.sin(diff_Lat/2) * Math.sin(diff_Lat/2) +
          Math.cos(rad(sourcePoint.lat())) * Math.cos(rad(destinationPoint.lat())) * Math.sin(diff_Long/2) *     Math.sin(diff_Long/2);
  var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
  var dist =RADIUS * c;

  return dist.toFixed(3);
}