I don't quite get how to import LRM into a ts file. After installing via npm install leaflet-routing-machine
, I defined Routing like this:
var Routing = require('leaflet-routing-machine');
var newRoute = Routing.control({Options});
which didn't help me and I got:
Error caused by: Routing.control is not a function
Here is my Ionic information:
Cordova CLI: 6.4.0
Ionic Framework Version: 2.0.1
Ionic CLI Version: 2.2.1
Ionic App Lib Version: 2.2.0
Ionic App Scripts Version: 1.1.0
Node Version: v6.3.1
BTW, I don't have any problem with leaflet itself.
Not sure if Leaflet Routing Machine plugin directly exports itself.
Normally it should at least have the side effect of attaching to the
L
global namespace.After calling
require('leaflet-routing-machine')
, have you tried instantiating a control withL.routing.control
? (pay attention to the startingL
)ok. how to use it!
1)
npm i leaflet-routing-machine https://www.npmjs.com/package/leaflet-routing-machine
2)
npm i leaflet-easybutton https://www.npmjs.com/package/leaflet-easybutton
3) import module in the work page:
4) declare L
declare var L:any;
add code content
6) add tag to view html
We solved this problem by adding the following line before declaring our component.
declare var L: any;
myclass.component.ts
As mentioned in this post, typescript seems to be causing problems to add properties to the global L object of Leaflet, but in our case declaring L of type any was enough to make it work.
In Ionic 4, try it.