简体   繁体   English

如何更改传单标记聚类扩展的单个标记节点图标?

[英]How can i change the single marker node icon of the leaflet marker clustering extension?

I would like to use a custom icon for the single value markers of the leaflet marker clustering extension.我想为传单标记聚类扩展的单值标记使用自定义图标。 Changing the default icon of leaflet with the following code snippet doesn't work:使用以下代码片段更改传单的默认图标不起作用:

var newIcon = L.Icon.Default.extend({
            options: {
                    iconUrl: 'new_icon_location.png' 
            }
         });

Marker clustering extension: https://github.com/Leaflet/Leaflet.markercluster标记聚类扩展: https : //github.com/Leaflet/Leaflet.markercluster

As for creating an icon in Leaflet, the syntax would be:至于在 Leaflet 中创建图标,语法是:

var newIcon = L.icon({
    iconUrl: 'new_icon_location.png'
});

If you want all markers to use that icon instead of the default one (ie L.Icon.Default as you figured out), you should rather override the icon option of L.Marker class (before you start instantiating markers, of course):如果您希望所有标记都使用该图标而不是默认图标(即您发现的L.Icon.Default ),您应该覆盖L.Marker类的icon选项(当然,在您开始实例化标记之前):

L.Marker.mergeOptions({
    icon: newIcon
});

As for changing the default icons used by Leaflet.markercluster plugin, you should use option iconCreateFunction :至于更改 Leaflet.markercluster 插件使用的默认图标,您应该使用选项iconCreateFunction

var mcg = L.markerClusterGroup({
    iconCreateFunction: function (cluster) {
        // create an icon, possibly based on cluster properties.
        return clusterIcon;
    }
});

Now I am not exactly sure of what you intend to do when you say " use a custom icon for the single value markers of the leaflet marker clustering ".现在,当您说“为传单标记聚类的单值标记使用自定义图标”时,我不确定您打算做什么。 I guess you use option singleMarkerMode and you would like a specific icon to be applied to "clusters of size 1" (which are in fact normal markers which icon is overriden by Marker Cluster plugin)?我猜您使用选项singleMarkerMode并且您希望将特定图标应用于“大小为 1 的集群”(实际上是正常标记,该图标被标记集群插件覆盖)?

In that case, a simple solution would have been not to use the singleMarkerMode option, and instead to make all your individual markers use that specific icon.在这种情况下,一个简单的解决方案是不使用singleMarkerMode选项,而是让所有单个标记都使用该特定图标。 Possibly through replacing the markers' default icon indeed.可能确实通过替换标记的默认图标。

But if for whatever reason you still want to use the singleMarkerMode option, then it is not that complicated either.但是如果出于某种原因您仍然想使用singleMarkerMode选项,那么它也没有那么复杂。 You could do for instance:例如,您可以这样做:

var mcg = L.markerClusterGroup({
    singleMarkerMode: true,
    iconCreateFunction: function (cluster) {
        var childCount = cluster.getChildCount();
        return childCount === 1 ? iconForSize1 : someOtherIcon;
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM