Google Maps API Tutorial

© 2006, 2007, 2009 Mike Williams

Translate this page:



The API provides a function that calculates the straight line distance between two points.

To use it to calculate the distance in metres between two GLatLng objects, we write P1.distanceFrom(P2).

That returns a number with the maximum number of decimal places that your Javascript interpreter supports, which is somewhat excessive so you might want to use the .toFixed(N) method to round the result to N decimal places.

Here's an example of the code in use.

For the bearing, I've used an algorithm by Vincenty which is claimed to produce better results than using atan2() when the distances are large.

Back to Mike's Homepage
Up to the start of this tutorial