This commit is contained in:
metacryst
2026-01-09 11:14:27 -06:00
parent cf03c95664
commit 637c9e4674
2149 changed files with 527743 additions and 0 deletions

View File

@@ -0,0 +1,113 @@
/**
* Copyright 2021 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/// <reference types="google.maps" />
import { Cluster } from "../cluster";
import { Marker } from "../marker-utils";
export interface AlgorithmInput {
/**
* The map containing the markers and clusters.
*/
map: google.maps.Map;
/**
* An array of markers to be clustered.
*
* There are some specific edge cases to be aware of including the following:
* * Markers that are not visible.
*/
markers: Marker[];
/**
* The `mapCanvasProjection` enables easy conversion from lat/lng to pixel.
*
* @see [MapCanvasProjection](https://developers.google.com/maps/documentation/javascript/reference/overlay-view#MapCanvasProjection)
*/
mapCanvasProjection: google.maps.MapCanvasProjection;
}
export interface AlgorithmOutput {
/**
* The clusters returned based upon the {@link AlgorithmInput}.
*/
clusters: Cluster[];
/**
* A boolean flag indicating that the clusters have not changed.
*/
changed?: boolean;
}
export interface Algorithm {
/**
* Calculates an array of {@link Cluster}.
*/
calculate: ({ markers, map }: AlgorithmInput) => AlgorithmOutput;
}
export interface AlgorithmOptions {
maxZoom?: number;
}
/**
* @hidden
*/
export declare abstract class AbstractAlgorithm implements Algorithm {
protected maxZoom: number;
constructor({ maxZoom }: AlgorithmOptions);
/**
* Helper function to bypass clustering based upon some map state such as
* zoom, number of markers, etc.
*
* ```typescript
* cluster({markers, map}: AlgorithmInput): Cluster[] {
* if (shouldBypassClustering(map)) {
* return this.noop({markers})
* }
* }
* ```
*/
protected noop<T extends Pick<AlgorithmInput, "markers">>({ markers, }: T): Cluster[];
/**
* Calculates an array of {@link Cluster}. Calculate is separate from
* {@link cluster} as it does preprocessing on the markers such as filtering
* based upon the viewport as in {@link AbstractViewportAlgorithm}. Caching
* and other optimizations can also be done here.
*/
abstract calculate({ markers, map }: AlgorithmInput): AlgorithmOutput;
/**
* Clusters the markers and called from {@link calculate}.
*/
protected abstract cluster({ markers, map }: AlgorithmInput): Cluster[];
}
/**
* @hidden
*/
export interface ViewportAlgorithmOptions extends AlgorithmOptions {
/**
* The number of pixels to extend beyond the viewport bounds when filtering
* markers prior to clustering.
*/
viewportPadding?: number;
}
/**
* Abstract viewport algorithm proves a class to filter markers by a padded
* viewport. This is a common optimization.
*
* @hidden
*/
export declare abstract class AbstractViewportAlgorithm extends AbstractAlgorithm {
protected viewportPadding: number;
constructor({ viewportPadding, ...options }: ViewportAlgorithmOptions);
calculate({ markers, map, mapCanvasProjection, }: AlgorithmInput): AlgorithmOutput;
protected abstract cluster({ markers, map }: AlgorithmInput): Cluster[];
}
/**
* @hidden
*/
export declare const noop: (markers: Marker[]) => Cluster[];

View File

@@ -0,0 +1,16 @@
/**
* Copyright 2021 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
export {};

View File

@@ -0,0 +1,46 @@
/**
* Copyright 2021 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/// <reference types="google.maps" />
import { AbstractViewportAlgorithm, AlgorithmInput, AlgorithmOutput, ViewportAlgorithmOptions } from "./core";
import { Cluster } from "../cluster";
import { Marker } from "../marker-utils";
export interface GridOptions extends ViewportAlgorithmOptions {
gridSize?: number;
/**
* Max distance between cluster center and point in meters.
* @default 10000
*/
maxDistance?: number;
}
/**
* The default Grid algorithm historically used in Google Maps marker
* clustering.
*
* The Grid algorithm does not implement caching and markers may flash as the
* viewport changes. Instead use {@link SuperClusterAlgorithm}.
*/
export declare class GridAlgorithm extends AbstractViewportAlgorithm {
protected gridSize: number;
protected maxDistance: number;
protected clusters: Cluster[];
protected state: {
zoom: number;
};
constructor({ maxDistance, gridSize, ...options }: GridOptions);
calculate({ markers, map, mapCanvasProjection, }: AlgorithmInput): AlgorithmOutput;
protected cluster({ markers, map, mapCanvasProjection, }: AlgorithmInput): Cluster[];
protected addToClosestCluster(marker: Marker, map: google.maps.Map, projection: google.maps.MapCanvasProjection): void;
}

View File

@@ -0,0 +1,16 @@
/**
* Copyright 2021 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
export {};

View File

@@ -0,0 +1,21 @@
/**
* Copyright 2021 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
export * from "./core";
export * from "./grid";
export * from "./noop";
export * from "./supercluster";
export * from "./superviewport";
export * from "./utils";

View File

@@ -0,0 +1,25 @@
/**
* Copyright 2021 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { AbstractAlgorithm, AlgorithmInput, AlgorithmOptions, AlgorithmOutput } from "./core";
import { Cluster } from "../cluster";
/**
* Noop algorithm does not generate any clusters or filter markers by the an extended viewport.
*/
export declare class NoopAlgorithm extends AbstractAlgorithm {
constructor({ ...options }: AlgorithmOptions);
calculate({ markers, map, mapCanvasProjection, }: AlgorithmInput): AlgorithmOutput;
protected cluster(input: AlgorithmInput): Cluster[];
}

View File

@@ -0,0 +1,43 @@
/**
* Copyright 2021 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { AbstractAlgorithm, AlgorithmInput, AlgorithmOutput } from "./core";
import SuperCluster, { ClusterFeature } from "supercluster";
import { Marker } from "../marker-utils";
import { Cluster } from "../cluster";
export type SuperClusterOptions = SuperCluster.Options<{
[name: string]: any;
}, {
[name: string]: any;
}>;
/**
* A very fast JavaScript algorithm for geospatial point clustering using KD trees.
*
* @see https://www.npmjs.com/package/supercluster for more information on options.
*/
export declare class SuperClusterAlgorithm extends AbstractAlgorithm {
protected superCluster: SuperCluster;
protected markers: Marker[];
protected clusters: Cluster[];
protected state: {
zoom: number;
};
constructor({ maxZoom, radius, ...options }: SuperClusterOptions);
calculate(input: AlgorithmInput): AlgorithmOutput;
cluster({ map }: AlgorithmInput): Cluster[];
protected transformCluster({ geometry: { coordinates: [lng, lat], }, properties, }: ClusterFeature<{
marker: Marker;
}>): Cluster;
}

View File

@@ -0,0 +1,16 @@
/**
* Copyright 2021 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
export {};

View File

@@ -0,0 +1,43 @@
/**
* Copyright 2021 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { AbstractViewportAlgorithm, AlgorithmInput, AlgorithmOutput, ViewportAlgorithmOptions } from "./core";
import { SuperClusterOptions } from "./supercluster";
import SuperCluster, { ClusterFeature } from "supercluster";
import { Marker } from "../marker-utils";
import { Cluster } from "../cluster";
export interface SuperClusterViewportOptions extends SuperClusterOptions, ViewportAlgorithmOptions {
}
export interface SuperClusterViewportState {
zoom: number;
view: [number, number, number, number];
}
/**
* A very fast JavaScript algorithm for geospatial point clustering using KD trees.
*
* @see https://www.npmjs.com/package/supercluster for more information on options.
*/
export declare class SuperClusterViewportAlgorithm extends AbstractViewportAlgorithm {
protected superCluster: SuperCluster;
protected markers: Marker[];
protected clusters: Cluster[];
protected state: SuperClusterViewportState;
constructor({ maxZoom, radius, viewportPadding, ...options }: SuperClusterViewportOptions);
calculate(input: AlgorithmInput): AlgorithmOutput;
cluster({ map, mapCanvasProjection }: AlgorithmInput): Cluster[];
protected transformCluster({ geometry: { coordinates: [lng, lat], }, properties, }: ClusterFeature<{
marker: Marker;
}>): Cluster;
}

View File

@@ -0,0 +1,16 @@
/**
* Copyright 2021 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
export {};

View File

@@ -0,0 +1,56 @@
/**
* Copyright 2021 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/// <reference types="google.maps" />
import { Marker } from "../marker-utils";
/**
* Returns the markers visible in a padded map viewport
*
* @param map
* @param mapCanvasProjection
* @param markers The list of marker to filter
* @param viewportPaddingPixels The padding in pixel
* @returns The list of markers in the padded viewport
*/
export declare const filterMarkersToPaddedViewport: (map: google.maps.Map, mapCanvasProjection: google.maps.MapCanvasProjection, markers: Marker[], viewportPaddingPixels: number) => Marker[];
/**
* Extends a bounds by a number of pixels in each direction
*/
export declare const extendBoundsToPaddedViewport: (bounds: google.maps.LatLngBounds, projection: google.maps.MapCanvasProjection, numPixels: number) => google.maps.LatLngBounds;
/**
* Gets the extended bounds as a bbox [westLng, southLat, eastLng, northLat]
*/
export declare const getPaddedViewport: (bounds: google.maps.LatLngBounds, projection: google.maps.MapCanvasProjection, pixels: number) => [number, number, number, number];
/**
* Returns the distance between 2 positions.
*
* @hidden
*/
export declare const distanceBetweenPoints: (p1: google.maps.LatLngLiteral, p2: google.maps.LatLngLiteral) => number;
type PixelBounds = {
northEast: google.maps.Point;
southWest: google.maps.Point;
};
/**
* Extends a pixel bounds by numPixels in all directions.
*
* @hidden
*/
export declare const extendPixelBounds: ({ northEast, southWest }: PixelBounds, numPixels: number) => PixelBounds;
/**
* @hidden
*/
export declare const pixelBoundsToLatLngBounds: ({ northEast, southWest }: PixelBounds, projection: google.maps.MapCanvasProjection) => google.maps.LatLngBounds;
export {};

View File

@@ -0,0 +1,16 @@
/**
* Copyright 2021 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
export {};