Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions src/algorithms/supercluster.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,35 @@ describe.each(testMarkerTypes)(
expect(cluster.markers[0]).toBe(marker);
});

test("should expose aggregated properties on Cluster", () => {
const superCluster = new SuperClusterAlgorithm({});
const marker: Marker = new markerClass();
const clusterFeature = {
type: "Feature",
geometry: { coordinates: [0, 0], type: "Point" },
properties: {
marker,
cluster: true,
cluster_id: 100,
point_count: 2,
point_count_abbreviated: 2,
highRiskCount: 1,
totalRisk: 3,
},
} as unknown as ClusterFeature<{ marker: Marker }>;

jest
.spyOn(superCluster["superCluster"], "getLeaves")
.mockImplementation(() => [clusterFeature]);

const cluster = superCluster["transformCluster"](clusterFeature);

expect(cluster.properties).toMatchObject({
highRiskCount: 1,
totalRisk: 3,
});
});

test("should not cluster if zoom didn't change", () => {
const mapCanvasProjection =
jest.fn() as unknown as google.maps.MapCanvasProjection;
Expand Down
1 change: 1 addition & 0 deletions src/algorithms/supercluster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ export class SuperClusterAlgorithm extends AbstractAlgorithm {
.getLeaves(properties.cluster_id, Infinity)
.map((leaf) => leaf.properties.marker),
position: { lat, lng },
properties,
});
}

Expand Down
1 change: 1 addition & 0 deletions src/algorithms/superviewport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ export class SuperClusterViewportAlgorithm extends AbstractViewportAlgorithm {
.getLeaves(properties.cluster_id, Infinity)
.map((leaf) => leaf.properties.marker),
position: { lat, lng },
properties,
});
}

Expand Down
8 changes: 7 additions & 1 deletion src/cluster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,24 @@

import { MarkerUtils, Marker } from "./marker-utils";

export type ClusterProperties = Record<string, unknown>;

export interface ClusterOptions {
position?: google.maps.LatLng | google.maps.LatLngLiteral;
markers?: Marker[];
/** Algorithm-specific properties associated with this cluster. */
properties?: ClusterProperties;
}

export class Cluster {
public marker?: Marker;
public readonly markers: Marker[] = [];
public readonly properties?: ClusterProperties;
protected _position?: google.maps.LatLng;

constructor({ markers, position }: ClusterOptions) {
constructor({ markers, position, properties }: ClusterOptions) {
if (markers) this.markers = markers;
this.properties = properties;
if (position) {
if (position instanceof google.maps.LatLng) {
this._position = position;
Expand Down
Loading