我正在使用mapbox-gl-js API,并将其与react一起使用来创建一些自定义标记,如下所示:
let div = document.createElement('div');
let marker = new mapboxgl.Marker(div, {
offset: [ -20, 80 ]
});
marker.setLngLat(person.geometry.coordinates);
render(
<MapPersonIcon />,
div,
() => {
marker.addTo(map);
}
);

这样做效果很好。然而,我现在想对这些标记进行聚类,产生与使用层的功能相同的效果。
https://www.mapbox.com/mapbox-gl-js/example/cluster/

有没有人知道这是否可能(希望也有自定义集群),或者它是否会在即将发布的版本中可用?
发布于 2019-04-25 11:04:08
此功能现在位于Mapbox GL js - https://docs.mapbox.com/mapbox-gl-js/example/cluster-html/中
要点:
使用map.addSource设置数据源时,请确保定义cluster: true和clusterRadius: int,如下所示:
map.addSource( 'sourceName', {
type: "geojson",
data: {
type: 'FeatureCollection',
features: [JSON]
},
cluster: true,
clusterRadius: 80,
});这将推动mapbox集群你的图标,但你需要告诉mapbox当它集群这些图标时要做什么:
map.on( 'moveend', updateMarkers ); // moveend also considers zoomend业务(针对相关性进行了调整):
function updateMarkers(){
var features = map.querySourceFeatures( 'sourceName' );
for ( var i = 0; i < features.length; i++ ) {
var coords = features[ i ].geometry.coordinates;
var props = features[ i ].properties;
if ( props.cluster ){ // this property is only present when the feature is clustered
// generate your clustered icon using props.point_count
var el = document.createElement( 'div' );
el.classList.add( 'mapCluster' );
el.innerText = props.point_count;
marker = new mapboxgl.Marker( { element: el } ).setLngLat( coords );
} else { // feature is not clustered, create an icon for it
var el = new Image();
el.src = 'icon.png';
el.classList.add( 'mapMarker' );
el.dataset.type = props.type; // you can use custom data if you have assigned it in the GeoJSON data
marker = new mapboxgl.Marker( { element: el } ).setLngLat( coords );
}
marker.addTo( map );
}注意:不要复制粘贴这段代码,而是将它与https://docs.mapbox.com/mapbox-gl-js/example/cluster-html/结合使用,以获得完整的内容。希望这能有所帮助!
发布于 2016-08-22 17:56:37
回答自己的问题:
目前,根据mapbox的github,这似乎是不可能的:

如果你想集群你的标记,你需要使用mapbox的原生maki图标(请看上面的示例图片和URL),直到有一个插件可用于你的自定义HTML标记。
https://stackoverflow.com/questions/39001933
复制相似问题