My New Answer :)
Use the Google Directions API.
It should be possible to make a request to
http://maps.google.com/maps/api/directions/<json|xml>?<params> and specifying the coords as
I briefly tried it but it returned no results. Along their docs it should work, but they don't explain in detail how to specify latitude and longitude. But they say it's possible. Quote:
[...] origin (required) — The address or textual latitude/longitude value from which you wish to calculate directions [...]
Nevertheless, this should get you started. I would suggest going with the JSON output format. It's much simpler to parse and should use up less bandwidth (it's less verbose as XML).
It works: Here's an example URL:
My Previous Answer
The straight line distance can easily be determined using the Haversine formula. If you retrieve the route from Google, then you could calculate the distance of each segment and sum them up.
A while back, I wrote down the (well-known) algorithm (Haversine) in a blog post (python and pl/sql)
Here's the copy of the python code:
from math import sin, cos, radians, sqrt, atan2
def lldistance(a, b):
Calculates the distance between two GPS points (decimal)
@param a: 2-tuple of point A
@param b: 2-tuple of point B
@return: distance in m
r = 6367442.5 # average earth radius in m
dLat = radians(a-b)
dLon = radians(a-b)
x = sin(dLat/2) ** 2 + \
cos(radians(a)) * cos(radians(b)) *\
sin(dLon/2) ** 2
#original# y = 2 * atan2(sqrt(x), sqrt(1-x))
y = 2 * asin(sqrt(x))
d = r * y
Translating this to Java should be trivial.