/
html-cluster-overlay.ts
80 lines (68 loc) · 2.41 KB
/
html-cluster-overlay.ts
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
import { point } from '@turf/helpers';
import Supercluster from 'supercluster';
import HtmlOverlay from './html-overlay';
export default class HtmlClusterOverlay<ObjType> extends HtmlOverlay {
_superCluster: Record<string, any>;
_lastObjects: ObjType[] | null | undefined = null;
getItems(): Record<string, any>[] {
// supercluster().load() is expensive and we want to run it only
// when necessary and not for every frame.
// TODO: Warn if this is running many times / sec
const newObjects = this.getAllObjects();
if (newObjects !== this._lastObjects) {
this._superCluster = new Supercluster(this.getClusterOptions());
this._superCluster.load(
newObjects.map((object) => point(this.getObjectCoordinates(object), { object }))
);
this._lastObjects = newObjects;
// console.log('new Supercluster() run');
}
const clusters = this._superCluster.getClusters(
[-180, -90, 180, 90],
Math.round(this.getZoom())
);
return clusters.map(
({
geometry: { coordinates },
properties: { cluster, point_count: pointCount, cluster_id: clusterId, object },
}) =>
cluster
? this.renderCluster(coordinates, clusterId, pointCount)
: this.renderObject(coordinates, object)
);
}
getClusterObjects(clusterId: number): ObjType[] {
return this._superCluster
.getLeaves(clusterId, Infinity)
.map((object) => object.properties.object);
}
// Override to provide items that need clustering.
// If the items have not changed please provide the same array to avoid
// regeneration of the cluster which causes performance issues.
getAllObjects(): ObjType[] {
return [];
}
// override to provide coordinates for each object of getAllObjects()
getObjectCoordinates(obj: ObjType): [number, number] {
return [0, 0];
}
// Get options object used when instantiating supercluster
getClusterOptions(): Record<string, any> | null | undefined {
return {
maxZoom: 20,
};
}
// override to return an HtmlOverlayItem
renderObject(coordinates: number[], obj: ObjType): Record<string, any> | null | undefined {
return null;
}
// override to return an HtmlOverlayItem
// use getClusterObjects() to get cluster contents
renderCluster(
coordinates: number[],
clusterId: number,
pointCount: number
): Record<string, any> | null | undefined {
return null;
}
}