javascript – 更改传单上的图标

根据这个问题:https://gis.stackexchange.com/questions/54651/change-marker-icon-on-click-using-leaflet,我做了这个:

// onEachFeature
function onEachFeature(feature, layer) {
    layer.on('click', function (e) {

        // change icon
        console.log(layer.options.icon);
        e.target.setIcon(myIconReplc);

    });
}

var myIconReplc = L.Icon.extend({
    options: {
        iconUrl: "../resources/img/map/icons/orange/ambulance.png",
        iconSize: [30,35],
        shadowUrl: "../resources/img/map/icons/shadow.png",
        shadowAnchor: [8, 20],
        shadowSize: [25, 18],
        iconSize: [20, 25],
        iconAnchor: [8, 30] // horizontal puis vertical
    }
});

我有这个错误:未捕获TypeError:undefined不是一个函数

怎么了 ?

—住:http://www.monde-du-rat.fr/pmr/new.php#/carte

解决方法:

您应该像这样创建实例(在myIconReplc之前添加new),example

var myIconReplc = L.Icon.extend({
    options: {
        iconUrl: "../resources/img/map/icons/orange/ambulance.png",
        iconSize: [30,35],
        shadowUrl: "../resources/img/map/icons/shadow.png",
        shadowAnchor: [8, 20],
        shadowSize: [25, 18],
        iconSize: [20, 25],
        iconAnchor: [8, 30] // horizontal puis vertical
    }
});

layer.on('click', function (e) {
   e.target.setIcon(new myIconReplc);
});
上一篇:javascript – Leaflet .addLayer()函数失败,在控制台工作


下一篇:javascript – 如何使用Leaflet.Draw绘制多边形时更改第一个顶点的颜色?