tags:

views:

90

answers:

2

I have a Map object created through the google maps api and I want to figure out the distance (km or mi) between the corners of the current view. I know how to get the coordinates of the corners, is there a utility function to calculate the distance between points?

A: 

From: http://code.google.com/apis/maps/documentation/reference.html#GLatLng

GLatLng.distanceFrom( other:GLatLng, radius?:Number );

Returns the distance, in meters, from this location to the given location. By default, this distance is calculated given the default equatorial earth radius of 6378137 meters. The earth is approximated as a sphere, hence the distance could be off as much as 0.3%, especially in the polar extremes. You may also pass an optional radius argument to calculate distances between GLatLng coordinates on spheres of a different radius than earth.

Clint
A: 

This somewhat depends on how accurate you need your answer to be.

The Google Maps API includes a function called distanceFrom, which will probably work just fine for you:

Returns the distance, in meters, from this location to the given location. By default, this distance is calculated given the default equatorial earth radius of 6378137 meters. The earth is approximated as a sphere, hence the distance could be off as much as 0.3%, especially in the polar extremes. You may also pass an optional radius argument to calculate distances between GLatLng coordinates on spheres of a different radius than earth.

If you need a more accurate answer, you will need a function that takes the shape of the Earth into consideration. I haven't tried it, but this one looks great.

Chris B