There are certainly other alternatives for implementing client-side point clustering for web mapping applications, for example the Leaflet.markercluster library, or the Esri Leaflet Cluster library, which extends the Leaflet.markercluster to work with ArcGIS feature services.
To use the this ClusterLayer object in a custom application, you can add the library to your dojoConfig, and import it like a typical AMD module using
require. Once the library is loaded, you can create an instance of the ClusterLayer object with the same constructor options that you would use for a regular FeatureLayer. It supports feature services, feature layers created with client-side graphics, or a variation of the latter that loads a URL as a simple JSON data file. To control the behaviour and appearance of the clusters on the map, an additional constructor parameter named
A worker object is used to perform the point clustering in the background, which helps prevent UI locking when larger datasets are being loaded. You can try several demos (linked from the README page on GitHub), which show clustering of a Feature Service with Toronto crime data (about 33k points), or randomly generated datasets with 10k and 50k points. The feature layer sample may take a bit of time to load in the background (while it queries the service), but the static 10k/50k datasets load very quickly. Although it’s a bit overkill, I was also curious to see if 1 million points could be loaded – you will find a demo for this too, but beware that it is a 40mb JSON file, and will likely exceed limits on mobile devices.
There are (at least) a few limitations with the current implementation:
- The ClusterLayer code is experimental – it is not tested to do anything beyond what is shown in the demos, and may not work as expected in different scenarios.
- Point loading can take a while when working against a large feature service with no definitionExpression applied, since all features will be downloaded before clusters are displayed. If/when time permits, I’d want to add some events to the ClusterLayer object and demonstrate UI feedback in the demos.
- Only the first mapView that the layer is added to will control the zoom/extent for which point clusters are displayed.
- A map that has an instance of the ClusterLayer added to it can be added to a 3D SceneView, however it will require a different renderer (and maybe some revisions to the code).
When you create an instance of the ClusterLayer object using the URL to an ArcGIS Feature Service, you can control what features are included in the clusters using the definitionExpression property. You can provide this when creating the object, and/or update the property at any time after that. When the definition expression is set or modified, the underlying feature service will be queried for all matching features, and the clusters are regenerated.
Feel free to comment on the project in GitHub, and/or clone the repository and make pull requests if you would like to contribute any of your own changes/improvements to the code.