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,
clusterLabelCreator, clusterPopupCreator
) => {
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",
map,
center: [ 80.20127, 22.12355 ],
zoom: 4
});
// Override the default symbol representing the cluster extent
view.popup.viewModel.selectedClusterBoundaryFeature.symbol = {
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({
view,
container: "legendDiv"
});
const infoDiv = document.getElementById("infoDiv");
view.ui.add(
new Expand({
view,
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;
layer.featureReduction =
fr && fr.type === "cluster" ? null : featureReduction;
}
toggleButton.innerText =
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;
layerView.filter = {
where: whereClause
};
// close popup for former cluster that no longer displays
view.popup.close();
});
});
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>