How to import leaflet-routing-machine into an Ionic2 project?

我怕爱的太早我们不能终老 提交于 2019-12-11 05:55:01

问题


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.


回答1:


We solved this problem by adding the following line before declaring our component.

declare var L: any;

myclass.component.ts

import { Component, OnInit } from '@angular/core';
...

// Leaflet and Leaflet Routing Machine have been installed with npm
import 'leaflet-routing-machine';
import 'leaflet-easybutton';

// Add this line to remove typescript errors
declare var L: any;

@Component({
  ...
})
export class MyClass extends OnInit {
  ...

  constructor(...) {
    ...
  }

  ngOnInit() {

    ...

    // The example snippet is now working
    L.Routing.control({
      waypoints: [
        L.latLng(57.74, 11.94),
        L.latLng(57.6792, 11.949)
      ]
    }).addTo(myMap);

    ...

  }

  ...

}

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.




回答2:


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 with L.routing.control? (pay attention to the starting L)




回答3:


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:

import leaflet from 'leaflet'; 
import 'leaflet-routing-machine'; 
import 'leaflet-easybutton';

4) declare L declare var L:any;

add code content

var maptt = leaflet.map('mapId2');
leaflet.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', { 
    attribution: '©suwitbb' 
}).addTo(maptt);

leaflet.Routing.control({ 
    waypoints: [ 
        leaflet.latLng(57.74, 11.94), 
        leaflet.latLng(57.6792, 11.949) 
    ], routeWhileDragging: true 
}).addTo(maptt);

6) add tag to view html




回答4:


In Ionic 4, try it.

1) npm i leaflet.

2) npm i leaflet-routing-machine.

3) import in "index.html" the js and css .

<!-- Leaflet CDN JS and Css-->
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.4.0/dist/leaflet.css" integrity="sha512-puBpdR0798OZvTTbP4A8Ix/l+A4dHDD0DGqYW6RQ+9jxkRFclaxxQb/SJAWZfWAkuyeQUytO7+7N4QKrDh+drA==" crossorigin="" />
<script src="https://unpkg.com/leaflet@1.4.0/dist/leaflet.js" integrity="sha512-QVftwZFqvtRNi0ZyCtsznlKSWOStnDORoefr1enyq5mVL4tmKB3S/EnC3rRJcxCPavG10IcrVGSmPh6Qw5lwrg==" crossorigin=""></script>

<!-- Leaflet Machine. path: 'node_modules/leaflet-routing-machine/dist/leaflet-routing-machine.js' the same to css-->
<link rel="stylesheet" href="./assets/css/leaflet-routing-machine.css">
<script src="./assets/js/leaflet-routing-machine.js"></script>

4) in your_component.ts

import Leaflet from 'leaflet';
import 'leaflet-routing-machine';

// Add this line to remove typescript errors
declare var L: any;

export class FullScreenMapPage implements OnInit { 
  // mapa
  mapa: any;

  ionViewDidEnter() {
    this.drawMap();
  }

drawMap(): void {
    // marcador del mapa
    const markerGroup = Leaflet.featureGroup();
    const marker = Leaflet.marker([lat, lng]);

    this.mapa = Leaflet.map('map').setView([lat, lng], 11);

    Leaflet.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
    attribution: '© Open Street Map',
    zoom: 8,
    maxZoom: 18,
    minZoom: 4,
    minResolution: 4891.96981025128,
    maxResolution: 39135.75848201024,
    doubleClickZoom: true,
    center: [lat, lng]
    }).addTo(this.mapa);

    Leaflet.Routing.control({
      waypoints: [
        Leaflet.latLng(PointA_lat, PointB_lng),
        Leaflet.latLng(PointB_lat, PointB_lng)
      ], routeWhileDragging: true
    }).addTo(this.mapa);

  }

}



来源:https://stackoverflow.com/questions/42380525/how-to-import-leaflet-routing-machine-into-an-ionic2-project

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!