Leaflet聚合图层---Leaflet.markercluster

作者:刘大

Leaflet.markercluster是Leaflet中运用比较多的一个可视化插件,本文我们就细说下该插件下的属性和方法。

####1.属性
在markercluster的gitbub地址https://github.com/Leaflet/Leaflet.markercluster上,已经详细介绍了各个属性的用法和作用。下面也通过一张表简单总结下
属性.png
下面重点说下spiderfyShapePositionsiconCreateFunction
#####1.1 spiderfyShapePositions
在markercluster 1.4.1的版本中使用spiderfyShapePositions该属性无效,可查看https://github.com/Leaflet/Leaflet.markercluster/issues/994
主要是因为1.4.1版本中未加入改自定义的方法。我们可在使用前重新加入,代码如下

  L.MarkerCluster.include({ spiderfy: function () {
	if (this._group._spiderfied === this || this._group._inZoomAnimation) {
		return;
	}

	var childMarkers = this.getAllChildMarkers(null, true),
		group = this._group,
		map = group._map,
		center = map.latLngToLayerPoint(this._latlng),
		positions;

	this._group._unspiderfy();
	this._group._spiderfied = this;

	//TODO Maybe: childMarkers order by distance to center

	if (this._group.options.spiderfyShapePositions) {
		positions = this._group.options.spiderfyShapePositions(childMarkers.length, center);
	} else if (childMarkers.length >= this._circleSpiralSwitchover) {
		positions = this._generatePointsSpiral(childMarkers.length, center);
	} else {
		center.y += 10; // Otherwise circles look wrong => hack for standard blue icon, renders differently for other icons.
		positions = this._generatePointsCircle(childMarkers.length, center);
	}

	this._animationSpiderfy(childMarkers, positions);
}});
    resultLayer = L.markerClusterGroup({
        spiderfyOnMaxZoom: false,
        showCoverageOnHover: false,
        zoomToBoundsOnClick: false,
        spiderfyShapePositions: function(count, centerPt) {
                var distanceFromCenter = 35,
                    markerDistance = 45,
                    lineLength = markerDistance * (count - 1),
                    lineStart = centerPt.y - lineLength / 2,
                    res = [],
                    i;

                res.length = count;

                for (i = count - 1; i >= 0; i--) {
                    res[i] = new L.Point(centerPt.x + distanceFromCenter, lineStart + markerDistance * i);
                }

                return res;
            }
    });

使用前后对比:
设置前.png

设置后.png
由此我们就可以根据自己的业务需求制定散开的形状了
如果你不想自定义,可以使用该插件下的子插件Leaflet.MarkerCluster.PlacementStrategies
#####1.2 iconCreateFunction

 resultLayer = L.markerClusterGroup({
        spiderfyOnMaxZoom: false,
        showCoverageOnHover: false,
        zoomToBoundsOnClick: false,
        iconCreateFunction: function(cluster) {
		return L.divIcon({ className:'customstyle',html: '<div class="customstyle">' + cluster.getChildCount() + '</div>' });
	}

自定义图标.png
####2.事件&方法
事件可L.Marker事件一样,只是需在前面添加‘cluster’
方法可概括为
总结.png
点击判断个数并进行散开或缩放

    resultLayer.on('clusterclick',function(a){
        if(a.layer.getChildCount()<20){
           a.layer.spiderfy()  
        } else {
            a.layer.zoomToBounds()
        }
    })

其他事件和方法可以根据具体需求进行调用。

使用 Leaflet 加载聚合图需要依赖 Leaflet.markercluster 插件。以下是一个简单的示例: 1. 在 HTML 中引入 LeafletLeaflet.markercluster 的 CSS 和 JS 文件: ``` <!-- Leaflet CSS --> <link rel="stylesheet" href="https://cdn.jsdelivr.net/leaflet/1.3.1/leaflet.css" /> <!-- Leaflet.markercluster CSS --> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/leaflet.markercluster/1.4.1/MarkerCluster.css" /> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/leaflet.markercluster/1.4.1/MarkerCluster.Default.css" /> <!-- Leaflet JS --> <script src="https://cdn.jsdelivr.net/leaflet/1.3.1/leaflet.js"></script> <!-- Leaflet.markercluster JS --> <script src="https://cdnjs.cloudflare.com/ajax/libs/leaflet.markercluster/1.4.1/leaflet.markercluster.js"></script> ``` 2. 创建 Leaflet 地图: ``` var map = L.map('map').setView([51.505, -0.09], 13); ``` 3. 添加一个 Leaflet.markercluster 图层: ``` var markers = L.markerClusterGroup(); ``` 4. 创建一组标记,并将它们添加到 Leaflet.markercluster 图层: ``` var marker1 = L.marker([51.5, -0.09]); var marker2 = L.marker([51.52, -0.07]); var marker3 = L.marker([51.49, -0.1]); markers.addLayers([marker1, marker2, marker3]); ``` 5. 将 Leaflet.markercluster 图层添加到地图上: ``` map.addLayer(markers); ``` 这样就可以在 Leaflet 地图上加载聚合图了。你可以通过 Leaflet.markercluster 插件的配置选项来自定义聚合图的外观和行为。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值