Distance Method "GLatLng.distanceFrom" Earth Radius

Showing 1-4 of 4 messages
Distance Method "GLatLng.distanceFrom" Earth Radius cob_geo 8/24/06 2:05 PM
Hi guys,

I'm trying to understand the algorithm for calculating distances
with the GLatLng.distanceFrom method.
In the API refernce it says "The earth is approximated as a sphere".
What radius is assumed for that sphere?

What does the algorithm look like?
something like this? ("D" being the Distance in degrees,
to get meters you would have to multiply it
with a constant, that constant would be interesting, I
think it depends on the assumed earth radius)
cos D = (sin lat1 * sin lat2) + (cos lat1 * cos lat2 * cos


Re: Distance Method "GLatLng.distanceFrom" Earth Radius Marcelo 8/24/06 11:26 PM

The radius used seems to be 6378137 meters.

The function is this:

        var b=this.Sa();
        var c=a.Sa();
        var d=b-c;
        var e=this.Ua()-a.Ua();
        return f*6378137

It is obfuscated, so it might take me a couple of hours to read it
I believe it can be translated into this, but beware, I'm only guessing

        var b =  this.lat * Math.PI/180;
        var c = a.lat * Math.PI/180;
        var d = b-c;
        var e = this.lon * Math.PI/180 - a.lon * Math.PI/180;

        var f = 2 * Math.asin( Math.sqrt( Math.pow( Math.sin(d/2) , 2 ) +
Math.cos(b) * Math.cos(c) * Math.pow(Math.sin(e/2),2) ) );
        return f * 6378137;

Another function I have for the distance is this:

function getDistance(lat1, lon1, lat2, lon2, unit) {
        var radlat1 = Math.PI * lat1/180
        var radlat2 = Math.PI * lat2/180
        var radlon1 = Math.PI * lon1/180
        var radlon2 = Math.PI * lon2/180
        var theta = lon1-lon2
        var radtheta = Math.PI * theta/180
        var dist = Math.sin(radlat1) * Math.sin(radlat2) + Math.cos(radlat1) *
Math.cos(radlat2) * Math.cos(radtheta);
        dist = Math.acos(dist)
        dist = dist * 180/Math.PI
        dist = dist * 60 * 1.1515
        if (unit=="K") { dist = dist * 1.609344 }
        if (unit=="N") { dist = dist * 0.8684 }
        return dist


My map: http://maps.forum.nu/google_map_pub.html

Distance Method "GLatLng.distanceFrom" Earth Radius cob_geo 8/24/06 2:22 PM
Re: Distance Method "GLatLng.distanceFrom" Earth Radius cob_geo 8/25/06 12:53 PM
I experimented a bit around with different formulas, but in the end,
the formula I mentioned
in my first post brought the same results as the "GLatLng.distanceFrom"
method, using your radius of 6378137 m. I had to convert the degrees to
radians first, but now it's spot on :)