【问题标题】:leaflet.draw: check there is an existing layerLeaflet.draw:检查是否存在现有图层
【发布时间】:2019-11-13 02:39:05
【问题描述】:

我有这个鼠标移动功能:

$("#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");
        }


}

【问题讨论】:

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

标签: javascript php codeigniter leaflet leaflet.draw


【解决方案1】:

看看我的例子:

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});
});

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

【讨论】:

    猜你喜欢
    • 2014-06-08
    • 1970-01-01
    • 2011-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多