[GIS] How to draw route from OSRM on right road using OpenLayers

openlayersopenlayers-2route

Asking to OSRM for a route, OSRM is returning the geometry:

"route_geometry":"ity_rArqlxIvTv|AoCrY"

After decode them, I'm getting 3 GPS coordinates:

43.529557,          -5.659434
43.529208999999994, -5.660934
43.529281,          -5.66136

But with just 3 points, I'm getting this route 🙁

enter image description here

How can I draw a right route on the road with openlayers? Like this:

enter image description here

Best Answer

The route geometry you get is a encoded polyline. You can create a route from it as follows:

// route is ol.geom.LineString
var route = new ol.format.Polyline({
    factor: 1e6
}).readGeometry(polyline, {
    dataProjection: 'EPSG:4326',
    featureProjection: 'EPSG:3857'
});
var feature = new ol.Feature(route);
feature.setStyle(styles.route);
vectorSource.addFeature(feature);

Source : http://jsfiddle.net/jonataswalker/079xha47/