Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to include leaflet-routing-machine into angular 2 typescript webpack application

I used this seed to get started with Angular 2 Typescript and Webpack: https://github.com/haoliangyu/angular2-leaflet-starter.

I'm new to most of the used tools and technologies (Angular 2, Typescript, Webpack). While I understand more and more about these it seems that I still haven't grasped how third-party non-typed JS-libraries are included:

I would like to include the leaflet-routing-machine.js into my project. To my knowledge, while there do exist typings for leaflet, there do not exist typings for leaflet-routing-machine.

I installed the package via npm install and added the required quick-start code snipped to display a route.

map.service.ts

/// <reference path="../../typings/leaflet/leaflet.d.ts"/>

import {Injectable} from 'angular2/core';
import {Map} from 'leaflet';

Injectable()
export class MapService {
  map: Map; // holds reference to the normal leaflet map object

  showRoute(){
        L.Routing.control({
        waypoints: [
            L.latLng(47.569198, 7.5874886),
            L.latLng(47.5685418, 7.5886755)
        ]
    }).addTo(this.map);

  }

}

The error I get on npm start is:

ERROR in ./app/services/map.service.ts
(56,11): error TS2339: Property 'Routing' does not exist on type 'typeof L'.

As I understand it I shouldn't include the JS file in the index.html as this should be automatically done by webpack. Next to the fact that I'm generally unsure how to include third-party libraries without typings (answers like this did not help) it seems my case is a bit different because the L Object is standard leaflet and does not know the Routing property. Which I somehow understand, since I don't see how the routing machine library extends the leaflet library.

Any suggestions?

like image 738
n0daft Avatar asked Nov 08 '22 14:11

n0daft


1 Answers

I haven't worked with either Angular 2, nor TypeScript, but I suspect that TypeScript doesn't like that LRM attaches itself to the L object/namespace.

Note that LRM also exports itself as a normal CommonJS module, so you can do something like this instead of using L.Routing.Control:

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

var map = L.map(...);
var routingControl = Routing.control({ ... });
like image 129
Liedman Avatar answered Nov 15 '22 07:11

Liedman