Skip to content

Commit dceb4c2

Browse files
authored
fix(grid): max zoom (#677)
1 parent d2ca9f5 commit dceb4c2

File tree

4 files changed

+21
-13
lines changed

4 files changed

+21
-13
lines changed

src/algorithms/core.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,10 @@ export interface Algorithm {
5757
}
5858

5959
export interface AlgorithmOptions {
60+
// Markers are not clustered at maxZoom and above.
6061
maxZoom?: number;
6162
}
63+
6264
/**
6365
* @hidden
6466
*/

src/algorithms/grid.test.ts

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
import { GridAlgorithm } from "./grid";
1818
import { initialize, MapCanvasProjection } from "@googlemaps/jest-mocks";
19-
import { Marker } from "../marker-utils";
2019

2120
initialize();
2221
const markers = [
@@ -35,7 +34,7 @@ describe.each(markers)(
3534

3635
test("calculate should return changed: true for first call when zoom > max zoom", () => {
3736
const mapCanvasProjection = new MapCanvasProjection();
38-
const markers: Marker[] = [marker];
37+
const markers = [marker];
3938

4039
const grid = new GridAlgorithm({ maxZoom: 16 });
4140
grid["noop"] = jest.fn();
@@ -60,10 +59,10 @@ describe.each(markers)(
6059
expect(changed).toBe(true);
6160
});
6261

63-
test("calculate should return changed: false for next calls above max zoom", () => {
62+
test("calculate should return changed: false when zoom doesn't change", () => {
6463
const mapCanvasProjection =
6564
jest.fn() as unknown as google.maps.MapCanvasProjection;
66-
const markers: Marker[] = [marker];
65+
const markers = [marker];
6766

6867
const grid = new GridAlgorithm({ maxZoom: 16 });
6968
grid["noop"] = jest.fn();
@@ -87,10 +86,10 @@ describe.each(markers)(
8786
expect(result.changed).toBe(false);
8887
});
8988

90-
test("calculate should return changed: false for next calls above max zoom, even if zoom changed", () => {
89+
test("calculate should return changed: false for next calls at or above max zoom, even if zoom changed", () => {
9190
const mapCanvasProjection =
9291
jest.fn() as unknown as google.maps.MapCanvasProjection;
93-
const markers: Marker[] = [marker];
92+
const markers = [marker];
9493

9594
const grid = new GridAlgorithm({ maxZoom: 16 });
9695
grid["noop"] = jest.fn();
@@ -114,6 +113,16 @@ describe.each(markers)(
114113
});
115114

116115
expect(result.changed).toBe(false);
116+
117+
map.getZoom = jest.fn().mockReturnValue(16);
118+
119+
result = grid.calculate({
120+
markers,
121+
map,
122+
mapCanvasProjection,
123+
});
124+
125+
expect(result.changed).toBe(false);
117126
});
118127
}
119128
);

src/algorithms/grid.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,13 @@ export class GridAlgorithm extends AbstractViewportAlgorithm {
5050
protected gridSize: number;
5151
protected maxDistance: number;
5252
protected clusters: Cluster[] = [];
53-
protected state: { zoom: number };
53+
protected state = { zoom: -1 };
5454

5555
constructor({ maxDistance = 40000, gridSize = 40, ...options }: GridOptions) {
5656
super(options);
5757

5858
this.maxDistance = maxDistance;
5959
this.gridSize = gridSize;
60-
this.state = { zoom: null };
6160
}
6261

6362
public calculate({
@@ -67,8 +66,8 @@ export class GridAlgorithm extends AbstractViewportAlgorithm {
6766
}: AlgorithmInput): AlgorithmOutput {
6867
const state = { zoom: map.getZoom() };
6968
let changed = false;
70-
if (this.state.zoom > this.maxZoom && state.zoom > this.maxZoom) {
71-
// still beyond maxZoom, no change
69+
if (this.state.zoom >= this.maxZoom && state.zoom >= this.maxZoom) {
70+
// still at or beyond maxZoom, no change
7271
} else {
7372
changed = !equal(this.state, state);
7473
}

src/algorithms/supercluster.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export class SuperClusterAlgorithm extends AbstractAlgorithm {
3636
protected superCluster: SuperCluster;
3737
protected markers: Marker[];
3838
protected clusters: Cluster[];
39-
protected state: { zoom: number | null };
39+
protected state = { zoom: -1 };
4040

4141
constructor({ maxZoom, radius = 60, ...options }: SuperClusterOptions) {
4242
super({ maxZoom });
@@ -46,8 +46,6 @@ export class SuperClusterAlgorithm extends AbstractAlgorithm {
4646
radius,
4747
...options,
4848
});
49-
50-
this.state = { zoom: null };
5149
}
5250

5351
public calculate(input: AlgorithmInput): AlgorithmOutput {

0 commit comments

Comments
 (0)