Giter Site home page Giter Site logo

rfriberg / lrm-mapzen Goto Github PK

View Code? Open in Web Editor NEW

This project forked from nextzen/lrm-mapzen

0.0 2.0 0.0 462 KB

Support for Mapzen Turn-by-Turn routing in Leaflet Routing Machine

Home Page: https://mapzen.com/projects/turn-by-turn/

License: Other

Shell 0.76% JavaScript 26.21% CSS 73.03%

lrm-mapzen's Introduction

npm version

Add support for Mapzen Turn-by-Turn routing in Leaflet Routing Machine

Mapzen Turn-by-Turn is an open-source routing service with dynamic run-time costing that lets you integrate automobile, bicycle, pedestrian, or multimodal navigation into a web or mobile application.

Use this plug-in to create a Leaflet map that has a route line between map locations (also known as waypoints), a text narrative of maneuvers to perform on the route, distances along your route and estimated travel times, and the ability to drag the route start and endpoints to get a different path.

With lrm-mapzen, Mapzen Turn-by-Turn is substituted for the default routing service used in Leaflet Routing Machine. You need to install the lrm-mapzen plug-in and obtain an API key from mapzen.com/developers.

Get started with lrm-mapzen

Follow along with this tutorial to build a map with lrm-mapzen.

Download lrm-mapzen and insert a reference to 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-mapzen.js"></script>
[...]

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

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

Insert your Mapzen API key for the placeholder text (mapzen-xxxxxx) and a routing options object to at least include the costing mode (auto, bicycle, pedestrian, or multimodal). Note that no additional options are needed for formatter.

var map = L.map('map');

L.Routing.control({
  // [...] See MapzenTurn-by-Turn API documentation for other options
  router: L.Routing.mapzen('mapzen-xxxxxx', {
    costing:'auto'
  }),
  formatter: new L.Routing.mapzenFormatter()
}).addTo(map);

If you want to include additional costing options to help define the the route and estimated time along the path, you can pass a costing option object as one of router parameters. You can also include options for directions in order to change the language, distance units or narrative guidance production. See the Mapzen Turn-by-Turn API documentation for more information on the available options.

L.Routing.control({
  router: L.Routing.mapzen('mapzen-xxxxxx', {
    costing: "bicycle",
    costing_options: {
      bicycle: {
        bicycle_type: "Road",
        cycling_speed: "17.0",
        use_roads: "0.1"
      },
    },
    directions_options: {
      language: 'en-US'
    }
  }),
  formatter: new L.Routing.mapzenFormatter(),
}).addTo(map);

With themultimodal costing mode, you can set costing options for preferences for taking buses or rail lines or having to make transfers. If you include a date_time, you can request a transit route departing at a certain time, for example. See the Mapzen Turn-by-Turn API documentation for more information on the available options.

L.Routing.control({
  router: L.Routing.mapzen('mapzen-xxxxxx', {
    // you need to pass mapzenLine as routeLine to router to see subroutes of transit routing.
    // you can skip routeLine if you don't want to use subroutes.
    routeLine: function (route, options) { return L.Routing.mapzenLine(route, options); },
    costing: "multimodal",
    date_time: {
      type: 1,
      value: "2016-05-10T08:00"
    }
  }),
  formatter: new L.Routing.mapzenFormatter(),
}).addTo(map);

See the Leaflet Routing Machine documentation and Mapzen Turn-by-Turn API documentation for more information.

Use lrm-mapzen with npm and Browserify

The Mapzen plug-in can be installed using npm instead of downloading the script manually:

npm install --save lrm-mapzen

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

var L = require('leaflet');
require('leaflet-routing-machine');
require('lrm-mapzen');

var map = L.map('map');

L.Routing.control({
  router: L.Routing.mapzen('mapzen-xxxxxx', {costing:'auto'}),
  formatter: new L.Routing.mapzenFormatter()
}).addTo(map);

For router, insert your Mapzen Turn-by-Turn API key and a routing options object to at least include the routing mode (such as auto, bicycle, pedestrian, or multimodal); see the Mapzen Turn-by-Turn API documentation for more information. (Note that no additional options are needed for formatter.)

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

var rr = L.Routing.mapzen('mapzen-xxxxxx', {costing:'auto'});
[...]
bikeButton.onClick: function () {
  rr.route({costing: "bicycle"});
}

Run a local example

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

  • Install lrm-mapzen through npm or download the contents of the lrm-mapzen repo
  • Get your API key from mapzen.com/developers
  • Paste it into the example's index.js and choose the transportation/costing 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 Mapzen are in the /examples folder)

lrm-mapzen's People

Contributors

alex-outdoor avatar easherma avatar eelcocramer avatar glennon avatar hanbyul-here avatar jessecrocker avatar kdiluca avatar kevinkreiser avatar louh avatar luizfonseca avatar migurski avatar perliedman avatar rmglennon avatar skorasaurus avatar stevendlander avatar tim-field avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.