Azure Maps能否修改聚类规则,按数据属性而非地理位置聚类?
基于属性独立聚类的Azure Maps实现方案
你之前创建多过滤图层的思路是对的,问题出在需要为每个属性类别单独配置独立的**数据源(DataSource)**和对应的聚类逻辑,而不是共用同一个数据源的多图层过滤——共用数据源时聚类计算会包含所有点,自然没法按属性拆分。
实现步骤
- 首先把所有建筑物标记按
color属性的三个取值(红/蓝/绿)分别筛选,存入三个独立的DataSource实例,每个数据源单独开启聚类配置,各自的聚类参数可以独立调整,互不影响 - 给三个数据源分别绑定符号图层(展示单点)和气泡图层(展示聚合聚类点),不同类别的聚类点可以自定义不同的样式,即使位置重叠也能正常渲染
核心代码示例(前端JS部分,直接嵌入ASP.NET Core视图页即可)
// 初始化地图 var map = new atlas.Map('myMap', { center: [120, 30], zoom: 10, view: 'Auto', authOptions: { authType: 'subscriptionKey', subscriptionKey: '<你的Azure Maps密钥>' } }); map.events.add('ready', function () { // 定义三个属性对应的独立数据源,都单独开启聚类 const redDataSource = new atlas.source.DataSource(null, { cluster: true, clusterRadius: 50, // 聚类半径可独立调整 clusterMaxZoom: 14 }); const blueDataSource = new atlas.source.DataSource(null, { cluster: true, clusterRadius: 50, clusterMaxZoom: 14 }); const greenDataSource = new atlas.source.DataSource(null, { cluster: true, clusterRadius: 50, clusterMaxZoom: 14 }); // 把三个数据源添加到地图 map.sources.add(redDataSource); map.sources.add(blueDataSource); map.sources.add(greenDataSource); // 从后端接口拉取所有建筑物标记数据 fetch('/api/Buildings/GetAll') .then(res => res.json()) .then(buildings => { // 按color属性拆分到不同数据源 buildings.forEach(b => { const feature = new atlas.data.Feature(new atlas.data.Point([b.lng, b.lat]), { color: b.color, name: b.name }); switch(b.color) { case 'red': redDataSource.add(feature); break; case 'blue': blueDataSource.add(feature); break; case 'green': greenDataSource.add(feature); break; } }); }); // 红色类图层配置 map.layers.add(new atlas.layer.BubbleLayer(redDataSource, null, { radius: ['step', ['get', 'point_count'], 15, 10, 20, 100, 30], color: 'rgba(255,0,0,0.8)', filter: ['has', 'point_count'] // 仅展示聚类点 })); map.layers.add(new atlas.layer.SymbolLayer(redDataSource, null, { iconOptions: { image: 'marker-red', allowOverlap: true }, filter: ['!', ['has', 'point_count']] // 仅展示非聚类单点 })); // 蓝色类图层配置 map.layers.add(new atlas.layer.BubbleLayer(blueDataSource, null, { radius: ['step', ['get', 'point_count'], 15, 10, 20, 100, 30], color: 'rgba(0,0,255,0.8)', filter: ['has', 'point_count'] })); map.layers.add(new atlas.layer.SymbolLayer(blueDataSource, null, { iconOptions: { image: 'marker-blue', allowOverlap: true }, filter: ['!', ['has', 'point_count']] })); // 绿色类图层配置 map.layers.add(new atlas.layer.BubbleLayer(greenDataSource, null, { radius: ['step', ['get', 'point_count'], 15, 10, 20, 100, 30], color: 'rgba(0,255,0,0.8)', filter: ['has', 'point_count'] })); map.layers.add(new atlas.layer.SymbolLayer(greenDataSource, null, { iconOptions: { image: 'marker-green', allowOverlap: true }, filter: ['!', ['has', 'point_count']] })); });
注意事项
- 不需要改动ASP.NET Core后端逻辑,仅需返回所有建筑物的经纬度和color属性即可
- 若后续属性分类增加,按照相同逻辑新增对应数据源和图层即可
- 若希望重叠的聚类点有层级区分,可以调整图层的
zIndex参数设置渲染优先级
内容的提问来源于stack exchange,提问作者fabifly
相关产品推荐
相关产品推荐

