Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<h1>My New Answer :)</h1> <p>Use the <a href="http://code.google.com/apis/maps/documentation/directions/" rel="nofollow noreferrer">Google Directions API</a>.</p> <p>It should be possible to make a request to <code>http://maps.google.com/maps/api/directions/&lt;json|xml&gt;?&lt;params&gt;</code> and specifying the coords as <code>origin</code> and <code>destination</code> parameter. <strike>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:</p> <blockquote> <p>[...] origin (required) — The address or <em>textual latitude/longitude value</em> from which you wish to calculate directions [...]</p> </blockquote> <p>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).</strike></p> <p><strong>It works:</strong> Here's an example URL: <code>http://maps.google.com/maps/api/directions/json?origin=49.75332,6.50322&amp;destination=49.71482,6.49944&amp;mode=walking&amp;sensor=false</code></p> <h1>My Previous Answer</h1> <p>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.</p> <p>A while back, I wrote down the (well-known) algorithm (<a href="http://en.wikipedia.org/wiki/Haversine_formula" rel="nofollow noreferrer">Haversine</a>) in a blog post (<a href="http://foobar.lu/wp/2007/09/17/calculate-the-distance-between-two-gps-coordinates/" rel="nofollow noreferrer">python</a> and <a href="http://foobar.lu/wp/2007/09/17/calculate-the-distance-between-two-gps-coordinates-ctd/" rel="nofollow noreferrer">pl/sql</a>)</p> <p>Here's the copy of the python code:</p> <pre><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[0]-b[0]) dLon = radians(a[1]-b[1]) x = sin(dLat/2) ** 2 + \ cos(radians(a[0])) * cos(radians(b[0])) *\ sin(dLon/2) ** 2 #original# y = 2 * atan2(sqrt(x), sqrt(1-x)) y = 2 * asin(sqrt(x)) d = r * y return d </code></pre> <p>Translating this to Java should be trivial.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload