Fork me on GitHub

Leaflet Routing Machine

Easy, flexible routing for Leaflet

« Back to tutorials

GraphHopper, Mapbox, Valhalla and other routing software

Behind the UI, Leaflet Routing Machine queries OSRM’s demo servers each time the route is recalculated, unless you tell it to use something else. This is great for a demo, but not for production: the service is free but comes with a usage policy and without any warranties or SLA. For production use, or for any use case were you need control over how routes are calculated (like other means of transport, like bike or foot), you need to use another server, be it OSRM or some other software. This tutorial tells you about the different options.

Using your own OSRM server

If it’s just the configuration and/or reliablity of the free servers that you need to control yourself, you can quite easily set up your own OSRM server. When you have it running, using it with Leaflet Routing Machineis a matter of telling telling it where the server is located:

    serviceUrl: 'http://my-osrm/route/v1'

Under the hood, this option will be passed along to the L.Routing.OSRM instance that is implicitly created with your control; this is the control’s router.

You can also, more explicitly, hand the router instance to use directly to the control:

    router: L.Routing.osrmv1({
        serviceUrl: 'http://my-osrm/route/v1'

Mapbox directions API

Another popular alternative for routing is to use Mapbox Directions API. The directions API is a part of Mapbox’s platform, so you can’t install your own version of it: it’s installed, hosted and supported by Mapbox. You’ll need a Mapbox account to use this feature.

First, you need an API access token to identify that it’s your account that should be billed for the routing.

Once you have an access token, support for Mapbox directions is already built into Leaflet Routing Machine, you just need to specify that you want to use the Mapbox router:

    router: L.Routing.mapbox('your-access-token-here')


Another popular, open source routing software is GraphHopper. It’s fast, works well with OpenStreetMap data and runs on a lot of platforms (even on Android).

To use switch to using GraphHopper with Leaflet Routing Machine, you need to use the plugin lrm-graphhopper. You can download lrm-graphhopper and insert the JavaScript file into your page right after where it loads Leaflet Routing Machine:

<script src="leaflet-routing-machine.js"></script>
<script src="lrm-graphhopper.js"></script>

Now you need to give the GraphHopper router to Leaflet Routing Machine’s control:

    router: L.Routing.graphHopper('apiKey');

Like OSRM, the GraphHopper router will by default use GraphHopper’s public servers. You need to register and get an API key to use these, and you need to give that API key to the router instance.

If you set up your own GraphHopper server, you don’t need the API key, but instead configure the address of your server:

    router: L.Routing.graphHopper(undefined /* no api key */, {
        serviceUrl: 'http://my-graphhopper/api/v1/route'

Using GraphHopper with npm and Browserify

If you’re lucky enough to work with Browserify, the plugin can be installed through npm instead of downloading the script manually:

npm install --save lrm-graphhopper

And later required into your source. Note that the plugin, like many Leaflet plugins, will tack itself on to the main Leaflet object, L, so there’s no explicit need to save the result you’ll get back from the require statement, although you can if that is how you structure your code.

var L = require('leaflet');
require('leaflet-routing-machine'); // Adds L.Routing onto L
require('lrm-graphhopper'); // Adds L.Routing.GraphHopper onto L.Routing

    router: L.Routing.graphHopper('api-key');

Mapzen Valhalla

Mapzen Valhalla is supported through Mapzen’s own plugin lrm-valhalla.

Download prebuilt files:

Load this file with a &lt;script&gt; tag in your page, after Leaflet and Leaflet Routing Machine has been loaded.

Or, to use with for example Browserify:

npm install --save lrm-valhalla

See the lrm-valhalla project page for info and docs on using the plug-in as well as the Valhalla API reference.

TomTom Online Routing API

Mathias Rohnstock has written a plugin for Leaflet Routing Machine: lrm-tomtom. Check it out!

Using other routers

You can also build support for other routing software. This way, you can for example use Leaflet Routing Machine with proprietary routing software of your choice.

The basic requirement is that the router class must implement the IRouter interface, so that Leaflet Routing Machine knows how to communicate with your router. The interface is pretty simple and contains a single method:

router.route(waypoints, callback, context, options)

waypoints is the array of waypoints the route should pass, and contains at least two elements when route is called; each element will have at least a latLng property, containing a L.LatLng that describes the waypoints location, optionally it will also have a name as string and options.

Since routing will most likely be an asynchronous operation, the route method isn’t expected to return a result, but rather call the provided callback function when the result has been calculated. The callback takes two arguments, like the Node.js convention: the first argument is an error, and is set to a falsy value if no error occured; in this case the other argument is the result, an array of route alternatives with at least one element.

A route is defined by the IRoute interface, and should among many things contain a description of the route’s geometry, the instructions for the route, as well as summaries of the total distance and expected time for the route.

By implementing these interfaces, you will get all the other functionality of Leaflet Routing Machine for free.

Preventing excessive requests (and bills)

Several routing services bill you per routing requests, or take very long time to respond to a request. In these scenarios, it might be required to reduce the number of routing requests made by Leaflet Routing Machine.

The first trick, which will makes the largest reduction in number of requests, is to disable the routeWhileDragging option for the control, if you have it enabled. While it definitely looks cool and gives great feedback to the user, it costs a lot of requests.

Another, more dramatic step, is to disable automatic routing altogether. By default, the control will make a routing request every time a waypoint is changed (when a waypoint marker is released after dragging, when an address is updated in the input fields, etc.). However, you can disable this, and only make the control route when you explicitly ask it to. This can be achieved by setting the option autoRoute to false. When doing so, the control will only calculate a route when the control’s route method is called. The method takes no arguments, but routes through the currently selected waypoints. You will need to implement a way (a button, for example), that calls the route method.

Copyright © 2015 Per Liedman, released under ISC License. Logo by Alexey Ivanov, released under Creative Commons CC-BY 3.0.

Sorry! The OSRM demo server appears down, or a network error occured.

This prevents Leaflet Routing Machine from displaying a route. The demo server will hopefully be back up soon. Please do not report this as an issue, Leaflet Routing Machine has no control over the demo server.

In production, set up your own OSRM instance, or pay a service provider.