問題描述
我有一堆存儲在數據庫中的多邊形.我想將它們添加到地圖中,以便可以使用 leaflet-draw 工具欄.雖然,現在多邊形已添加到地圖中,但我無法對其進行編輯.
I have a bunch of polygons which are stored in a database. I would like to add them to the map in such a way that they can be edited using the leaflet-draw toolbar. Although, now the polygons get added to the map, I am unable edit them.
我認為這是因為它們沒有添加到 layerGroup()
添加新繪制的形狀.
I think this is because they are not added to the layerGroup()
to which newly drawn shapes are added.
請幫忙.
推薦答案
你必須將你的多邊形添加到 featureGroup drawnItems
!比方說,
You have to add your polygons to the featureGroup drawnItems
! Let's say,
var polyLayers = dbArray;
是您的帶有多邊形的數據庫數組.首先使用您繪制的項目創建一個特征組:
is your database array with polygons. First create a feature group with your drawn items:
var drawnItems = new L.FeatureGroup();
并將其添加到地圖中:
map.addLayer(drawnItems);
然后你只需要從你的數據庫中迭代你的多邊形并將它們添加到drawedItems FeatureGroup
:
Then you simply need to iterate over your polygons from your database and add them to the drawnItems FeatureGroup
:
for(layer of polyLayers) {
drawnItems.addLayer(layer);
};
現在圖層已添加到地圖并可以編輯.
Now the layers are added to the map and editable.
這里有一個示例:
var drawnItems = new L.FeatureGroup();
map.addLayer(drawnItems);
var polyLayers = [];
var polygon1 = L.polygon([
[51.509, -0.08],
[51.503, -0.06],
[51.51, -0.047]
]);
polyLayers.push(polygon1)
var polygon2 = L.polygon([
[51.512642, -0.099993],
[51.520387, -0.087633],
[51.509116, -0.082483]
]);
polyLayers.push(polygon2)
// Add the layers to the drawnItems feature group
for(let layer of polyLayers) {
drawnItems.addLayer(layer);
}
這篇關于將現有的傳單多邊形添加到現有的傳單圖層的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!