【问题标题】:TypeError: L.Control.Draw is not a constructorTypeError: L.Control.Draw 不是构造函数
【发布时间】:2016-12-20 04:22:24
【问题描述】:

我想在我的 ionic2 应用程序的传单地图中绘制一个多边形,因为我找到了传单绘制插件,但我收到了这个错误 TypeError: L.Control.Draw 不是构造函数

我的代码是这样的

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);

【问题讨论】:

  • Leaflet.draw 是一个插件。它不是 Leaflet 核心的一部分。您是否关注了installation instructions 并包含了相应的 JavaScript 文件?
  • 我确实按照安装说明进行操作。但是为了能够从我的打字稿代码中使用它,我安装了传单绘制的类型并通过参考路径包含在我的类中:/// 。我有什么遗漏的吗
  • 另外,当我从我的 chrome 浏览器调试它时,我看到 L.Control 是在传单打字稿文件中定义的函数,而在传单绘制打字稿文件中,'Control' 是一个命名空间,其中Draw 是定义的,所以我认为这段代码不是指当前的'Control'
  • 您发布的代码看起来不错。你能展示你在哪里/如何导入 Leaflet.draw 吗?您提到的错误看起来像是运行时错误 - 不是 TypeScript 错误 - 对吧?
  • 我会尝试让 Leaflet 位在没有 TypeScript 的情况下工作。一旦你有基础工作,就添加 TypeScript。

标签: typescript ionic2 leaflet.draw


【解决方案1】:

你需要添加到head html CDN's

<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);

【讨论】:

  • 我自己错过了!谢谢。我认为问题在于文档没有指定您需要导入脚本,对我来说它似乎已经包含在leaflet.js中
【解决方案2】:

您可以从以下地址获取最新版本的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

【讨论】:

    【解决方案3】:

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

    1. npm install leaflet-draw
    2. 在 angular.json 文件中包含 leaflet.draw.css 文件。
    3. 在您的角度组件中导入leaflet-draw。

    【讨论】:

      【解决方案4】:

      对于使用 NPM 和 TypeScript
      的任何人 (基本上,问题在于您缺少 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",
            ...
          ]
      }
      

      【讨论】:

        猜你喜欢
        • 2019-08-14
        • 2018-06-27
        • 2016-04-16
        • 2021-11-26
        • 2019-10-27
        • 2018-07-18
        • 2020-10-04
        相关资源
        最近更新 更多