If you want to calculate it yourself, then you can use the Haversine formula:
var rad = function(x) {
return x * Math.PI / 180;
};
var getDistance = function(p1, p2) {
var R = 6378137; // Earth’s mean radius in meter
var dLat = rad(p2.lat() - p1.lat());
var dLong = rad(p2.lng() - p1.lng());
var a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
Math.cos(rad(p1.lat())) * Math.cos(rad(p2.lat())) *
Math.sin(dLong / 2) * Math.sin(dLong / 2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
var d = R * c;
return d; // returns the distance in meter
};
Answer from Mike Williams on Stack OverflowIf you want to calculate it yourself, then you can use the Haversine formula:
var rad = function(x) {
return x * Math.PI / 180;
};
var getDistance = function(p1, p2) {
var R = 6378137; // Earth’s mean radius in meter
var dLat = rad(p2.lat() - p1.lat());
var dLong = rad(p2.lng() - p1.lng());
var a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
Math.cos(rad(p1.lat())) * Math.cos(rad(p2.lat())) *
Math.sin(dLong / 2) * Math.sin(dLong / 2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
var d = R * c;
return d; // returns the distance in meter
};
There actually seems to be a method in GMap3. It's a static method of the google.maps.geometry.spherical namespace.
It takes as arguments two LatLng objects and will utilize a default Earth radius of 6378137 meters, although the default radius can be overridden with a custom value if necessary.
Make sure you include:
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false&v=3&libraries=geometry"></script>
in your head section.
The call will be:
google.maps.geometry.spherical.computeDistanceBetween (latLngA, latLngB);
What is the distance between two consecutive latitudes?
The distance between any two adjacent latitudes is approximately 69 miles or 111 km. Latitude lines run parallel to each other. That's why the distance between them remains constant from the South to the North pole. On the other hand, longitude lines are furthest apart at the equator and meet at the poles.
What is the distance between the North and South pole?
The distance between the North and South pole is 20,015.09 km or 12,436.80 miles. The North pole, the northernmost point on the planet, is located at 90 degrees of latitude, while the South pole sits at -90 degrees.
Where is the Prime Meridian located?
5.3 arcseconds or 102 meters east of the Royal Observatory, Greenwich. This location serves as the International Reference Meridian, which also acts as the reference meridian for the Global Positioning System or GPS. The zero longitude line was moved to account for local gravitational effects.
If you've got the latitude and longitude, you can use the Haversine formula to calculate the distance between them - assuming you want "straight line" distance.
EDIT: Okay, now you've actually told us what you want, I suspect you need to use the Directions API. You'll need to make a web request to the appropriate URL with your parameters, specifying either XML or JSON output - which you'll then need to parse.
Note that "JSON" != "Javascript-based". Think of JSON as a data serialization format which just happens to be executable Javascript.
A quick Google gave me this: http://www.postalcodeworld.com/samples/distance.java.html Looks like this is a pretty good implementation of what you need using the Haversine Formula I think.
If Google Maps is using the great circle distance then the results are within less than 1% of the geodesic distance for the ellipsoid. If you want to see the true ellipsoidal geodesic path in Google Maps, then visit
http://geographiclib.sourceforge.net/scripts/geod-google.html
This uses Javascript to solve the geodesic problems for an ellipsoid. Wikipedia has a lot of information on ellipsoidal geodesics; see
https://en.wikipedia.org/wiki/Geodesics_on_an_ellipsoid
According to http://www.daftlogic.com/projects-google-maps-distance-calculator.htm
You can use the Google Maps Distance Calculator to find out the distance between two or more points anywhere on the earth. In other words, the distance between A and B. Click once on the map to place the first marker and then click again to position the second marker. The mileage between the points will then be displayed. You can also build up a series of locations to find a total distance. An important feature of this distance calculator tool is that is "as the crow flies"...
Calculating the "As the Crow Flies" distance is a matter of using Great Circle formula.
and the Great Circle formula reference is to http://en.wikipedia.org/wiki/Great-circle_distance which does provide various computational formulas.