views:

88

answers:

5

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  A: 

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.

jeffamaphone
+1  A: 

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

Carl Smotricz
+1  A: 

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;
});
Mantis
+1  A: 

Google Map API has a function to find this.

GLatLng.distanceFrom()
Wai Yip Tung
+1  A: 

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/

Jay Bose
I just saw you're looking for "as the crow flies" Tung's the way to go.
Jay Bose