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.
Answer from Jon Skeet on Stack Overflowjava - Calculate distance using google map given a latitude and longitude - Stack Overflow
Calculate distance between two points in google maps V3
coordinate system - What formula is used for distance between 2 points in Google Maps? - Geographic Information Systems Stack Exchange
How can I calculate the distance between two points using Latitude and Longitude?
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 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);
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.