Skip to content

Commit

Permalink
fix(grid): max zoom (#677)
Browse files Browse the repository at this point in the history
  • Loading branch information
vicb authored Jun 26, 2023
1 parent d2ca9f5 commit dceb4c2
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 13 deletions.
2 changes: 2 additions & 0 deletions src/algorithms/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,10 @@ export interface Algorithm {
}

export interface AlgorithmOptions {
// Markers are not clustered at maxZoom and above.
maxZoom?: number;
}

/**
* @hidden
*/
Expand Down
21 changes: 15 additions & 6 deletions src/algorithms/grid.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

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

initialize();
const markers = [
Expand All @@ -35,7 +34,7 @@ describe.each(markers)(

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

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

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

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

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

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

expect(result.changed).toBe(false);

map.getZoom = jest.fn().mockReturnValue(16);

result = grid.calculate({
markers,
map,
mapCanvasProjection,
});

expect(result.changed).toBe(false);
});
}
);
7 changes: 3 additions & 4 deletions src/algorithms/grid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,13 @@ export class GridAlgorithm extends AbstractViewportAlgorithm {
protected gridSize: number;
protected maxDistance: number;
protected clusters: Cluster[] = [];
protected state: { zoom: number };
protected state = { zoom: -1 };

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

this.maxDistance = maxDistance;
this.gridSize = gridSize;
this.state = { zoom: null };
}

public calculate({
Expand All @@ -67,8 +66,8 @@ export class GridAlgorithm extends AbstractViewportAlgorithm {
}: AlgorithmInput): AlgorithmOutput {
const state = { zoom: map.getZoom() };
let changed = false;
if (this.state.zoom > this.maxZoom && state.zoom > this.maxZoom) {
// still beyond maxZoom, no change
if (this.state.zoom >= this.maxZoom && state.zoom >= this.maxZoom) {
// still at or beyond maxZoom, no change
} else {
changed = !equal(this.state, state);
}
Expand Down
4 changes: 1 addition & 3 deletions src/algorithms/supercluster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export class SuperClusterAlgorithm extends AbstractAlgorithm {
protected superCluster: SuperCluster;
protected markers: Marker[];
protected clusters: Cluster[];
protected state: { zoom: number | null };
protected state = { zoom: -1 };

constructor({ maxZoom, radius = 60, ...options }: SuperClusterOptions) {
super({ maxZoom });
Expand All @@ -46,8 +46,6 @@ export class SuperClusterAlgorithm extends AbstractAlgorithm {
radius,
...options,
});

this.state = { zoom: null };
}

public calculate(input: AlgorithmInput): AlgorithmOutput {
Expand Down

0 comments on commit dceb4c2

Please sign in to comment.