【发布时间】:2018-02-27 12:13:03
【问题描述】:
将 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);
});
}
【问题讨论】: