我正在使用https://github.com/Leaflet/Leaflet.draw插件,我试图找出如何检索已编辑图层的图层类型.
在draw:created事件上,我有layer和layerType,但是在draw:edited(保存所有编辑时触发)我得到一个已编辑的图层列表.
该抽奖:创建活动
map.on('draw:created', function (e) {
var type = e.layerType,
layer = e.layer;
if (type === 'marker') {
// Do marker specific actions
}
// Do whatever else you need to. (save to db, add to map etc)
map.addLayer(layer);
});
Run Code Online (Sandbox Code Playgroud)
该抽奖:编辑事件
map.on('draw:edited', function (e) {
var layers = e.layers;
layers.eachLayer(function (layer) {
//do stuff, but I can't check which type I'm working with
// the layer parameter doesn't mention its type
});
});
Run Code Online (Sandbox Code Playgroud)
感谢您的时间.
Pwn*_*rus 35
你可以使用instanceof (这里的文档).
map.on('draw:edited', function (e) {
var layers = e.layers;
layers.eachLayer(function (layer) {
if (layer instanceof L.Marker){
//Do marker specific actions here
}
});
});
Run Code Online (Sandbox Code Playgroud)
mar*_*nas 24
使用时要非常小心instanceof.Leaflet.draw插件使用标准Leaflet的L.Rectangle.Leaflet的矩形扩展了Polygon.多边形延伸折线.因此,某些形状可能会使用此方法给您意想不到的结果.
var drawnItems = new L.FeatureGroup();
map.addLayer(drawnItems);
... add layers to drawnItems ...
// Iterate through the layers
drawnItems.eachLayer(function(layer) {
if (layer instanceof L.Rectangle) {
console.log('im an instance of L rectangle');
}
if (layer instanceof L.Polygon) {
console.log('im an instance of L polygon');
}
if (layer instanceof L.Polyline) {
console.log('im an instance of L polyline');
}
});
Run Code Online (Sandbox Code Playgroud)
var getShapeType = function(layer) {
if (layer instanceof L.Circle) {
return 'circle';
}
if (layer instanceof L.Marker) {
return 'marker';
}
if ((layer instanceof L.Polyline) && ! (layer instanceof L.Polygon)) {
return 'polyline';
}
if ((layer instanceof L.Polygon) && ! (layer instanceof L.Rectangle)) {
return 'polygon';
}
if (layer instanceof L.Rectangle) {
return 'rectangle';
}
};
Run Code Online (Sandbox Code Playgroud)