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

柔情痞子 提交于 2019-11-29 20:10:33

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.

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

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

Adithya Ajith

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!