distanceFrom (distance between 2 point)

464 views
Skip to first unread message

MacBoy

unread,
Feb 24, 2010, 6:01:22 AM2/24/10
to Google Maps JavaScript API v3
Please make it clear that, I am not asking about Directions service.
This question is about the distance between to point in the world.

In v2, there is a distanceFrom Methods under class GLatLng, but I
can't find it in v3, is there similar function available? is this
Methods going to appear in v3 soon?

Thanks,
Leonardo

v2 distanceFrom
http://code.google.com/intl/en/apis/maps/documentation/reference.html#GLatLng.distanceFrom

Chad Killingsworth

unread,
Feb 24, 2010, 8:00:30 PM2/24/10
to Google Maps JavaScript API v3
Until they are part of the API, you can add them yourself. The
following code will add a distanceFrom prototype to LatLng objects and
calculate the distance using the spherical law of cosines.

/**
* @param {google.maps.LatLng} newLatLng
* @returns {number}
*/
google.maps.LatLng.prototype.distanceFrom = function(newLatLng) {
var lat1 = this.lat() * Math.PI / 180.0;
var lat2 = newLatLng.lat() * Math.PI / 180.0;
var lngDiff = (newLatLng.lng() - this.lng()) * Math.PI / 180.0;
var a = Math.acos(Math.sin(lat1) * Math.sin(lat2) + Math.cos(lat1)
* Math.cos(lat2) * Math.cos(lngDiff)) * 20902231.0029; ;
return Math.acos(Math.sin(lat1) * Math.sin(lat2) + Math.cos(lat1)
* Math.cos(lat2) * Math.cos(lngDiff)) * 20902231.0029;
}

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

Chad Killingsworth

> v2 distanceFromhttp://code.google.com/intl/en/apis/maps/documentation/reference.html...

Chad Killingsworth

unread,
Feb 24, 2010, 9:29:46 PM2/24/10
to Google Maps JavaScript API v3
Whoops had an extra line left in there from some debugging. Try this:

/**
* @param {google.maps.LatLng} newLatLng
* @returns {number}
*/
google.maps.LatLng.prototype.distanceFrom = function(newLatLng) {
var lat1 = this.lat() * Math.PI / 180.0;
var lat2 = newLatLng.lat() * Math.PI / 180.0;
var lngDiff = (newLatLng.lng() - this.lng()) * Math.PI / 180.0;

return Math.acos(Math.sin(lat1) * Math.sin(lat2) + Math.cos(lat1)
* Math.cos(lat2) * Math.cos(lngDiff)) * 20902231.0029;
}

Chad Killingsworth

On Feb 24, 7:00 pm, Chad Killingsworth


<chadkillingswo...@missouristate.edu> wrote:
> Until they are part of the API, you can add them yourself. The
> following code will add a distanceFrom prototype to LatLng objects and
> calculate the distance using the spherical law of cosines.
>
> /**
> * @param {google.maps.LatLng} newLatLng
> * @returns {number}
> */
> google.maps.LatLng.prototype.distanceFrom = function(newLatLng) {
>     var lat1 = this.lat() * Math.PI / 180.0;
>     var lat2 = newLatLng.lat() * Math.PI / 180.0;
>     var lngDiff = (newLatLng.lng() - this.lng()) * Math.PI / 180.0;
>     var a = Math.acos(Math.sin(lat1) * Math.sin(lat2) + Math.cos(lat1)
> * Math.cos(lat2) * Math.cos(lngDiff)) * 20902231.0029; ;
>     return Math.acos(Math.sin(lat1) * Math.sin(lat2) + Math.cos(lat1)
> * Math.cos(lat2) * Math.cos(lngDiff)) * 20902231.0029;
>
> }
>

> Seehttp://www.movable-type.co.uk/scripts/latlong.htmlfor reference.

Message has been deleted

Esa

unread,
Feb 25, 2010, 9:11:43 AM2/25/10
to Google Maps JavaScript API v3
Quickly tried and I got very different results compared to mine:

/**
* distance from this LatLng to a given LatLng
*/
google.maps.LatLng.prototype.kmTo = function(a){
var e = Math, ra = e.PI/180;
var b = this.lat() * ra, c = a.lat() * ra, d = b - c;
var g = this.lng() * ra - a.lng() * ra;
var f = 2 * e.asin(e.sqrt(e.pow(e.sin(d/2), 2) + e.cos(b) * e.cos(c)
* e.pow(e.sin(g/2), 2)));
return f * 6378.137;

Chad Killingsworth

unread,
Feb 25, 2010, 9:19:10 AM2/25/10
to Google Maps JavaScript API v3
I suppose I should have specified, mine returns the results in feet.
I'm using it for relatively small distances.

Chad Killingsworth

MacBoy

unread,
Feb 26, 2010, 12:05:30 AM2/26/10
to Google Maps JavaScript API v3
Thanks everyone!

Chad, I tested your code, marker1LatLng.distanceFrom (marker1LatLng)
does not return 0.

I finally come up with the following code, I tested it return 0 for
the same LatLng.
The result is return in "meters", which is same as Google Map v2, but
the result is not except the same with v2, but similar I think.

Thanks,
Leonardo Wong

google.maps.LatLng.prototype.distanceFrom = function(newLatLng) {
//var R = 6371; // km (change this constant to get miles)
var R = 6371000; // meters
var lat1 = this.lat();
var lon1 = this.lng();
var lat2 = newLatLng.lat();
var lon2 = newLatLng.lng();
var dLat = (lat2-lat1) * Math.PI / 180;
var dLon = (lon2-lon1) * Math.PI / 180;
var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.cos(lat1 * Math.PI / 180 ) * Math.cos(lat2 * Math.PI / 180 )
*
Math.sin(dLon/2) * Math.sin(dLon/2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
var d = R * c;
return d;
}

Reference:
http://en.wikipedia.org/wiki/Earth_radius
http://groups.google.com/group/google-maps-js-api-v3/browse_thread/thread/f84629b7c774082b/5afb9bb86cbf966e?lnk=gst&q=distance#5afb9bb86cbf966e
http://www.barattalo.it/2009/12/19/ruler-for-google-maps-v3-to-measure-distance-on-map/

Reply all
Reply to author
Forward
0 new messages