Javascript 基于内部图标的传单簇颜色
我在我的传单.js地图上有图钉,图像由它们所代表的对象的状态决定。例如,在线和离线用户-在线为绿色,离线为红色。为此,我向divIcon添加了一个类,然后用css控制图像 现在,我已将标记聚类添加到地图中。我想做的是通过集群中的大多数状态来确定集群的颜色。我的第一个想法是这样做:Javascript 基于内部图标的传单簇颜色,javascript,leaflet,markerclusterer,Javascript,Leaflet,Markerclusterer,我在我的传单.js地图上有图钉,图像由它们所代表的对象的状态决定。例如,在线和离线用户-在线为绿色,离线为红色。为此,我向divIcon添加了一个类,然后用css控制图像 现在,我已将标记聚类添加到地图中。我想做的是通过集群中的大多数状态来确定集群的颜色。我的第一个想法是这样做: this.markers = L.markerClusterGroup({ iconCreateFunction: function(cluster) { // Use this somehow
this.markers = L.markerClusterGroup({
iconCreateFunction: function(cluster) {
// Use this somehow to filter through and look at the pin elements
console.log(cluster.getAllChildMarkers());
return new L.DivIcon({ html: /* ?? */ });
}
});
但不幸的是,我无法从getAllChildMarkers
返回的数组中访问HTML元素
有人知道我该怎么做吗?还是获取pin的HTML元素的方法
谢谢
编辑:
这里是我创建贴图管脚的地方(分配给我的主干模型的mapPin
属性):
我以为我可以像上面那样从返回的
getAllChildMarkers
中访问\u图标,但它似乎不在那里。您可以使用getAllChildMarkers
获取集群中的所有标记。一旦有了标记,就可以使用marker.options.icon.options.className
访问它的类。您可以使用marker.options.icon.options.html
下面的一些代码使用underline.js函数计算每个类的标记数,找到最流行的标记,并将该类用于集群标记
var markers = L.markerClusterGroup({
iconCreateFunction: function (cluster) {
var childMarkers = cluster.getAllChildMarkers();
// count how many there are of each class
var counts = _.countBy(childMarkers, function(marker) {
// class at icon level
//return marker.options.icon.options.className;
// class inside html
return $(marker.options.icon.options.html).attr('class');
});
// get the class with the highest count
var maxClass = _.invert(counts)[_.max(counts)];
// use this class in the cluster marker
return L.divIcon({ html: cluster.getChildCount(), className: maxClass });
},
});
我应该具体说明的。。随着时间的推移,我会动态地更新这些类。所以className选项实际上并没有被用来分配我正在寻找的类。我将编辑以显示我是如何更新类的。您可以使用marker.options.icon.options.html获取html,然后使用jQuery提取类属性。这只是开始时在选项中定义的html,我正在动态更改它-我需要获取更新的htmljQuery应为您提供的当前状态:var mid=$(marker.options.icon.options.html).attr('id');return$(mid.attr('class');
$(model.get('mapPin')._icon).find('.org-status').attr('class', 'org-status ' + model.getGroupStatus());
var markers = L.markerClusterGroup({
iconCreateFunction: function (cluster) {
var childMarkers = cluster.getAllChildMarkers();
// count how many there are of each class
var counts = _.countBy(childMarkers, function(marker) {
// class at icon level
//return marker.options.icon.options.className;
// class inside html
return $(marker.options.icon.options.html).attr('class');
});
// get the class with the highest count
var maxClass = _.invert(counts)[_.max(counts)];
// use this class in the cluster marker
return L.divIcon({ html: cluster.getChildCount(), className: maxClass });
},
});