39

Suppose I have a route defined from one town to another. From the Google Maps API I can recover a route between the two. However, the route returned from Google is a driving route that includes geo-coordinates only at places where there is another step in a leg (for example, where I have to turn from one highway to another).

What I need is geo-locations (lat/long) along the entire route, at specific intervals (for example, every 1/4 mile or 100m).

Is there a way to accomplish this via the Google Maps API / web services?

Or would the OpenStreetMap database be the way to do it?

Kind regards, Madeleine.

Madeleine P. Vincent
  • 3,361
  • 5
  • 25
  • 30
  • Are you trying to make "mile markers"? Like [this question](http://stackoverflow.com/questions/16705895/place-marker-on-polyline-with-specific-distance/16706394#16706394) or [this example](http://www.geocodezip.com/v3_GoogleEx_directions-waypoints_kmmarkersC.html) – geocodezip May 23 '13 at 13:03
  • Yes, something like mile markers is what I'm after. The geocodzip link is very helpful. – Madeleine P. Vincent May 24 '13 at 16:42

5 Answers5

30

OSRM gives you routes with road geometries as they are in the OpenStreetMap database. For example, you can get the route as GPX (and post-process this file if you want). This would look like the following:

GET http://router.project-osrm.org/viaroute?hl=en&loc=47.064970,15.458470&loc=47.071100,15.476760&output=gpx

Read more: OSRM API docs.

tyr
  • 1,779
  • 1
  • 15
  • 15
  • Is there something similar for paths or trails? – masterwok Sep 09 '16 at 23:18
  • 1
    The query above does not work anymore since the "viaroute" service of OSRM was replaced by the "route" service. See my answer for a current solution: https://stackoverflow.com/a/47539464/2350644 – ice_chrysler Nov 28 '17 at 19:32
13

Since the accepted answer is outdated and does not work anymore, here is how all nodes along a road can be queried using the route service from Project OSRM.

Given an arbitrary number of lon,lat pairs. For Instance the following three (in Berlin):

  • 13.388860,52.517037
  • 13.397634,52.529407
  • 13.428555,52.523219

The route-service calculates the fastest route between these points and its possible to return all nodes along the road using the following query:

http://router.project-osrm.org/route/v1/driving/13.388860,52.517037;13.397634,52.529407;13.428555,52.523219?alternatives=false&annotations=nodes

This returns a json response containing node IDs of all the nodes along the route. The result should look something like this:

{
  "routes": [
    {
      ...
      "legs": [
        {
          "annotation": {
            "nodes": [
              2264199819,
              2045820592,
              21487242,
              ...
    ]
 }

To receive the lat,lon coordinates of the nodes OverpassAPI can be used.

[out:json];
(
   node(264199819);
   node(...);
   node(...);
   ...
);
(._;>;);
out;

Here is a sample request using overpass-turbo: http://overpass-turbo.eu/s/toe

ice_chrysler
  • 2,633
  • 1
  • 21
  • 27
  • 2
    I have source and destination lat-lng pair and I want the lat-lng pairs for intermediate points which are not far by say 20 miles. How do I achieve that? –  Mar 06 '18 at 19:52
  • When I use the OverpassAPI to get the lats and lngs, I get a json that is sorted by node_id. But I want the json sorted as same as the order of nodes in my query. Do you have any idea, whether I can get it right? – Ehsan Jun 28 '18 at 07:46
  • Whilst this does indeed work, it requires another API request, which uses data unnecessarily and can be slow (I made an 8 second request extremely easily). I'd recommend the answer by @Adithya Ajith, because it is almost instant. – JaffaKetchup Jun 10 '21 at 17:54
6

It's simply google.maps.DirectionsService().route() method. You need to pass the service request and then a callback which executes upon completion of the service request.

https://developers.google.com/maps/documentation/javascript/directions

malintha
  • 176
  • 3
  • 18
4

While not used as API, here: https://www.nmeagen.org/ one can create "Multi-point line", set the distance between points and download route (coordinates) as CSV.

Ohad Lahav
  • 290
  • 3
  • 7
1

Adding to the Marlio's answer.

You can use Google Maps Directions API itself. For a given origin and destination, in the JSON output, look for following:

"polyline" : {
  "points" : ""
}

You can use a decoder to get the coordinates from the polyline.: https://github.com/emcconville/google-map-polyline-encoding-tool Or. you can use the googleway package in R to decode the same. https://cran.r-project.org/web/packages/googleway/googleway.pdf

I am not sure how to set the resolution to your desired level though.But the resolution in the API output is really good.

SymbolixAU
  • 25,502
  • 4
  • 67
  • 139
Adithya Ajith
  • 57
  • 1
  • 10
  • Actually, you'll want to get the `overview_polyline` to get all the lat/lon coordinates for the entire route. The [vignette](https://github.com/SymbolixAU/googleway/blob/master/vignettes/googleway-vignette.Rmd) gives more examples & detail. – SymbolixAU May 29 '17 at 09:55
  • But the overview does not trace the curvature of the road precisely.The polyline points for each step in the legs give more detailed route coordinates. – Adithya Ajith May 29 '17 at 17:41
  • In my opinion using each of the individual `polyline : points` results in a lot of redundant information (for example the last coordinate of one leg will be the start of the next). Whereas the `overview_polyline` has a point for every change of direction along a road & route, and all the points in between are therefore on a straight line and can be inferred. – SymbolixAU May 29 '17 at 21:55