leaflet.draw:检查是否存在现有图层



我有这个鼠标移动功能:

$("#mapContainer").on("mousemove", "#colorpicker"+mapNo, function(event){
//do something here
//layer.setStyle({color: color, fillColor: color}); 
});

每当我在mousemove函数中删除layer.setStyle的注释时,我都会收到一个错误"layer is not defined">,所以我希望在映射中已经存在层时调用mousemove函数。我在定义层的地方有这个功能:

function drawCreated(e) {
type = e.layerType,
layer = e.layer;
layer.addTo(drawnItems); 
console.log(type, ' drawn', layer);
//for drawing
if (type === 'circle') {
var theCenterPt = layer.getLatLng();
var theRadius = layer.getRadius();
var center = [theCenterPt.lng,theCenterPt.lat]; 
console.log(center);
points['lng'] = theCenterPt.lng;
points['lat'] = theCenterPt.lat;
points['radius'] = theRadius;
console.log(points);
drawnItems.addLayer(layer);
//drawnItems.setStyle({color: color, fillColor: color});
} 
if(map.hasLayer(layer)){
console.log("true");
//must call the mousemove function here or mousemove will work when there is a layer
}else{
console.log("false");
}

}

看看我的例子:

https://jsfiddle.net/falkedesign/95g08z71/10/

你必须用你的图层替换"圆圈"。

// https://iro.js.org/guide.html#color-picker-events
colorPicker.on(["color:init", "color:change"], function(color){
circle.setStyle({fillColor: color.hexString});
});

要说什么在你的代码中不起作用,我需要一个有效的例子。也许你可以创建一个小提琴或将你的代码上传到网站。

相关内容

  • 没有找到相关文章

最新更新