4

I've been trying to get a list of co-ordinates in a route from point A to B using OSRM with the following request:

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

However, on opening the url, i find the 'via_points' tag containing only two co-ordinates. Surely, that can't be the entire route? Anything I'm missing here? Is there any other way of generating the list of route co-ordinates with OSRM? Thanks

1
  • Please use the new API. The old endpoint will be deprecated soon. What you want is result.routes[0].overview with paramaters overview=full and then decode with github.com/mapbox/polyline Commented Jun 24, 2016 at 15:26

2 Answers 2

1

The route is contained in the route_geometry object. It is an encoded polyline. If you don't want to uncompress it yourself you can disable compression via compression=false:

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

Not sure what the via_points contains. OSRM's documentation seems to be outdated. Maybe they are just your start and end points snapped to the nearest road or something similar.

Sign up to request clarification or add additional context in comments.

2 Comments

The documentation is up to date, your are just using the old API. For the new version >=5 the request would look as follows: router.project-osrm.org/route/v1/driving/…
A little confused. do i use overview=full or overview=false? what does it mean?
0

The URL /viaroute listed in the question and the other answer is deprecated. Instead you should use the URL /route as shown in the simple demo below:

OpenstreetMap with 2 markers and OSRM route

'use strict';

function processOsrmReply(data) {

  if (data.code === 'Ok') {
    data.routes.forEach(function(route) {
      routesGroup.addData(route.geometry);
    });

    myMap.flyToBounds(routesGroup.getBounds());
  }
}

function sendOsrmRequest() {
  routesGroup.clearLayers();

  var url = 'https://router.project-osrm.org/route/v1/car/' +
    parseFloat(startMarker.getLatLng().lng).toFixed(6) + ',' +
    parseFloat(startMarker.getLatLng().lat).toFixed(6) + ';' +
    parseFloat(finalMarker.getLatLng().lng).toFixed(6) + ',' +
    parseFloat(finalMarker.getLatLng().lat).toFixed(6) +
    '?overview=simplified' +
    '&alternatives=3' +
    '&steps=false' +
    '&annotations=false' +
    '&geometries=geojson';

  var request = new XMLHttpRequest();
  request.open('GET', url, true);
  request.onload = function() {
    if (this.status >= 200 && this.status < 400) {
      var data = JSON.parse(this.response);
      processOsrmReply(data);
    }
  };
  request.send();
}

var startPosition = [47.064970, 15.458470];
var finalPosition = [47.071100, 15.476760];

var myMap = L.map('myMap').setView(startPosition, 14);
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
  attribution: '&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
}).addTo(myMap);

var markersGroup = L.featureGroup().addTo(myMap);
var routesGroup = L.geoJSON().addTo(myMap);

var overlays = {
  'Show start and finish markers': markersGroup,
  'Show OSRM route geometry': routesGroup
};

L.control.layers(null, overlays, { collapsed: false }).addTo(myMap);

var startMarker = L.marker(startPosition, { draggable: true })
  .on('dragend', sendOsrmRequest)
  .bindPopup('Start')
  .addTo(markersGroup);

var finalMarker = L.marker(finalPosition, { draggable: true })
  .on('dragend', sendOsrmRequest)
  .bindPopup('Finish')
  .addTo(markersGroup);

sendOsrmRequest();
html,
body {
  margin: 0;
  padding: 0;
}

#myMap {
  position: absolute;
  top: 2em;
  left: 0;
  bottom: 0;
  right: 0;
  z-index: 1;
}

#myStatus {
  position: absolute;
  z-index: 2;
  width: 100%;
  text-align: center;
}
<link href="https://cdn.jsdelivr.net/npm/leaflet@1/dist/leaflet.min.css" type="text/css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/leaflet@1/dist/leaflet-src.min.js"></script>

<div id="myStatus">Drag the 2 markers to calculate the OSRM route</div>
<div id="myMap"></div>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.