Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用Mapbox';s簇?_Javascript_Mapbox - Fatal编程技术网

Javascript 如何使用Mapbox';s簇?

Javascript 如何使用Mapbox';s簇?,javascript,mapbox,Javascript,Mapbox,我正在用Mabpox gl js v0.45对集群进行POC 我想自定义集群的属性(实际默认值是point_count和point_count_缩写)。我的每个点(每个城市一个)都有一个曲面特性(整数),我希望在聚集点时对其求和 我在对用于计算自定义属性的reduce函数的引用中看到: SuperCluster.prototype = { options: { minZoom: 0, // min zoom to generate clusters on

我正在用Mabpox gl js v0.45对集群进行POC

我想自定义集群的属性(实际默认值是point_count和point_count_缩写)。我的每个点(每个城市一个)都有一个曲面特性(整数),我希望在聚集点时对其求和

我在对用于计算自定义属性的reduce函数的引用中看到:

SuperCluster.prototype = {
    options: {
        minZoom: 0,   // min zoom to generate clusters on
        // .....
        log: false,   // whether to log timing info

        // a reduce function for calculating custom cluster properties
        reduce: null, // function (accumulated, props) { accumulated.sum += props.sum; }

        // initial properties of a cluster (before running the reducer)
        initial: function () { return {}; }, // function () { return {sum: 0}; },

        // properties to use for individual points when running the reducer
        map: function (props) { return props; } // function (props) { return {sum: props.my_value}; },
    },
但我在文档中没有看到任何关于它的提及如何设置这些选项?

Mapbox似乎没有发布这些接口(),也没有提到:


它看起来像是一个普通的减压阀。它将为每个点调用一个,并允许您使用点的属性为整个集群创建属性

所以如果你这样定义你的reduce

supercluster({
  reduce: (clusterProps, pointProps) => {
    clusterProps.sum += pointProps.surface;
  }
});

然后集群上的
sum
属性将是点上所有
surface
属性的总和。

有人给了我一个解决方法:不要使用嵌入式supecluster,而是创建自己的supecluster并将其用作源:

var myCluster = supercluster({
    radius: 40,
    maxZoom: 16,
    reduce: function(p) { /* I can use reduce/map/... functions! */ }
});

// My clustered source is created without Mapbox's clusters since I managed my clusters outside Mapbox library.
map.addSource("earthquakes", {
    type: "geojson",
    data : {
      "type" : "FeatureCollection",
      "features" : []
    }
});

function loadRemoteGeoJson() {
    var features
    // Do what you want, when you want to retrieve remote features...
    // ...
    // In the end set features into your supercluster
    myCluster.load(features)
    pushClusterIntoMapbox(map)
}

// Function to call when you load remote data AND when you zoom in or out !
function pushClusterIntoMapbox(map) {
  // I maybe should be bounded here...
  var clusters = myCluster.getClusters([ -180.0000, -90.0000, 180.0000, 90.0000 ], Math
      .floor(map.getZoom()))

  // My colleague advice me to use http://turfjs.org as helper but I think it's quite optionnal
  var features = turf.featureCollection(clusters)
  map.getSource("earthquakes").setData(features)
}

我可能不太清楚我的问题,我想知道如何设置这些方法,这些方法似乎不会被Mapbox的API暴露给用户。这看起来很有趣。不过,我很难用一个简单的geojson示例来理解如何实现它。我刚刚问了一个与数据集相关的问题,如果你有时间看一看的话:Mapbox仍然没有集成超级星团,所以我们只能用Mapbox来做。我还想使用supercluster,但我能找到的唯一一个map/reduce函数的示例被破坏(放大时),并且没有包含注释,因此有点难以理解。当缩放级别足够高时,它也不会显示未聚集的点。我在一个新问题中提到了所有这些。你介意看一下吗:
var myCluster = supercluster({
    radius: 40,
    maxZoom: 16,
    reduce: function(p) { /* I can use reduce/map/... functions! */ }
});

// My clustered source is created without Mapbox's clusters since I managed my clusters outside Mapbox library.
map.addSource("earthquakes", {
    type: "geojson",
    data : {
      "type" : "FeatureCollection",
      "features" : []
    }
});

function loadRemoteGeoJson() {
    var features
    // Do what you want, when you want to retrieve remote features...
    // ...
    // In the end set features into your supercluster
    myCluster.load(features)
    pushClusterIntoMapbox(map)
}

// Function to call when you load remote data AND when you zoom in or out !
function pushClusterIntoMapbox(map) {
  // I maybe should be bounded here...
  var clusters = myCluster.getClusters([ -180.0000, -90.0000, 180.0000, 90.0000 ], Math
      .floor(map.getZoom()))

  // My colleague advice me to use http://turfjs.org as helper but I think it's quite optionnal
  var features = turf.featureCollection(clusters)
  map.getSource("earthquakes").setData(features)
}