根据这个问题: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);
});