Beh*_*ted 2 javascript leaflet leaflet.draw
我想从数据库中检索多边形数据,然后对其进行编辑。我可以检索多边形(存储为 geojson),但不能使它们可编辑。我怎样才能做到这一点?
var drawnItems = new L.FeatureGroup();
map.addLayer(drawnItems);
var drawControl = new L.Control.Draw({
edit: {
featureGroup: drawnItems
}
});
map.addControl(drawControl);
map.on('draw:created',function(e) {
e.layer.addTo(drawnItems);
});
L.control.layers(baseLayers).addTo(map);
var oldPolygon = null;
function showOnMap(rowid){
if(oldPolygon != null){
map.removeLayer(oldPolygon);
}
$.get("testdbextract.php?show="+rowid,function(data){
var newPolygon = L.geoJson(data);
newPolygon.addTo(drawnItems); // or drawnItems.addLayer(newPolygon);
oldPolygon = newPolygon;
});
}
Run Code Online (Sandbox Code Playgroud)
在您的示例中,您需要解析收到的 geojson 数据,创建图层并初始化drawItems
为了更容易,你可以像这样创建一个GeoJson层:
// Create a GeoJson layer without adding it to the map
L.geoJson(geojsonFeatures, {
onEachFeature: onEachFeature
});
// Take advantage of the onEachFeature callback to initialize drawnItems
function onEachFeature(feature, layer) {
drawnItems.addLayer(layer);
}
Run Code Online (Sandbox Code Playgroud)
这是一个例子
在您的代码中,它可以这样使用
$.get("testdbextract.php?show="+rowid,function(data){
L.geoJson(data, {
onEachFeature: onEachFeature
});
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4090 次 |
| 最近记录: |