Leaflet OSRM Server – How to Change OSRM Server with Leaflet-Routing-Machine

leafletopen-source-routing-machineopenstreetmap

I am using leaflet-routing-machine with OSRM, How can I change OSRM server and do not use demo?

I have the same warning:

You are using OSRM's demo server. Please note that it is **NOT SUITABLE FOR PRODUCTION       USE**.
Refer to the demo server's usage policy: https://github.com/Project-OSRM/osrm-backend   /wiki/Api-usage-policy

To change, set the serviceUrl option.

Please do not report issues with this server to neither Leaflet Routing Machine or OSRM -   it's for
demo only, and will sometimes not be available, or work in unexpected ways.

Please set up your own OSRM server, or use a paid service provider for production.

Today, the routes have not showed and I have the same error in Firefox:

Routing error: 
Object { message: "HTTP request failed: abort", url: "https://router.project-osrm.org/route   /v1/driving/-4.772748,3…38.013284?overview=false&alternatives=true&steps=true&hints=;", status:    -1, target: abort }

In Chrome:

 Access to XMLHttpRequest at 'https://router.project-osrm.org/route/v1/driving   /-4.772748,37.89155;-3.370496,38.013284?overview=false&alternatives=true&steps=true&hints=;'          from origin 'https://www.transeop.com' has been blocked by CORS policy: No 'Access-Control-      Allow-Origin' header is present on the requested resource.
 router.project-osrm.org/route/v1/driving/-4.772748,37.89155;-3.370496,38.013284?overview=false&alternatives=true&steps=true&hints=;:1 Failed to load resource: net::ERR_FAILED

It is my code:

    L.tileLayer('https://{s}.tile.osm.org/{z}/{x}/{y}.png', {
      attribution: '&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
    }).addTo(map);

  let control = L.Routing.control({
  waypoints: [
    L.latLng(origin_latitude, origin_longitude),
    L.latLng(destination_latitude, destination_longitude)
  ],
  plan: L.Routing.plan([
    L.latLng(origin_latitude, origin_longitude),
    L.latLng(destination_latitude, destination_longitude)
  ], {
    createMarker: function(i, wp) {
      return L.marker(wp.latLng, {
        draggable: false
      });
    }
  }),
  addWaypoints: false,
  routeWhileDragging: false,
  draggableWaypoints : false,
  show: false,
}).addTo(map);

Best Answer

You need to specify the address of the routing server you're using as per the Leaflet Routing Machine tutorial: https://www.liedman.net/leaflet-routing-machine/tutorials/alternative-routers/

Routing servers are hardware-intensive and hence expensive to run. There aren't many people prepared to do it for free without restriction. You should therefore expect to set up your own instance of OSRM.

However, for very limited, non-commercial use, you may be able to use the openstreetmap.de routing server as long as you comply with their terms of use.