将onClick事件添加到由GeoJSON生成的标记时出现错误。我尝试过使用以下内容:
layer.on(‘click’, (e)=> {this.onMapClick(e)});
点击时收到的错误是
ERROR TypeError:_this.onMapClick不是函数
onMapClick(e) {
console.log(e.latlng.lng, e.latlng.lat)
}
initMap() {
this.MapData.getPoints().subscribe((res) => {
this.markers = res;
function oneachFeature(feature, layer) {
layer.bindPopup(feature.properties.description);
layer.on('click', (e) => {
this.onMapClick(e)
});
}
var myLayer = L.geoJSON(this.markers, {
pointToLayer: function(feature, latlng) {
if (feature.properties.route === "red") {
var smallIcon = new L.Icon({
iconSize: [27, 27],
iconAnchor: [13, 27],
popupAnchor: [1, -24],
iconUrl: 'assets/images/redx2.png'
});
return L.marker(latlng, {
icon: smallIcon
});
} else if (feature.properties.route === "blue") {
var smallIcon = new L.Icon({
iconSize: [27, 27],
iconAnchor: [13, 27],
popupAnchor: [1, -24],
iconUrl: 'assets/images/bluex2.png'
});
return L.marker(latlng, {
icon: smallIcon
});
}
},
onEachFeature: oneachFeature
})
myLayer.addTo(this.map);
});
}