问题描述
因此,我目前在地图框示例的帮助下将地图上的点网格聚类: Create and style clusters
MapBox的示例代码计算了个点数,并使用变量point_count_abbreviated
这样显示:
map.addLayer({
id: "cluster-count",...
layout: {
"text-field": "{point_count_abbreviated}",},...
})
我的geoJSON源看起来像这样:
{
"type": "Feature","geometry": { ... },"properties": {
"location": { ... },"id": 111,...
"value": {
"count": 2
}
}
}
我要做的是,将所有“ properties.value.count”加在一起, 并显示每个群集上的总和。 我已经尝试过documentation about clusterProperties中的示例 但是由于我的count变量嵌套在value对象中, 我很难使它工作。
所以我猜在集群源定义中是这样的:
clusterProperties: {"sum": ["+",["object","value",['get,'count']]]}
并将其显示在图层中:
layout: {
"text-field": ['get','sum'],
有人可以指出正确的方向吗? :)
解决方法
我设法完成了这项工作。逻辑如下: 在属性对象内的值对象内获取计数变量。 属性对象是here中描述的保留表达式。
来源定义:
clusterProperties: {
sum: ["+",["get","count","value",["properties"]]]],},
在显示总和的层中:
"text-field": ["get","sum"],
,
感谢分享这个方法。我想为正在处理转换(CSV 到 GeoJSON)数据的他们添加一个细节。我花了几个小时来寻找将数字字段转换为整数的解决方案。所有数据都被转换为字符串,csv2geojson的官方网页中没有关于如何转换的信息。最后我添加了 -> numericFields: 'customers',选项,它对我有用。
function makeGeoJSON(csvData) {
csv2geojson.csv2geojson(csvData,{
latfield: 'Latitude',lonfield: 'Longitude',delimiter: ',',numericFields: 'customers',cluster: true,clusterMaxZoom: 14,clusterRadius: 50,function (err,data) {
map.on('load',function () {
map.addSource('geopoints',{
type: 'geojson',data: data,clusterProperties: {
sum: ["+","customers",["properties"]]],});