2016-08-13 673 views
5

我想在我的ionic2應用程序中的小冊子地圖上畫一個多邊形,爲此我找到了leaflet-draw插件,但是出現此錯誤 TypeError:L.Control .Draw是不是構造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); 
+2

['Leaflet.draw'](https://github.com/Leaflet/Leaflet.draw)是一個插件。它不是Leaflet核心的一部分。你是否按照[安裝說明](https://github.com/Leaflet/Leaflet.draw#install)並且包含了適當的JavaScript文件? – cartant

+0

我確實按照安裝說明進行操作。但爲了能夠從我的腳本代碼中使用它,我安裝了leaflet-draw的類型,並通過參考路徑包含在我的類中:/// 。有什麼我在這裏錯過 – Nishant

+0

當我從我的瀏覽器中調試它時,我看到L.Control是一個在小冊子typescript文件中定義的函數,而在leaflet -print typescript文件中,「Control」是一個名稱空間,裏面其中繪製定義,所以我認爲這段代碼是不是指的是正確的'控制' – Nishant

回答

2

您需要添加頭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);

+0

自己錯過了!謝謝。我認爲問題在於文檔沒有指定您需要導入腳本,對我來說它好像已經包含在leaflet.js中 –