开发者

How to calculate distance between 2 places

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

  • http://www.trackdollarbills.com/tour/
  • http://www.wheresgeorge.com/hits_by_county.php/
  • http://www.wherehaveibeen.com/

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.


Google Map API has a function to find this.

GLatLng.distanceFrom()


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.


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


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;
});


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/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜