1

I've been asked if i could create a website like these:

I'm curious as to how i would be able to calculate distance between places. For example if i were to enter a dollar bill in the system form Minneapolis, and that bill was then entered by another user in LA, how would i calculate how many miles that is?

I've looked at the Google maps API a little bit, but i didn't see anything about calculating distance.

1
  • Do you want as the crow flies or a routing distance? Jul 21, 2010 at 14:30

5 Answers 5

1

Google Map API has a function to find this.

GLatLng.distanceFrom()
1

Take the GPS coordinates of the two locations and then use the formula for finding distance between two points on a sphere? That would be the as-the-crow-flies distance. If you want the shortest driving distance, then that's different.

1
  • Well the Earth is an oblate spheroid not a sphere. Its squashed at the poles. So to get a more accurate result you need to use Vincenty formula for distance between two Latitude/Longitude points.
    – Tim Child
    Dec 23, 2013 at 23:21
1

Here's an explanation, with formulae: http://en.wikipedia.org/wiki/Great-circle_distance .

1

What you want is a JavaScript implementation of the Vincenty formula for distance between two Latitude/Longitude points.

See http://www.movable-type.co.uk/scripts/latlong-vincenty.html

This is quite simple to then implement in Google Maps, if you need to, taking the two LatLng coordinates as the parameters.

For example, to get the distance between the last two points clicked on the map:

// requires distVincenty method from www.movable-type.co.uk/scripts/latlong-vincenty.html
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
lastClickLatLng = false;
google.maps.event.addListener(map, 'click', function (event) {
    if (lastClickLatLng) {
        var myDistance = distVincenty(lastClickLatLng.lat(), lastClickLatLng.lng(), event.latLng.lat(), event.latLng.lng());
        // Do something with myDistance!
    }
    lastClickLatLng = event.latLng;
});
1

Wai Yip Tung's response is perfect for an "as the crow flies" calculation. However, if you want to know the distance via walking or driving, use Google's Directions API - http://code.google.com/apis/maps/documentation/directions/

1
  • I just saw you're looking for "as the crow flies" Tung's the way to go.
    – Jay Bose
    Jul 23, 2010 at 1:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.