18

我想在我的 ionic2 应用程序的传单地图中绘制一个多边形,因为我找到了传单绘制插件,但我收到此错误 TypeError: L.Control.Draw is not a constructor

我的代码看起来像这样

this.map = L
  .map("map")
  .setView(this.latLng, 13)
  .on("click", this.onMapClicked.bind(this))

L.tileLayer("https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png")
  .addTo(this.map);

this.marker = L
  .marker(this.latLng, { draggable: true })
  .on("dragend", this.onMarkerPositionChanged.bind(this))
  .addTo(this.map);

var drawnItems = new L.FeatureGroup();
this.map.addLayer(drawnItems);
console.log(drawnItems);
var drawControl = new L.Control.Draw({

  edit: {
    featureGroup: drawnItems
  }
});
this.map.addControl(drawControl);
4

4 回答 4

21

您需要添加到 head html CDN

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/leaflet.draw/0.4.2/leaflet.draw.css"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/leaflet.draw/0.4.2/leaflet.draw.js"></script>

并添加到地图{ drawControl: true }

var map = L.map('mapid', { drawControl: true }).setView([25, 25], 2);

于 2017-05-21T11:12:10.830 回答
5

可以leaflet.draw从以下地址获取最新版本

https://cdnjs.com/libraries/leaflet.draw

版本 1.0.4

https://cdnjs.cloudflare.com/ajax/libs/leaflet.draw/1.0.4/leaflet.draw.css
https://cdnjs.cloudflare.com/ajax/libs/leaflet.draw/1.0.4/leaflet.draw.js
于 2019-06-17T19:04:18.473 回答
3

嗨要将传单绘制添加到 Ionic 或 Angular 应用程序中,需要执行几个步骤。希望您已经安装了传单。

  1. npm install leaflet-draw
  2. 在angular.json文件中包含leaflet.draw.css文件。 在此处输入图像描述
  3. 在您的角度组件中导入传单绘制。 在此处输入图像描述
于 2020-11-02T17:27:47.033 回答
1

对于使用NPMTypeScript
的任何人 (基本上,问题是您缺少 Leaflet-Draw 库)

安装包和类型

$ npm i -S leaflet
$ npm i -S leaflet-draw
$ npm i -D @types/leaflet
$ npm i -D @types/leaflet-draw

更新您的tsconfig.json

{
  "compilerOptions": {
    ...
    types: [
      "leaflet",
      "leaflet-draw",
      ...
    ]
}
于 2021-09-12T14:31:00.397 回答