Get exact geo coordinates along an entire route, Google Maps or OpenStreetMap
Asked Answered
A

5

40

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.

Anatolian answered 23/5, 2013 at 10:19 Comment(2)
Are you trying to make "mile markers"? Like this question or this exampleLabaw
Yes, something like mile markers is what I'm after. The geocodzip link is very helpful.Anatolian
A
29

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.

Azal answered 23/5, 2013 at 13:57 Comment(2)
Is there something similar for paths or trails?Auxiliaries
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://mcmap.net/q/397718/-get-exact-geo-coordinates-along-an-entire-route-google-maps-or-openstreetmapBuenabuenaventura
B
14

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

Buenabuenaventura answered 28/11, 2017 at 19:30 Comment(3)
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?Nucellus
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?Marcelmarcela
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.Icebound
H
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

Helbonnah answered 15/8, 2013 at 16:16 Comment(1)
Looking at it. But does it give the lat.long between a "leg", say, which is larger than 10 miles ?Dejong
A
5

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.

Archenemy answered 25/4, 2021 at 11:20 Comment(1)
I was looking for something exactly like this! I imported the CSV file into the project and can draw any custom line I want. Thank you!Reathareave
R
2

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.

Ranite answered 26/5, 2017 at 21:47 Comment(3)
Actually, you'll want to get the overview_polyline to get all the lat/lon coordinates for the entire route. The vignette gives more examples & detail.Perryperryman
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.Ranite
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.Perryperryman

© 2022 - 2024 — McMap. All rights reserved.