Javascript 使用群集时,为什么单击事件上的传单标记不起作用?

Javascript 使用群集时,为什么单击事件上的传单标记不起作用?,javascript,leaflet,event-propagation,Javascript,Leaflet,Event Propagation,正如JSFIDLE所示,单个标记可以绑定到单击事件,而集群标记不能: 为什么,以及如何使其工作 我的Javascript代码: //An extract of address points from the LINZ bulk extract: http://www.linz.govt.nz/survey-titles/landonline-data/landonline-bde //Should be this data set: http://data.linz.govt.nz/#/laye

正如JSFIDLE所示,单个标记可以绑定到单击事件,而集群标记不能:

为什么,以及如何使其工作

我的Javascript代码:

//An extract of address points from the LINZ bulk extract: http://www.linz.govt.nz/survey-titles/landonline-data/landonline-bde
//Should be this data set: http://data.linz.govt.nz/#/layer/779-nz-street-address-electoral/
var addressPoints = [[-37.793167, 175.211862,"the one"],
    [-37.8210922667, 175.2209316333, "2"],
    [-37.8210819833, 175.2213903167, "3"]
    /* many more coordinates, see JSFiddle link */
]; 

var tiles = L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
    maxZoom: 18,
    attribution: '&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors, Points &copy 2012 LINZ'
}),
    latlng = L.latLng(-37.82, 175.24);

var map = L.map('map', {center: latlng, zoom: 13, layers: [tiles]});

var markers = L.markerClusterGroup();

for (var i = 0; i < addressPoints.length; i++) {
    var a = addressPoints[i];
    var title = a[2];
    var marker = L.marker(new L.LatLng(a[0], a[1]), {title: title});
    marker.bindPopup(title);
    markers.addLayer(marker);
}

map.addLayer(markers);

$( ".leaflet-marker-pane img").on( "click", function() {
    console.log("click");
});
//从LINZ批量提取中提取地址点:http://www.linz.govt.nz/survey-titles/landonline-data/landonline-bde
//此数据集应为:http://data.linz.govt.nz/#/layer/779-新西兰街道地址/
var addressPoints=[-37.793167175.211862,“一个”],
[-37.8210922667, 175.2209316333, "2"],
[-37.8210819833, 175.2213903167, "3"]
/*更多坐标,请参见JSFIDLE链接*/
]; 
var tiles=L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png'{
maxZoom:18,
属性:“©;贡献者、积分和副本2012 LINZ”
}),
latlng=L.latlng(-37.82175.24);
var map=L.map('map',{center:latlng,zoom:13,layers:[tiles]});
var markers=L.markerClusterGroup();
对于(变量i=0;i
HTML:

`

您需要收听
群集单击事件,检查关于事件:

markers.on('clusterclick', function (a) {
    console.log('cluster with ' + a.layer.getAllChildMarkers().length + ' markers in it');
});

jsiddle:

您需要监听
集群。单击
事件,检查关于事件:

markers.on('clusterclick', function (a) {
    console.log('cluster with ' + a.layer.getAllChildMarkers().length + ' markers in it');
});

jsIDLE:

如何获取单个标记的弹出内容?@Matoeil您可以通过两种方式实现:
var a=L.marker([51.5,-0.09]).addTo(map.bindPopup('stuff');console.log(一个弹出的内容)(黑客的方式)或
var b=L.marker([51.5,-0.095],{someAttribute:'hello'}).addTo(map.bindpoop(“stuff”);console.log(b.options.someAttribute)
(更好)如何获取单个标记的弹出内容?@Matoeil您可以通过两种方式实现:
var a=L.marker([51.5,-0.09]).addTo(map.bindpoppup('stuff');console.log(一个弹出的内容)(黑客的方式)或
var b=L.marker([51.5,-0.095],{someAttribute:'hello'}).addTo(map.bindpoop(“stuff”);console.log(b.options.someAttribute)
(更好)