接下來有幾天要來介紹 Leaflet.draw 套件的使用。
import "leaflet-draw/dist/leaflet.draw";
import "leaflet-draw/dist/leaflet.draw.css";
onMounted(() => {
const drawnItems = new L.FeatureGroup();
map.addLayer(drawnItems);
})
const drawControl = new L.Control.Draw({
edit: {
featureGroup: drawnItems,
}
});
map.addControl(drawControl);
繪圖工具:
const drawControl = new L.Control.Draw({
edit: {
featureGroup: drawnItems,
},
draw: false,
});
draw: {
polygon: false,
marker: false,
},
以下官方提供兩種寫法
http://leaflet.github.io/Leaflet.draw/docs/leaflet-draw-latest.html#l-draw-event
第一種寫法 :
這裡監聽 created 事件,透過 e.layer 將繪製後的圖形加入地圖圖層
map.on(L.Draw.Event.CREATED, function (e) {
console.log(e);
const layer = e.layer;
map.addLayer(layer);
});
第二種寫法 :
map.on("draw:created", function (e) {
const layer = e.layer;
map.addLayer(layer);
console.log(e);
});