lrm-valhalla

0.1.1 • Public • Published

Leaflet Routing Machine / Valhalla by Mapzen

 ██▒   █▓ ▄▄▄       ██▓     ██░ ██  ▄▄▄       ██▓     ██▓    ▄▄▄      
▓██░   █▒▒████▄    ▓██▒    ▓██░ ██▒▒████▄    ▓██▒    ▓██▒   ▒████▄    
 ▓██  █▒░▒██  ▀█▄  ▒██░    ▒██▀▀██░▒██  ▀█▄  ▒██░    ▒██░   ▒██  ▀█▄  
  ▒██ █░░░██▄▄▄▄██ ▒██░    ░▓█ ░██ ░██▄▄▄▄██ ▒██░    ▒██░   ░██▄▄▄▄██ 
   ▒▀█░   ▓█   ▓██▒░██████▒░▓█▒░██▓ ▓█   ▓██▒░██████▒░██████▒▓█   ▓██▒
   ░ ▐░   ▒▒   ▓▒█░░ ▒░▓  ░ ▒ ░░▒░▒ ▒▒   ▓▒█░░ ▒░▓  ░░ ▒░▓  ░▒▒   ▓▒█░
   ░ ░░    ▒   ▒▒ ░░ ░ ▒  ░ ▒ ░▒░ ░  ▒   ▒▒ ░░ ░ ▒  ░░ ░ ▒  ░ ▒   ▒▒ ░
     ░░    ░   ▒     ░ ░    ░  ░░ ░  ░   ▒     ░ ░     ░ ░    ░   ▒   
      ░        ░  ░    ░  ░ ░  ░  ░      ░  ░    ░  ░    ░  ░     ░  ░
     ░                                                                    

Extends Leaflet Routing Machine with support for Valhalla.

Valhalla is a free, open-source routing service with dynamic run-time costing that lets you integrate automobile, bicycle, and pedestrian navigation into a web or mobile application. To use Valhalla with the Leaflet Routing Machine, install the lrm-valhalla plug-in with npm and get your free API key from mapzen.com/developers.

How to use

As with the other LRM plug-ins, you can download lrm-valhalla and insert the JavaScript file into your page right after the line where it loads Leaflet Routing Machine:

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

Also, include the stylesheet. This can replace the default leaflet-routing-machine.css provided by LRM, since the Valhalla plugin includes its own styles and icons.

<link rel="stylesheet" href="leaflet.routing.valhalla.css">

Insert your Valhalla API key and the routing mode (auto, bicycle, or pedestrian). (Note that no options are needed for formatter.)

var map = L.map('map');
 
L.Routing.control({
  // [...] See Valhalla API documentation for other options
  router: L.Routing.valhalla('<my api key>', 'auto'),
  formatter: new L.Routing.Valhalla.Formatter()
}).addTo(map);

See the Leaflet Routing Machine documentation and Valhalla API documentation for more information.

If you want to include additional costing options to help define the the route and estimated time along the path, you can pass costing option object as one of router parameter. See the Valhalla API documentation for more information on the available options for each routing mode.

L.Routing.control({
  router: L.Routing.valhalla('<my api key>', 'bicycle', {
        bicycle: {
        bicycle_type: "Road",
        cycling_speed: 17,
        use_roads: "0.1"
      }
    }),
  formatter: new L.Routing.Valhalla.Formatter(),
}).addTo(map);

Using Valhalla with npm and Browserify

Like other plug-ins, the Valhalla plug-in can be installed using npm instead of downloading the script manually:

npm install --save lrm-valhalla

Once the Valhalla plug-in is installed, update the router and formatter instances to tell the Leaflet Routing Machine to use Valhalla’s engine.

var L = require('leaflet');
require('leaflet-routing-machine');
require('lrm-valhalla');
 
var map = L.map('map');
 
L.Routing.control({
  router: L.Routing.valhalla('<my api key>', 'auto'),
  formatter: new L.Routing.Valhalla.Formatter()
}).addTo(map);

For router, insert your Valhalla API key and the routing mode (such as auto, bicycle, or pedestrian); see the Valhalla API documentation for more information. (Note that no options are needed for formatter.)

You can also change the routing mode after the router is created. Say you had different transportation options on your map and wanted to change transitmode to bicycle when that button is clicked:

var rr = L.Routing.valhalla('<my api key>', 'auto');
[...]
bikeButton.onClick: function () {
  rr.route({transitmode: "bicycle"});
}

Running a local example

If you want to run your lrm-valhalla plug-in locally for test and development purposes:

  • Install lrm-valhalla through npm or download the contents of the lrm-valhalla repo
  • get your API key from mapzen.com/developers
  • paste it into the example's index.js and choose the transportation mode (auto, bicycle, or pedestrian)
  • start a local web server (such as python -m SimpleHTTPServer or the local server you prefer)
  • go to http://localhost:8000/examples in your browser (all assets needed to run Valhalla are in the /examples folder)

Package Sidebar

Install

npm i lrm-valhalla

Weekly Downloads

3

Version

0.1.1

License

MIT

Last publish

Collaborators

  • mapzen