点聚类 - 生成建议的配置

尝试一下在线预览

此示例演示如何为在 MapView 中启用的点聚类生成默认 popupTemplate标注。要生成 popupTemplate,请在 geoscene/smartMapping/popup/clusters 模块中调用 getTemplates() 方法。在 geoscene/smartMapping/labels/clusters 中使用 getLabelSchemes() 方法为聚类生成建议的默认的 labelingInfo 以及建议的 clusterMinSize。 

配置集群
157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 178 178 178 178 178 178 178 178 178 178 178 178 178 178 178 178 178 178 178 178 178 178 178 178 178 178 178 178
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8" />
  <meta
    name="viewport"
    content="initial-scale=1,maximum-scale=1,user-scalable=no"
  />

<title>Point clustering - generate suggested configuration | Sample | GeoScene API for JavaScript 4.22</title>

<link rel="stylesheet" href="https://js.geoscene.cn/4.23/geoscene/themes/light/main.css" />
<script src="https://js.geoscene.cn/4.23/"></script>

<style>
  html, body, #viewDiv {
    height: 100%;
    width: 100%;
    margin: 0;
    padding: 0;
  #infoDiv {
    background: white;
    padding: 10px;
</style>

<script>
  require([
    "geoscene/WebMap",
    "geoscene/views/MapView",
    "geoscene/layers/FeatureLayer",
    "geoscene/widgets/Legend",
    "geoscene/widgets/Expand",
    "geoscene/smartMapping/labels/clusters",
    "geoscene/smartMapping/popup/clusters"
  ], (Map, MapView, FeatureLayer, Legend, Expand,
  ) => {
    const serviceUrl =
      "https://services.arcgis.com/V6ZHFr6zdgNZuVG0/arcgis/rest/services/Places_of_Worship_India/FeatureServer/0";
    const layer = new FeatureLayer({
      url: serviceUrl,
      title: "Places of worship",
      outFields: [ "name", "religion", "denomination" ],
      popupTemplate: {
        title: "{name}",
        content: [
            type: "fields",
            fieldInfos: [
                fieldName: "religion"
                fieldName: "denomination"
    const map = new Map({
      basemap: "gray-vector",
      layers: [ layer ]
    const view = new MapView({
      container: "viewDiv",
      center: [ 80.20127, 22.12355 ],
      zoom: 4
    // Override the default symbol representing the cluster extent
      type: "simple-fill",
      style: "solid",
      color: "rgba(50,50,50,0.15)",
      outline: {
        width: 0.5,
        color: "rgba(50,50,50,0.25)"
    const legend = new Legend({
      container: "legendDiv"
    const infoDiv = document.getElementById("infoDiv");
      new Expand({
        content: infoDiv,
        expandIconClass: "esri-icon-layer-list",
        expanded: true
      }), "top-right");
      .then((featureReduction) => {
        const toggleButton = document.getElementById("toggle-cluster");
        toggleButton.addEventListener("click", toggleClustering);
        // To turn off clustering on a layer, set the
        // featureReduction property to null
        function toggleClustering() {
          if(isWithinScaleThreshold()){
            let fr = layer.featureReduction;
              fr && fr.type === "cluster" ? null : featureReduction;
            toggleButton.innerText === "Enable Clustering"
              ? "Disable Clustering"
              : "Enable Clustering";
        view.whenLayerView(layer).then((layerView) => {
          const filterSelect = document.getElementById("filter");
          // filters the layer using a definitionExpression
          // based on a religion selected by the user
          filterSelect.addEventListener("change", (event) => {
            const newValue = event.target.value;
            const whereClause = newValue
              ? `religion = '${newValue}'`
              : null;
              where: whereClause
            // close popup for former cluster that no longer displays
        view.watch("scale", (scale) => {
          if(toggleButton.innerText === "Disable Clustering"){
            layer.featureReduction = isWithinScaleThreshold() ? featureReduction : null;
      }).catch((error) => {
        console.error(error);
    function isWithinScaleThreshold(){
      return view.scale > 50000;
    async function generateClusterConfig(layer){

      // generates default popupTemplate
      const popupTemplate = await clusterPopupCreator
        .getTemplates({ layer })
        .then(popupTemplateResponse => popupTemplateResponse.primaryTemplate.value);


      // generates default labelingInfo
      const { labelingInfo, clusterMinSize } = await clusterLabelCreator
        .getLabelSchemes({ layer, view })
        .then(labelSchemes => labelSchemes.primaryScheme );


      return {
        type: "cluster",
        popupTemplate,
        labelingInfo,
        clusterMinSize
      };
    }
</script>

</head>

<body>
  <div id="viewDiv"></div>
  <div id="infoDiv" class="geoscene-widget">
    Filter by religion:
    <select id="filter" class="esri-select">
      <option value="">All</option>
      <option value="Hindu">Hindu</option>
      <option value="Christian">Christian</option>
      <option value="Muslim">Muslim</option>
      <option value="Buddhist">Buddhist</option>
      <option value="Sikh">Sikh</option>
      <option value="Jain">Jain</option>
    </select>
    <div style="padding-top: 10px;">
      <button id="toggle-cluster" class="geoscene-button">Disable Clustering</button>
    </div>
    <div id="legendDiv"></div>
  </div>
</body>

</html>

聚类是通过 FeatureLayer 的 featureReduction 属性启用的。

设置生成的集群配置
102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 103 104 105 106 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8" />
  <meta
    name="viewport"
    content="initial-scale=1,maximum-scale=1,user-scalable=no"
  />

<title>Point clustering - generate suggested configuration | Sample | GeoScene API for JavaScript 4.22</title>

<link rel="stylesheet" href="https://js.geoscene.cn/4.23/geoscene/themes/light/main.css" />
<script src="https://js.geoscene.cn/4.23/"></script>

<style>
  html, body, #viewDiv {
    height: 100%;
    width: 100%;
    margin: 0;
    padding: 0;
  #infoDiv {
    background: white;
    padding: 10px;
</style>

<script>
  require([
    "geoscene/WebMap",
    "geoscene/views/MapView",
    "geoscene/layers/FeatureLayer",
    "geoscene/widgets/Legend",
    "geoscene/widgets/Expand",
    "geoscene/smartMapping/labels/clusters",
    "geoscene/smartMapping/popup/clusters"
  ], (Map, MapView, FeatureLayer, Legend, Expand,
  ) => {
    const serviceUrl =
      "https://services.arcgis.com/V6ZHFr6zdgNZuVG0/arcgis/rest/services/Places_of_Worship_India/FeatureServer/0";
    const layer = new FeatureLayer({
      url: serviceUrl,
      title: "Places of worship",
      outFields: [ "name", "religion", "denomination" ],
      popupTemplate: {
        title: "{name}",
        content: [
            type: "fields",
            fieldInfos: [
                fieldName: "religion"
                fieldName: "denomination"
    const map = new Map({
      basemap: "gray-vector",
      layers: [ layer ]
    const view = new MapView({
      container: "viewDiv",
      center: [ 80.20127, 22.12355 ],
      zoom: 4
    // Override the default symbol representing the cluster extent
      type: "simple-fill",
      style: "solid",
      color: "rgba(50,50,50,0.15)",
      outline: {
        width: 0.5,
        color: "rgba(50,50,50,0.25)"
    const legend = new Legend({
      container: "legendDiv"
    const infoDiv = document.getElementById("infoDiv");
      new Expand({
        content: infoDiv,
        expandIconClass: "esri-icon-layer-list",
        expanded: true
      }), "top-right");
    layer.when()
      .then(generateClusterConfig)
      .then((featureReduction) => {

        layer.featureReduction = featureReduction;
        const toggleButton = document.getElementById("toggle-cluster");
        toggleButton.addEventListener("click", toggleClustering);
        // To turn off clustering on a layer, set the
        // featureReduction property to null
        function toggleClustering() {
          if(isWithinScaleThreshold()){
            let fr = layer.featureReduction;
              fr && fr.type === "cluster" ? null : featureReduction;
            toggleButton.innerText === "Enable Clustering"
              ? "Disable Clustering"
              : "Enable Clustering";
        view.whenLayerView(layer).then((layerView) => {
          const filterSelect = document.getElementById("filter");
          // filters the layer using a definitionExpression
          // based on a religion selected by the user
          filterSelect.addEventListener("change", (event) => {
            const newValue = event.target.value;
            const whereClause = newValue
              ? `religion = '${newValue}'`
              : null;
              where: whereClause
            // close popup for former cluster that no longer displays
        view.watch("scale", (scale) => {
          if(toggleButton.innerText === "Disable Clustering"){
            layer.featureReduction = isWithinScaleThreshold() ? featureReduction : null;
      }).catch((error) => {
        console.error(error);
    function isWithinScaleThreshold(){
      return view.scale > 50000;
    async function generateClusterConfig(layer){
      // generates default popupTemplate
      const popupTemplate = await clusterPopupCreator
        .then(popupTemplateResponse => popupTemplateResponse.primaryTemplate.value);
      // generates default labelingInfo
      const { labelingInfo, clusterMinSize } = await clusterLabelCreator
        .then(labelSchemes => labelSchemes.primaryScheme );
      return {
        type: "cluster",
</script>

</head>

<body>
  <div id="viewDiv"></div>
  <div id="infoDiv" class="geoscene-widget">
    Filter by religion:
    <select id="filter" class="esri-select">
      <option value="">All</option>
      <option value="Hindu">Hindu</option>
      <option value="Christian">Christian</option>
      <option value="Muslim">Muslim</option>
      <option value="Buddhist">Buddhist</option>
      <option value="Sikh">Sikh</option>
      <option value="Jain">Jain</option>
    </select>
    <div style="padding-top: 10px;">
      <button id="toggle-cluster" class="geoscene-button">Disable Clustering</button>
    </div>
    <div id="legendDiv"></div>
  </div>
</body>

</html>

由于图层的 featureReduction 属性独立于其渲染器,因此符号和聚类图形的 popupTemplate 可用于汇总构成聚类的要素。有关聚类可以汇总其表示的点的各种方式的详细信息,请参阅聚类样式和配置

此示例中的图层使用 UniqueValueRenderer 可视化礼拜场所。启用聚类后,将为每个聚类分配聚类中要素中最常见的 uniqueValueInfo 的符号。

显示所有点显示聚类要素
clustering-type-disabled clustering-type-enabled

您可以在 popupTemplate 中引用聚类的主要值。对于具有 UniqueValueRenderer 的图层,汇总字段名称遵循以下格式:{cluster_type_fieldName}。在这种情况下,渲染器将可视化 religion 字段中的唯一值,因此要在 popupTemplate 中引用的聚合字段名称为 {cluster_type_religion}

此示例还演示了如何在启用聚类的情况下按类别浏览和过滤图层,方法与在非聚类图层上的方式相同。将过滤器应用于聚类图层的图层视图时,聚类将重新计算客户端,并仅显示符合过滤器的信息。

         
1
2
3
4
5
6
7
8
9
filterSelect.addEventListener("change", (event) => {
  const newValue = event.target.value;
  const whereClause = newValue ? "religion = '" + newValue + "'" : null;
  layerView.filter = {
    where: whereClause
  };
  // close popup for former cluster that no longer displays
  view.popup.close();
});

Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.