Take the 2-minute tour ×

Is it possible to send two lat long points to google to calculate the distance between the two?

share|improve this question
Go straight to @SunnyD's answer below for the Google Maps API V3 answer. –  Josh Jul 15 '11 at 15:22

8 Answers 8

If you're looking to use the v3 google maps API, here is a function I use: Note: you must add &libraries=geometry to your script source

<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false&libraries=geometry"></script>

Now the function:

//calculates distance between two points in km's
function calcDistance(p1, p2){
  return (google.maps.geometry.spherical.computeDistanceBetween(p1, p2) / 1000).toFixed(2);
share|improve this answer
I want road distance not geometric distance? –  codepk Oct 19 '13 at 3:14

What you're after is the Haversine formula. You don't need Google Maps to do this, you can work it out separately. There's a script to do this (in JavaScript) here.

share|improve this answer

Yes google can do this

google api docs

here's a piece of java script that get's the distance in km's for two points

function initialize() {
      if (GBrowserIsCompatible()) {
          map.setCenter(new GLatLng(52.6345701, -1.1294433), 13);
          map.addControl(new GLargeMapControl());          
          map.addControl(new GMapTypeControl());
          geocoder = new GClientGeocoder();

          // NR14 7PZ
          var loc1 = new GLatLng(52.5773139, 1.3712427);
          // NR32 1TB
          var loc2 = new GLatLng(52.4788314, 1.7577444);          
          alert(loc2.distanceFrom(loc1) / 1000);

share|improve this answer

No, but you can use Vincenty's formula, which models the shape of the Earth. It's available in Javascript here.

A quite easy to parse web page is (e.g.): http://boulter.com/gps/distance/?from=51.5329855+-0.1303506&to=40.757584+-73.985642&units=m

For people who only want a quick measure of distance, check out this gadget. However, it only uses the great circle calculation.

As Rob notes, Google have added it, but they still only use the great circle formula, which can be inaccurate by 1/300.

share|improve this answer

And the distance cal in c#:

    	public static double CalculateDistance(double lat1, double lon1, double lat2, double lon2)
		double t = lon1 - lon2;
		double distance = Math.Sin(Degree2Radius(lat1)) * Math.Sin(Degree2Radius(lat2)) + Math.Cos(Degree2Radius(lat1)) * Math.Cos(Degree2Radius(lat2)) * Math.Cos(Degree2Radius(t));
		distance = Math.Acos(distance);
		distance = Radius2Degree(distance);
		distance = distance * 60 * 1.1515;

		return distance;


	private static double Degree2Radius(double deg)
		return (deg * Math.PI / 180.0);

	private static double Radius2Degree(double rad)
		return rad / Math.PI * 180.0;
share|improve this answer

Its been many years since I did this but you could use the Great Circle Distance

share|improve this answer

If you just want to get the distance as a number, try something like this.

function InitDistances() {
    var startLocation = new GLatLng(startLat, startLon);
    var endLocation = new GLatLng(endLat, endLon);
    var dist = startLocation .distanceFrom(endLocation);

    // Convert distance to miles with two decimal points precision
    return (dist / 1609.344).toFixed(2);
share|improve this answer
up vote 0 down vote accepted

there is a handy function


basically create 2 GlatLng objects then use the above method

share|improve this answer
This is for version 2 of the API - do you know if a similar thing exists in V3? –  Rachel May 21 '10 at 14:42
not that I know of but I guess the closest thing is using the route planner and getting the distance from that –  thiswayup May 25 '10 at 10:59

Your Answer


