Javascript 传单.draw:检查是否存在现有图层

Javascript 传单.draw:检查是否存在现有图层,javascript,php,codeigniter,leaflet,leaflet.draw,Javascript,Php,Codeigniter,Leaflet,Leaflet.draw,我有一个mousemove函数: $("#mapContainer").on("mousemove", "#colorpicker"+mapNo, function(event){ //do something here //layer.setStyle({color: color, fillColor: color}); }); 每当我在mousemove函数中删除layer.setStyle的注释时,都会出现一个错误“layer is not d

我有一个mousemove函数:

$("#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://iro.js.org/guide.html#color-picker-events
colorPicker.on(["color:init", "color:change"], function(color){
      circle.setStyle({fillColor: color.hexString});
});

要说哪些代码不工作,我需要一个工作示例。也许您可以创建一个小提琴或将代码上载到网站。

我不确定这是否是XY问题-您能描述一下您试图实现的行为吗?我想使用mousemove功能更改图层的颜色。但是我总是收到一个错误,说图层没有定义,因为地图中没有图层,这就是为什么我想检查,如果有图层,mousemove将工作,但如果没有,它将不工作。我不明白你的目标是什么。您是希望在移动图层时更改图层的颜色,还是希望在鼠标移动时更改图层的颜色?是所有层都改变颜色还是只改变某些层?@FalkeDesign我想在颜色选择器中移动光标时改变层的颜色。它应该改变我画的某个图层。