Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Google Map v3: How to calculate distance between two end points?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 250
    Comment on it

    Google Map v3: Calculate Distance

    How to calculate distance between two end points in google map v3?

    Use the Haversine formula to calculate distance between two end points.

    Note: you must add &libraries=geometry to your script source. This is the following code:

    <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false&libraries=geometry"></script>
    
    var rad = function(x) {
      return x * Math.PI / 180;
    };
    
    var getDistance = function(p1, p2) {
      var R = 6378137; // Earths mean radius in meter
      var dLat = rad(p2.lat() - p1.lat());
      var dLong = rad(p2.lng() - p1.lng());
      var a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
        Math.cos(rad(p1.lat())) * Math.cos(rad(p2.lat())) *
        Math.sin(dLong / 2) * Math.sin(dLong / 2);
      var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
      var d = R * c;
      return d; // returns the distance in meter
    };

    Thats all!

    Thanks for reading the blog

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: