Leaflet Routing Machine is an amazing plugin developed by @perliedman in order to draw routes on a leaflet map. TypeScript definition helps you to use this plugin in your typescript projects.
- Install leaflet
npm i leaflet
- Install TypeScript definition of leaflet
npm i --save-dev @types/leaflet
- Install leaflet routing machine
npm i leaflet-routing-machine
- Install TypeScript definition of leaflet routing machine
npm i --save-dev @types/leaflet-routing-machine
- Import leaflet.css & leaflet-routing-machine.css into the project
- Open angular-cli.json
- Add leaflet & leaflet-routing-machine css file into style attribute.
"styles": [ "../node_modules/leaflet/dist/leaflet.css", "../node_modules/leaflet-routing-machine/dist/leaflet-routing-machine.css" ]
- Import leaflet into the component
- Add below import statements top of your component
import * as L from 'leaflet'; import 'leaflet-routing-machine';
import * as L from 'leaflet';
import 'leaflet-routing-machine';
const map: L.Map = L.map('map-container');
L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {
attribution: '© <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);
L.Routing.control({
router: L.Routing.osrmv1({
serviceUrl: `http://router.project-osrm.org/route/v1/`
}),
showAlternatives: true,
lineOptions: {styles: [{color: '#242c81', weight: 7}]},
fitSelectedRoutes: false,
altLineOptions: {styles: [{color: '#ed6852', weight: 7}]},
show: false,
routeWhileDragging: true,
waypoints: [
L.latLng(57.74, 11.94),
L.latLng(57.6792, 11.949)
]
}).addTo(map);
This type definition is provided as a fundamental solution for configure and display a route by using leaflet routine machine. This definition may not be enough for advanced use cases such as Interaction, Support for other routing machine back ends. If you need any support with such scenario feel free to contact me via [email protected].