@googlemaps/markerclusterer
Version:
Creates and manages per-zoom-level clusters for large amounts of markers.
1 lines • 89.8 kB
Source Map (JSON)
{"version":3,"file":"index.esm.mjs","sources":["../node_modules/tslib/tslib.es6.js","../src/marker-utils.ts","../src/cluster.ts","../src/utils.ts","../src/algorithms/utils.ts","../src/algorithms/core.ts","../src/algorithms/grid.ts","../src/algorithms/noop.ts","../src/algorithms/supercluster.ts","../src/algorithms/superviewport.ts","../src/renderer.ts","../src/overlay-view-safe.ts","../src/markerclusterer.ts"],"sourcesContent":["/******************************************************************************\r\nCopyright (c) Microsoft Corporation.\r\n\r\nPermission to use, copy, modify, and/or distribute this software for any\r\npurpose with or without fee is hereby granted.\r\n\r\nTHE SOFTWARE IS PROVIDED \"AS IS\" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH\r\nREGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY\r\nAND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,\r\nINDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM\r\nLOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR\r\nOTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR\r\nPERFORMANCE OF THIS SOFTWARE.\r\n***************************************************************************** */\r\n/* global Reflect, Promise, SuppressedError, Symbol, Iterator */\r\n\r\nvar extendStatics = function(d, b) {\r\n extendStatics = Object.setPrototypeOf ||\r\n ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||\r\n function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };\r\n return extendStatics(d, b);\r\n};\r\n\r\nexport function __extends(d, b) {\r\n if (typeof b !== \"function\" && b !== null)\r\n throw new TypeError(\"Class extends value \" + String(b) + \" is not a constructor or null\");\r\n extendStatics(d, b);\r\n function __() { this.constructor = d; }\r\n d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());\r\n}\r\n\r\nexport var __assign = function() {\r\n __assign = Object.assign || function __assign(t) {\r\n for (var s, i = 1, n = arguments.length; i < n; i++) {\r\n s = arguments[i];\r\n for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];\r\n }\r\n return t;\r\n }\r\n return __assign.apply(this, arguments);\r\n}\r\n\r\nexport function __rest(s, e) {\r\n var t = {};\r\n for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)\r\n t[p] = s[p];\r\n if (s != null && typeof Object.getOwnPropertySymbols === \"function\")\r\n for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {\r\n if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))\r\n t[p[i]] = s[p[i]];\r\n }\r\n return t;\r\n}\r\n\r\nexport function __decorate(decorators, target, key, desc) {\r\n var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;\r\n if (typeof Reflect === \"object\" && typeof Reflect.decorate === \"function\") r = Reflect.decorate(decorators, target, key, desc);\r\n else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;\r\n return c > 3 && r && Object.defineProperty(target, key, r), r;\r\n}\r\n\r\nexport function __param(paramIndex, decorator) {\r\n return function (target, key) { decorator(target, key, paramIndex); }\r\n}\r\n\r\nexport function __esDecorate(ctor, descriptorIn, decorators, contextIn, initializers, extraInitializers) {\r\n function accept(f) { if (f !== void 0 && typeof f !== \"function\") throw new TypeError(\"Function expected\"); return f; }\r\n var kind = contextIn.kind, key = kind === \"getter\" ? \"get\" : kind === \"setter\" ? \"set\" : \"value\";\r\n var target = !descriptorIn && ctor ? contextIn[\"static\"] ? ctor : ctor.prototype : null;\r\n var descriptor = descriptorIn || (target ? Object.getOwnPropertyDescriptor(target, contextIn.name) : {});\r\n var _, done = false;\r\n for (var i = decorators.length - 1; i >= 0; i--) {\r\n var context = {};\r\n for (var p in contextIn) context[p] = p === \"access\" ? {} : contextIn[p];\r\n for (var p in contextIn.access) context.access[p] = contextIn.access[p];\r\n context.addInitializer = function (f) { if (done) throw new TypeError(\"Cannot add initializers after decoration has completed\"); extraInitializers.push(accept(f || null)); };\r\n var result = (0, decorators[i])(kind === \"accessor\" ? { get: descriptor.get, set: descriptor.set } : descriptor[key], context);\r\n if (kind === \"accessor\") {\r\n if (result === void 0) continue;\r\n if (result === null || typeof result !== \"object\") throw new TypeError(\"Object expected\");\r\n if (_ = accept(result.get)) descriptor.get = _;\r\n if (_ = accept(result.set)) descriptor.set = _;\r\n if (_ = accept(result.init)) initializers.unshift(_);\r\n }\r\n else if (_ = accept(result)) {\r\n if (kind === \"field\") initializers.unshift(_);\r\n else descriptor[key] = _;\r\n }\r\n }\r\n if (target) Object.defineProperty(target, contextIn.name, descriptor);\r\n done = true;\r\n};\r\n\r\nexport function __runInitializers(thisArg, initializers, value) {\r\n var useValue = arguments.length > 2;\r\n for (var i = 0; i < initializers.length; i++) {\r\n value = useValue ? initializers[i].call(thisArg, value) : initializers[i].call(thisArg);\r\n }\r\n return useValue ? value : void 0;\r\n};\r\n\r\nexport function __propKey(x) {\r\n return typeof x === \"symbol\" ? x : \"\".concat(x);\r\n};\r\n\r\nexport function __setFunctionName(f, name, prefix) {\r\n if (typeof name === \"symbol\") name = name.description ? \"[\".concat(name.description, \"]\") : \"\";\r\n return Object.defineProperty(f, \"name\", { configurable: true, value: prefix ? \"\".concat(prefix, \" \", name) : name });\r\n};\r\n\r\nexport function __metadata(metadataKey, metadataValue) {\r\n if (typeof Reflect === \"object\" && typeof Reflect.metadata === \"function\") return Reflect.metadata(metadataKey, metadataValue);\r\n}\r\n\r\nexport function __awaiter(thisArg, _arguments, P, generator) {\r\n function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }\r\n return new (P || (P = Promise))(function (resolve, reject) {\r\n function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }\r\n function rejected(value) { try { step(generator[\"throw\"](value)); } catch (e) { reject(e); } }\r\n function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }\r\n step((generator = generator.apply(thisArg, _arguments || [])).next());\r\n });\r\n}\r\n\r\nexport function __generator(thisArg, body) {\r\n var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === \"function\" ? Iterator : Object).prototype);\r\n return g.next = verb(0), g[\"throw\"] = verb(1), g[\"return\"] = verb(2), typeof Symbol === \"function\" && (g[Symbol.iterator] = function() { return this; }), g;\r\n function verb(n) { return function (v) { return step([n, v]); }; }\r\n function step(op) {\r\n if (f) throw new TypeError(\"Generator is already executing.\");\r\n while (g && (g = 0, op[0] && (_ = 0)), _) try {\r\n if (f = 1, y && (t = op[0] & 2 ? y[\"return\"] : op[0] ? y[\"throw\"] || ((t = y[\"return\"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;\r\n if (y = 0, t) op = [op[0] & 2, t.value];\r\n switch (op[0]) {\r\n case 0: case 1: t = op; break;\r\n case 4: _.label++; return { value: op[1], done: false };\r\n case 5: _.label++; y = op[1]; op = [0]; continue;\r\n case 7: op = _.ops.pop(); _.trys.pop(); continue;\r\n default:\r\n if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }\r\n if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }\r\n if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }\r\n if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }\r\n if (t[2]) _.ops.pop();\r\n _.trys.pop(); continue;\r\n }\r\n op = body.call(thisArg, _);\r\n } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }\r\n if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };\r\n }\r\n}\r\n\r\nexport var __createBinding = Object.create ? (function(o, m, k, k2) {\r\n if (k2 === undefined) k2 = k;\r\n var desc = Object.getOwnPropertyDescriptor(m, k);\r\n if (!desc || (\"get\" in desc ? !m.__esModule : desc.writable || desc.configurable)) {\r\n desc = { enumerable: true, get: function() { return m[k]; } };\r\n }\r\n Object.defineProperty(o, k2, desc);\r\n}) : (function(o, m, k, k2) {\r\n if (k2 === undefined) k2 = k;\r\n o[k2] = m[k];\r\n});\r\n\r\nexport function __exportStar(m, o) {\r\n for (var p in m) if (p !== \"default\" && !Object.prototype.hasOwnProperty.call(o, p)) __createBinding(o, m, p);\r\n}\r\n\r\nexport function __values(o) {\r\n var s = typeof Symbol === \"function\" && Symbol.iterator, m = s && o[s], i = 0;\r\n if (m) return m.call(o);\r\n if (o && typeof o.length === \"number\") return {\r\n next: function () {\r\n if (o && i >= o.length) o = void 0;\r\n return { value: o && o[i++], done: !o };\r\n }\r\n };\r\n throw new TypeError(s ? \"Object is not iterable.\" : \"Symbol.iterator is not defined.\");\r\n}\r\n\r\nexport function __read(o, n) {\r\n var m = typeof Symbol === \"function\" && o[Symbol.iterator];\r\n if (!m) return o;\r\n var i = m.call(o), r, ar = [], e;\r\n try {\r\n while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);\r\n }\r\n catch (error) { e = { error: error }; }\r\n finally {\r\n try {\r\n if (r && !r.done && (m = i[\"return\"])) m.call(i);\r\n }\r\n finally { if (e) throw e.error; }\r\n }\r\n return ar;\r\n}\r\n\r\n/** @deprecated */\r\nexport function __spread() {\r\n for (var ar = [], i = 0; i < arguments.length; i++)\r\n ar = ar.concat(__read(arguments[i]));\r\n return ar;\r\n}\r\n\r\n/** @deprecated */\r\nexport function __spreadArrays() {\r\n for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;\r\n for (var r = Array(s), k = 0, i = 0; i < il; i++)\r\n for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)\r\n r[k] = a[j];\r\n return r;\r\n}\r\n\r\nexport function __spreadArray(to, from, pack) {\r\n if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {\r\n if (ar || !(i in from)) {\r\n if (!ar) ar = Array.prototype.slice.call(from, 0, i);\r\n ar[i] = from[i];\r\n }\r\n }\r\n return to.concat(ar || Array.prototype.slice.call(from));\r\n}\r\n\r\nexport function __await(v) {\r\n return this instanceof __await ? (this.v = v, this) : new __await(v);\r\n}\r\n\r\nexport function __asyncGenerator(thisArg, _arguments, generator) {\r\n if (!Symbol.asyncIterator) throw new TypeError(\"Symbol.asyncIterator is not defined.\");\r\n var g = generator.apply(thisArg, _arguments || []), i, q = [];\r\n return i = Object.create((typeof AsyncIterator === \"function\" ? AsyncIterator : Object).prototype), verb(\"next\"), verb(\"throw\"), verb(\"return\", awaitReturn), i[Symbol.asyncIterator] = function () { return this; }, i;\r\n function awaitReturn(f) { return function (v) { return Promise.resolve(v).then(f, reject); }; }\r\n function verb(n, f) { if (g[n]) { i[n] = function (v) { return new Promise(function (a, b) { q.push([n, v, a, b]) > 1 || resume(n, v); }); }; if (f) i[n] = f(i[n]); } }\r\n function resume(n, v) { try { step(g[n](v)); } catch (e) { settle(q[0][3], e); } }\r\n function step(r) { r.value instanceof __await ? Promise.resolve(r.value.v).then(fulfill, reject) : settle(q[0][2], r); }\r\n function fulfill(value) { resume(\"next\", value); }\r\n function reject(value) { resume(\"throw\", value); }\r\n function settle(f, v) { if (f(v), q.shift(), q.length) resume(q[0][0], q[0][1]); }\r\n}\r\n\r\nexport function __asyncDelegator(o) {\r\n var i, p;\r\n return i = {}, verb(\"next\"), verb(\"throw\", function (e) { throw e; }), verb(\"return\"), i[Symbol.iterator] = function () { return this; }, i;\r\n function verb(n, f) { i[n] = o[n] ? function (v) { return (p = !p) ? { value: __await(o[n](v)), done: false } : f ? f(v) : v; } : f; }\r\n}\r\n\r\nexport function __asyncValues(o) {\r\n if (!Symbol.asyncIterator) throw new TypeError(\"Symbol.asyncIterator is not defined.\");\r\n var m = o[Symbol.asyncIterator], i;\r\n return m ? m.call(o) : (o = typeof __values === \"function\" ? __values(o) : o[Symbol.iterator](), i = {}, verb(\"next\"), verb(\"throw\"), verb(\"return\"), i[Symbol.asyncIterator] = function () { return this; }, i);\r\n function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }\r\n function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }\r\n}\r\n\r\nexport function __makeTemplateObject(cooked, raw) {\r\n if (Object.defineProperty) { Object.defineProperty(cooked, \"raw\", { value: raw }); } else { cooked.raw = raw; }\r\n return cooked;\r\n};\r\n\r\nvar __setModuleDefault = Object.create ? (function(o, v) {\r\n Object.defineProperty(o, \"default\", { enumerable: true, value: v });\r\n}) : function(o, v) {\r\n o[\"default\"] = v;\r\n};\r\n\r\nvar ownKeys = function(o) {\r\n ownKeys = Object.getOwnPropertyNames || function (o) {\r\n var ar = [];\r\n for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;\r\n return ar;\r\n };\r\n return ownKeys(o);\r\n};\r\n\r\nexport function __importStar(mod) {\r\n if (mod && mod.__esModule) return mod;\r\n var result = {};\r\n if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== \"default\") __createBinding(result, mod, k[i]);\r\n __setModuleDefault(result, mod);\r\n return result;\r\n}\r\n\r\nexport function __importDefault(mod) {\r\n return (mod && mod.__esModule) ? mod : { default: mod };\r\n}\r\n\r\nexport function __classPrivateFieldGet(receiver, state, kind, f) {\r\n if (kind === \"a\" && !f) throw new TypeError(\"Private accessor was defined without a getter\");\r\n if (typeof state === \"function\" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError(\"Cannot read private member from an object whose class did not declare it\");\r\n return kind === \"m\" ? f : kind === \"a\" ? f.call(receiver) : f ? f.value : state.get(receiver);\r\n}\r\n\r\nexport function __classPrivateFieldSet(receiver, state, value, kind, f) {\r\n if (kind === \"m\") throw new TypeError(\"Private method is not writable\");\r\n if (kind === \"a\" && !f) throw new TypeError(\"Private accessor was defined without a setter\");\r\n if (typeof state === \"function\" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError(\"Cannot write private member to an object whose class did not declare it\");\r\n return (kind === \"a\" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;\r\n}\r\n\r\nexport function __classPrivateFieldIn(state, receiver) {\r\n if (receiver === null || (typeof receiver !== \"object\" && typeof receiver !== \"function\")) throw new TypeError(\"Cannot use 'in' operator on non-object\");\r\n return typeof state === \"function\" ? receiver === state : state.has(receiver);\r\n}\r\n\r\nexport function __addDisposableResource(env, value, async) {\r\n if (value !== null && value !== void 0) {\r\n if (typeof value !== \"object\" && typeof value !== \"function\") throw new TypeError(\"Object expected.\");\r\n var dispose, inner;\r\n if (async) {\r\n if (!Symbol.asyncDispose) throw new TypeError(\"Symbol.asyncDispose is not defined.\");\r\n dispose = value[Symbol.asyncDispose];\r\n }\r\n if (dispose === void 0) {\r\n if (!Symbol.dispose) throw new TypeError(\"Symbol.dispose is not defined.\");\r\n dispose = value[Symbol.dispose];\r\n if (async) inner = dispose;\r\n }\r\n if (typeof dispose !== \"function\") throw new TypeError(\"Object not disposable.\");\r\n if (inner) dispose = function() { try { inner.call(this); } catch (e) { return Promise.reject(e); } };\r\n env.stack.push({ value: value, dispose: dispose, async: async });\r\n }\r\n else if (async) {\r\n env.stack.push({ async: true });\r\n }\r\n return value;\r\n\r\n}\r\n\r\nvar _SuppressedError = typeof SuppressedError === \"function\" ? SuppressedError : function (error, suppressed, message) {\r\n var e = new Error(message);\r\n return e.name = \"SuppressedError\", e.error = error, e.suppressed = suppressed, e;\r\n};\r\n\r\nexport function __disposeResources(env) {\r\n function fail(e) {\r\n env.error = env.hasError ? new _SuppressedError(e, env.error, \"An error was suppressed during disposal.\") : e;\r\n env.hasError = true;\r\n }\r\n var r, s = 0;\r\n function next() {\r\n while (r = env.stack.pop()) {\r\n try {\r\n if (!r.async && s === 1) return s = 0, env.stack.push(r), Promise.resolve().then(next);\r\n if (r.dispose) {\r\n var result = r.dispose.call(r.value);\r\n if (r.async) return s |= 2, Promise.resolve(result).then(next, function(e) { fail(e); return next(); });\r\n }\r\n else s |= 1;\r\n }\r\n catch (e) {\r\n fail(e);\r\n }\r\n }\r\n if (s === 1) return env.hasError ? Promise.reject(env.error) : Promise.resolve();\r\n if (env.hasError) throw env.error;\r\n }\r\n return next();\r\n}\r\n\r\nexport function __rewriteRelativeImportExtension(path, preserveJsx) {\r\n if (typeof path === \"string\" && /^\\.\\.?\\//.test(path)) {\r\n return path.replace(/\\.(tsx)$|((?:\\.d)?)((?:\\.[^./]+?)?)\\.([cm]?)ts$/i, function (m, tsx, d, ext, cm) {\r\n return tsx ? preserveJsx ? \".jsx\" : \".js\" : d && (!ext || !cm) ? m : (d + ext + \".\" + cm.toLowerCase() + \"js\");\r\n });\r\n }\r\n return path;\r\n}\r\n\r\nexport default {\r\n __extends: __extends,\r\n __assign: __assign,\r\n __rest: __rest,\r\n __decorate: __decorate,\r\n __param: __param,\r\n __esDecorate: __esDecorate,\r\n __runInitializers: __runInitializers,\r\n __propKey: __propKey,\r\n __setFunctionName: __setFunctionName,\r\n __metadata: __metadata,\r\n __awaiter: __awaiter,\r\n __generator: __generator,\r\n __createBinding: __createBinding,\r\n __exportStar: __exportStar,\r\n __values: __values,\r\n __read: __read,\r\n __spread: __spread,\r\n __spreadArrays: __spreadArrays,\r\n __spreadArray: __spreadArray,\r\n __await: __await,\r\n __asyncGenerator: __asyncGenerator,\r\n __asyncDelegator: __asyncDelegator,\r\n __asyncValues: __asyncValues,\r\n __makeTemplateObject: __makeTemplateObject,\r\n __importStar: __importStar,\r\n __importDefault: __importDefault,\r\n __classPrivateFieldGet: __classPrivateFieldGet,\r\n __classPrivateFieldSet: __classPrivateFieldSet,\r\n __classPrivateFieldIn: __classPrivateFieldIn,\r\n __addDisposableResource: __addDisposableResource,\r\n __disposeResources: __disposeResources,\r\n __rewriteRelativeImportExtension: __rewriteRelativeImportExtension,\r\n};\r\n","/**\n * Copyright 2023 Google LLC\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\n/**\n * Supports markers of either \"legacy\" or \"advanced\" types.\n */\nexport type Marker =\n | google.maps.Marker\n | google.maps.marker.AdvancedMarkerElement;\n\n/**\n * util class that creates a common set of convenience functions to wrap\n * shared behavior of Advanced Markers and Markers.\n */\nexport class MarkerUtils {\n public static isAdvancedMarkerAvailable(map: google.maps.Map): boolean {\n return (\n google.maps.marker &&\n map.getMapCapabilities().isAdvancedMarkersAvailable === true\n );\n }\n\n public static isAdvancedMarker(\n marker: Marker\n ): marker is google.maps.marker.AdvancedMarkerElement {\n return (\n google.maps.marker &&\n marker instanceof google.maps.marker.AdvancedMarkerElement\n );\n }\n\n public static setMap(marker: Marker, map: google.maps.Map | null) {\n if (this.isAdvancedMarker(marker)) {\n marker.map = map;\n } else {\n marker.setMap(map);\n }\n }\n\n public static getPosition(marker: Marker): google.maps.LatLng {\n // SuperClusterAlgorithm.calculate expects a LatLng instance so we fake it for Adv Markers\n if (this.isAdvancedMarker(marker)) {\n if (marker.position) {\n if (marker.position instanceof google.maps.LatLng) {\n return marker.position;\n }\n // since we can't cast to LatLngLiteral for reasons =(\n if (\n Number.isFinite(marker.position.lat) &&\n Number.isFinite(marker.position.lng)\n ) {\n return new google.maps.LatLng(\n marker.position.lat,\n marker.position.lng\n );\n }\n }\n\n // @ts-ignore\n return new google.maps.LatLng(null);\n }\n\n return marker.getPosition()!;\n }\n\n public static getVisible(marker: Marker) {\n if (this.isAdvancedMarker(marker)) {\n /**\n * Always return true for Advanced Markers because the clusterer\n * uses getVisible as a way to count legacy markers not as an actual\n * indicator of visibility for some reason. Even when markers are hidden\n * Marker.getVisible returns `true` and this is used to set the marker count\n * on the cluster. See the behavior of Cluster.count\n */\n return true;\n }\n return marker.getVisible();\n }\n}\n","/**\n * Copyright 2021 Google LLC\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { MarkerUtils, Marker } from \"./marker-utils\";\n\nexport interface ClusterOptions {\n position?: google.maps.LatLng | google.maps.LatLngLiteral;\n markers?: Marker[];\n}\n\nexport class Cluster {\n public marker?: Marker;\n public readonly markers: Marker[] = [];\n protected _position?: google.maps.LatLng;\n\n constructor({ markers, position }: ClusterOptions) {\n if (markers) this.markers = markers;\n if (position) {\n if (position instanceof google.maps.LatLng) {\n this._position = position;\n } else {\n this._position = new google.maps.LatLng(position);\n }\n }\n }\n\n public get bounds(): google.maps.LatLngBounds | undefined {\n if (this.markers.length === 0 && !this._position) {\n return;\n }\n\n const bounds = new google.maps.LatLngBounds(this._position, this._position);\n for (const marker of this.markers) {\n bounds.extend(MarkerUtils.getPosition(marker));\n }\n return bounds;\n }\n\n public get position(): google.maps.LatLng {\n // @ts-ignore\n return this._position || this.bounds.getCenter();\n }\n\n /**\n * Get the count of **visible** markers.\n */\n public get count(): number {\n return this.markers.filter((m: Marker) => MarkerUtils.getVisible(m)).length;\n }\n\n /**\n * Add a marker to the cluster.\n */\n public push(marker: Marker): void {\n this.markers.push(marker);\n }\n\n /**\n * Cleanup references and remove marker from map.\n */\n public delete(): void {\n if (this.marker) {\n MarkerUtils.setMap(this.marker, null);\n this.marker = undefined;\n }\n this.markers.length = 0;\n }\n}\n","/**\n * Copyright 2021 Google LLC\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\n/**\n * A typescript assertion function used in cases where typescript has to be\n * convinced that the object in question can not be null.\n *\n * @param value\n * @param message\n */\nexport function assertNotNull<TValue>(\n value: TValue,\n message: string = \"assertion failed\"\n): asserts value is NonNullable<TValue> {\n if (value === null || value === undefined) {\n throw Error(message);\n }\n}\n","/**\n * Copyright 2021 Google LLC\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { MarkerUtils, Marker } from \"../marker-utils\";\nimport { assertNotNull } from \"../utils\";\n\n/**\n * Returns the markers visible in a padded map viewport\n *\n * @param map\n * @param mapCanvasProjection\n * @param markers The list of marker to filter\n * @param viewportPaddingPixels The padding in pixel\n * @returns The list of markers in the padded viewport\n */\nexport const filterMarkersToPaddedViewport = (\n map: google.maps.Map,\n mapCanvasProjection: google.maps.MapCanvasProjection,\n markers: Marker[],\n viewportPaddingPixels: number\n): Marker[] => {\n const bounds = map.getBounds();\n assertNotNull(bounds);\n\n const extendedMapBounds = extendBoundsToPaddedViewport(\n bounds,\n mapCanvasProjection,\n viewportPaddingPixels\n );\n\n return markers.filter((marker) =>\n extendedMapBounds.contains(MarkerUtils.getPosition(marker))\n );\n};\n\n/**\n * Extends bounds by a number of pixels in each direction\n */\nexport const extendBoundsToPaddedViewport = (\n bounds: google.maps.LatLngBounds,\n projection: google.maps.MapCanvasProjection,\n numPixels: number\n): google.maps.LatLngBounds => {\n const { northEast, southWest } = latLngBoundsToPixelBounds(\n bounds,\n projection\n );\n const extendedPixelBounds = extendPixelBounds(\n { northEast, southWest },\n numPixels\n );\n return pixelBoundsToLatLngBounds(extendedPixelBounds, projection);\n};\n\n/**\n * Gets the extended bounds as a bbox [westLng, southLat, eastLng, northLat]\n */\nexport const getPaddedViewport = (\n bounds: google.maps.LatLngBounds,\n projection: google.maps.MapCanvasProjection,\n pixels: number\n): [number, number, number, number] => {\n const extended = extendBoundsToPaddedViewport(bounds, projection, pixels);\n const ne = extended.getNorthEast();\n const sw = extended.getSouthWest();\n\n return [sw.lng(), sw.lat(), ne.lng(), ne.lat()];\n};\n\n/**\n * Returns the distance between 2 positions.\n *\n * @hidden\n */\nexport const distanceBetweenPoints = (\n p1: google.maps.LatLngLiteral,\n p2: google.maps.LatLngLiteral\n): number => {\n const R = 6371; // Radius of the Earth in km\n const dLat = ((p2.lat - p1.lat) * Math.PI) / 180;\n const dLon = ((p2.lng - p1.lng) * Math.PI) / 180;\n const sinDLat = Math.sin(dLat / 2);\n const sinDLon = Math.sin(dLon / 2);\n const a =\n sinDLat * sinDLat +\n Math.cos((p1.lat * Math.PI) / 180) *\n Math.cos((p2.lat * Math.PI) / 180) *\n sinDLon *\n sinDLon;\n const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));\n return R * c;\n};\n\ntype PixelBounds = {\n northEast: google.maps.Point;\n southWest: google.maps.Point;\n};\n\n/**\n * Converts a LatLng bound to pixels.\n *\n * @hidden\n */\nconst latLngBoundsToPixelBounds = (\n bounds: google.maps.LatLngBounds,\n projection: google.maps.MapCanvasProjection\n): PixelBounds => {\n const northEast = projection.fromLatLngToDivPixel(bounds.getNorthEast());\n const southWest = projection.fromLatLngToDivPixel(bounds.getSouthWest());\n\n assertNotNull(northEast);\n assertNotNull(southWest);\n\n return { northEast, southWest };\n};\n\n/**\n * Extends a pixel bounds by numPixels in all directions.\n *\n * @hidden\n */\nexport const extendPixelBounds = (\n { northEast, southWest }: PixelBounds,\n numPixels: number\n): PixelBounds => {\n northEast.x += numPixels;\n northEast.y -= numPixels;\n\n southWest.x -= numPixels;\n southWest.y += numPixels;\n\n return { northEast, southWest };\n};\n\n/**\n * @hidden\n */\nexport const pixelBoundsToLatLngBounds = (\n { northEast, southWest }: PixelBounds,\n projection: google.maps.MapCanvasProjection\n): google.maps.LatLngBounds => {\n const sw = projection.fromDivPixelToLatLng(southWest);\n const ne = projection.fromDivPixelToLatLng(northEast);\n return new google.maps.LatLngBounds(sw, ne);\n};\n","/**\n * Copyright 2021 Google LLC\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { Cluster } from \"../cluster\";\nimport { filterMarkersToPaddedViewport } from \"./utils\";\nimport { MarkerUtils, Marker } from \"../marker-utils\";\nimport { assertNotNull } from \"../utils\";\n\nexport interface AlgorithmInput {\n /**\n * The map containing the markers and clusters.\n */\n map: google.maps.Map;\n /**\n * An array of markers to be clustered.\n *\n * There are some specific edge cases to be aware of including the following:\n * * Markers that are not visible.\n */\n markers: Marker[];\n /**\n * The `mapCanvasProjection` enables easy conversion from lat/lng to pixel.\n *\n * @see [MapCanvasProjection](https://developers.google.com/maps/documentation/javascript/reference/overlay-view#MapCanvasProjection)\n */\n mapCanvasProjection: google.maps.MapCanvasProjection;\n}\n\nexport interface AlgorithmOutput {\n /**\n * The clusters returned based upon the {@link AlgorithmInput}.\n */\n clusters: Cluster[];\n /**\n * A boolean flag indicating that the clusters have not changed.\n */\n changed?: boolean;\n}\n\nexport interface Algorithm {\n /**\n * Calculates an array of {@link Cluster}.\n */\n calculate: ({ markers, map }: AlgorithmInput) => AlgorithmOutput;\n}\n\nexport interface AlgorithmOptions {\n // Markers are not clustered at maxZoom and above.\n maxZoom?: number;\n}\n\n/**\n * @hidden\n */\nexport abstract class AbstractAlgorithm implements Algorithm {\n protected maxZoom: number;\n\n constructor({ maxZoom = 16 }: AlgorithmOptions) {\n this.maxZoom = maxZoom;\n }\n /**\n * Helper function to bypass clustering based upon some map state such as\n * zoom, number of markers, etc.\n *\n * ```typescript\n * cluster({markers, map}: AlgorithmInput): Cluster[] {\n * if (shouldBypassClustering(map)) {\n * return this.noop({markers})\n * }\n * }\n * ```\n */\n protected noop<T extends Pick<AlgorithmInput, \"markers\">>({\n markers,\n }: T): Cluster[] {\n return noop(markers);\n }\n /**\n * Calculates an array of {@link Cluster}. Calculate is separate from\n * {@link cluster} as it does preprocessing on the markers such as filtering\n * based upon the viewport as in {@link AbstractViewportAlgorithm}. Caching\n * and other optimizations can also be done here.\n */\n public abstract calculate({ markers, map }: AlgorithmInput): AlgorithmOutput;\n\n /**\n * Clusters the markers and called from {@link calculate}.\n */\n protected abstract cluster({ markers, map }: AlgorithmInput): Cluster[];\n}\n\n/**\n * @hidden\n */\nexport interface ViewportAlgorithmOptions extends AlgorithmOptions {\n /**\n * The number of pixels to extend beyond the viewport bounds when filtering\n * markers prior to clustering.\n */\n viewportPadding?: number;\n}\n\n/**\n * Abstract viewport algorithm proves a class to filter markers by a padded\n * viewport. This is a common optimization.\n *\n * @hidden\n */\nexport abstract class AbstractViewportAlgorithm extends AbstractAlgorithm {\n protected viewportPadding = 60;\n\n constructor({ viewportPadding = 60, ...options }: ViewportAlgorithmOptions) {\n super(options);\n this.viewportPadding = viewportPadding;\n }\n public calculate({\n markers,\n map,\n mapCanvasProjection,\n }: AlgorithmInput): AlgorithmOutput {\n const zoom = map.getZoom();\n\n assertNotNull(zoom);\n\n if (zoom >= this.maxZoom) {\n return {\n clusters: this.noop({\n markers,\n }),\n changed: false,\n };\n }\n\n return {\n clusters: this.cluster({\n markers: filterMarkersToPaddedViewport(\n map,\n mapCanvasProjection,\n markers,\n this.viewportPadding\n ),\n map,\n mapCanvasProjection,\n }),\n };\n }\n protected abstract cluster({ markers, map }: AlgorithmInput): Cluster[];\n}\n\n/**\n * @hidden\n */\nexport const noop = (markers: Marker[]): Cluster[] => {\n const clusters = markers.map(\n (marker) =>\n new Cluster({\n position: MarkerUtils.getPosition(marker),\n markers: [marker],\n })\n );\n return clusters;\n};\n","/**\n * Copyright 2021 Google LLC\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport {\n AbstractViewportAlgorithm,\n AlgorithmInput,\n AlgorithmOutput,\n ViewportAlgorithmOptions,\n} from \"./core\";\nimport {\n distanceBetweenPoints,\n extendBoundsToPaddedViewport,\n filterMarkersToPaddedViewport,\n} from \"./utils\";\n\nimport { Cluster } from \"../cluster\";\nimport { deepEqual } from \"fast-equals\";\nimport { MarkerUtils, Marker } from \"../marker-utils\";\nimport { assertNotNull } from \"../utils\";\n\nexport interface GridOptions extends ViewportAlgorithmOptions {\n gridSize?: number;\n /**\n * Max distance between cluster center and point in meters.\n * @default 10000\n */\n maxDistance?: number;\n}\n\n/**\n * The default Grid algorithm historically used in Google Maps marker\n * clustering.\n *\n * The Grid algorithm does not implement caching and markers may flash as the\n * viewport changes. Instead use {@link SuperClusterAlgorithm}.\n */\nexport class GridAlgorithm extends AbstractViewportAlgorithm {\n protected gridSize: number;\n protected maxDistance: number;\n protected clusters: Cluster[] = [];\n protected state = { zoom: -1 };\n\n constructor({ maxDistance = 40000, gridSize = 40, ...options }: GridOptions) {\n super(options);\n\n this.maxDistance = maxDistance;\n this.gridSize = gridSize;\n }\n\n public calculate({\n markers,\n map,\n mapCanvasProjection,\n }: AlgorithmInput): AlgorithmOutput {\n const zoom = map.getZoom();\n\n assertNotNull(zoom);\n\n const newState = { zoom };\n let changed = false;\n if (this.state.zoom >= this.maxZoom && newState.zoom >= this.maxZoom) {\n // still at or beyond maxZoom, no change\n } else {\n changed = !deepEqual(this.state, newState);\n }\n\n this.state = newState;\n\n if (zoom >= this.maxZoom) {\n return {\n clusters: this.noop({ markers }),\n changed,\n };\n }\n\n return {\n clusters: this.cluster({\n markers: filterMarkersToPaddedViewport(\n map,\n mapCanvasProjection,\n markers,\n this.viewportPadding\n ),\n map,\n mapCanvasProjection,\n }),\n };\n }\n\n protected cluster({\n markers,\n map,\n mapCanvasProjection,\n }: AlgorithmInput): Cluster[] {\n this.clusters = [];\n markers.forEach((marker) => {\n this.addToClosestCluster(marker, map, mapCanvasProjection);\n });\n\n return this.clusters;\n }\n\n protected addToClosestCluster(\n marker: Marker,\n map: google.maps.Map,\n projection: google.maps.MapCanvasProjection\n ): void {\n let maxDistance = this.maxDistance; // Some large number\n let cluster: Cluster | null = null;\n\n for (let i = 0; i < this.clusters.length; i++) {\n const candidate = this.clusters[i];\n\n assertNotNull(candidate.bounds);\n\n const distance = distanceBetweenPoints(\n candidate.bounds.getCenter().toJSON(),\n MarkerUtils.getPosition(marker).toJSON()\n );\n\n if (distance < maxDistance) {\n maxDistance = distance;\n cluster = candidate;\n }\n }\n\n if (cluster) {\n assertNotNull(cluster.bounds);\n\n if (\n extendBoundsToPaddedViewport(\n cluster.bounds,\n projection,\n this.gridSize\n ).contains(MarkerUtils.getPosition(marker))\n ) {\n cluster.push(marker);\n } else {\n const cluster = new Cluster({ markers: [marker] });\n this.clusters.push(cluster);\n }\n } else {\n const cluster = new Cluster({ markers: [marker] });\n this.clusters.push(cluster);\n }\n }\n}\n","/**\n * Copyright 2021 Google LLC\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport {\n AbstractAlgorithm,\n AlgorithmInput,\n AlgorithmOptions,\n AlgorithmOutput,\n} from \"./core\";\n\nimport { Cluster } from \"../cluster\";\n\n/**\n * Noop algorithm does not generate any clusters or filter markers by the an extended viewport.\n */\nexport class NoopAlgorithm extends AbstractAlgorithm {\n constructor({ ...options }: AlgorithmOptions) {\n super(options);\n }\n public calculate({\n markers,\n map,\n mapCanvasProjection,\n }: AlgorithmInput): AlgorithmOutput {\n return {\n clusters: this.cluster({ markers, map, mapCanvasProjection }),\n changed: false,\n };\n }\n\n protected cluster(input: AlgorithmInput): Cluster[] {\n return this.noop(input);\n }\n}\n","/**\n * Copyright 2021 Google LLC\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { AbstractAlgorithm, AlgorithmInput, AlgorithmOutput } from \"./core\";\nimport SuperCluster, { ClusterFeature } from \"supercluster\";\nimport { MarkerUtils, Marker } from \"../marker-utils\";\nimport { Cluster } from \"../cluster\";\nimport { deepEqual } from \"fast-equals\";\nimport { assertNotNull } from \"../utils\";\n\nexport type SuperClusterOptions = SuperCluster.Options<\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n { [name: string]: any },\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n { [name: string]: any }\n>;\n\n/**\n * A very fast JavaScript algorithm for geospatial point clustering using KD trees.\n *\n * @see https://www.npmjs.com/package/supercluster for more information on options.\n */\nexport class SuperClusterAlgorithm extends AbstractAlgorithm {\n protected superCluster: SuperCluster;\n protected markers: Marker[] = [];\n protected clusters: Cluster[] = [];\n protected state = { zoom: -1 };\n\n constructor({ maxZoom, radius = 60, ...options }: SuperClusterOptions) {\n super({ maxZoom });\n\n this.superCluster = new SuperCluster({\n maxZoom: this.maxZoom,\n radius,\n ...options,\n });\n }\n\n public calculate(input: AlgorithmInput): AlgorithmOutput {\n let changed = false;\n let zoom = input.map.getZoom();\n\n assertNotNull(zoom);\n\n zoom = Math.round(zoom);\n\n const state = { zoom: zoom };\n\n if (!deepEqual(input.markers, this.markers)) {\n changed = true;\n // TODO use proxy to avoid copy?\n this.markers = [...input.markers];\n\n const points = this.markers.map((marker) => {\n const position = MarkerUtils.getPosition(marker);\n const coordinates = [position.lng(), position.lat()];\n return {\n type: \"Feature\",\n geometry: { type: \"Point\", coordinates },\n properties: { marker },\n } as const;\n });\n this.superCluster.load(points);\n }\n\n if (!changed) {\n if (this.state.zoom <= this.maxZoom || state.zoom <= this.maxZoom) {\n changed = !deepEqual(this.state, state);\n }\n }\n\n this.state = state;\n\n // when input is empty, return right away\n if (input.markers.length === 0) {\n this.clusters = [];\n\n return { clusters: this.clusters, changed };\n }\n\n if (changed) {\n this.clusters = this.cluster(input);\n }\n\n return { clusters: this.clusters, changed };\n }\n\n public cluster({ map }: AlgorithmInput): Cluster[] {\n const zoom = map.getZoom();\n assertNotNull(zoom);\n\n return this.superCluster\n .getClusters([-180, -90, 180, 90], Math.round(zoom))\n .map((feature) =>\n this.transformCluster(feature as ClusterFeature<{ marker: Marker }>)\n );\n }\n\n protected transformCluster({\n geometry: {\n coordinates: [lng, lat],\n },\n properties,\n }: ClusterFeature<{ marker: Marker }>): Cluster {\n if (properties.cluster) {\n return new Cluster({\n markers: this.superCluster\n .getLeaves(properties.cluster_id, Infinity)\n .map((leaf) => leaf.properties.marker),\n position: { lat, lng },\n });\n }\n\n const marker = properties.marker;\n\n return new Cluster({\n markers: [marker],\n position: MarkerUtils.getPosition(marker),\n });\n }\n}\n","/**\n * Copyright 2021 Google LLC\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport {\n AbstractViewportAlgorithm,\n AlgorithmInput,\n AlgorithmOutput,\n ViewportAlgorithmOptions,\n} from \"./core\";\nimport { SuperClusterOptions } from \"./supercluster\";\nimport SuperCluster, { ClusterFeature } from \"supercluster\";\nimport { MarkerUtils, Marker } from \"../marker-utils\";\nimport { Cluster } from \"../cluster\";\nimport { getPaddedViewport } from \"./utils\";\nimport { deepEqual } from \"fast-equals\";\nimport { assertNotNull } from \"../utils\";\n\nexport interface SuperClusterViewportOptions\n extends SuperClusterOptions,\n ViewportAlgorithmOptions {}\n\nexport interface SuperClusterViewportState {\n /* The current zoom level */\n zoom: number;\n\n /* The current viewport as a bbox [westLng, southLat, eastLng, northLat] */\n view: [number, number, number, number];\n}\n\n/**\n * A very fast JavaScript algorithm for geospatial point clustering using KD trees.\n *\n * @see https://www.npmjs.com/package/supercluster for more information on options.\n */\nexport class SuperClusterViewportAlgorithm extends AbstractViewportAlgorithm {\n protected superCluster: SuperCluster;\n protected markers: Marker[] = [];\n protected clusters: Cluster[] = [];\n protected state: SuperClusterViewportState;\n\n constructor({\n maxZoom,\n radius = 60,\n viewportPadding = 60,\n ...options\n }: SuperClusterViewportOptions) {\n super({ maxZoom, viewportPadding });\n\n this.superCluster = new SuperCluster({\n maxZoom: this.maxZoom,\n radius,\n ...options,\n });\n\n this.state = { zoom: -1, view: [0, 0, 0, 0] };\n }\n\n public calculate(input: AlgorithmInput): AlgorithmOutput {\n const state = this.getViewportState(input);\n\n let changed = !deepEqual(this.state, state);\n if (!deepEqual(input.markers, this.markers)) {\n changed = true;\n // TODO use proxy to avoid copy?\n this.markers = [...input.markers];\n\n const points = this.markers.map((marker) => {\n const position = MarkerUtils.getPosition(marker);\n const coordinates = [position.lng(), position.lat()];\n return {\n type: \"Feature\" as const,\n geometry: {\n type: \"Point\" as const,\n coordinates,\n },\n properties: { marker },\n };\n });\n this.superCluster.load(points);\n }\n\n if (changed) {\n this.clusters = this.cluster(input);\n this.state = state;\n }\n\n return { clusters: this.clusters, changed };\n }\n\n public cluster(input: AlgorithmInput): Cluster[] {\n /* recalculate new state because we can't use the cached version. */\n const state = this.getViewportState(input);\n\n return this.superCluster\n .getClusters(state.view, state.zoom)\n .map((feature) =>\n this.transformCluster(feature as ClusterFeature<{ marker: Marker }>)\n );\n }\n\n protected transformCluster({\n geometry: {\n coordinates: [lng, lat],\n },\n properties,\n }: ClusterFeature<{ marker: Marker }>): Cluster {\n if (properties.cluster) {\n return new Cluster({\n markers: this.superCluster\n .getLeaves(properties.cluster_id, Infinity)\n .map((leaf) => leaf.properties.marker),\n position: { lat, lng },\n });\n }\n\n const marker = properties.marker;\n\n return new Cluster({\n markers: [marker],\n position: MarkerUtils.getPosition(marker),\n });\n }\n\n protected getViewportState(input: AlgorithmInput): SuperClusterViewportState {\n const mapZoom = i