@react-google-maps/marker-clusterer 2.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/esm.js ADDED
@@ -0,0 +1,936 @@
1
+ var ClusterIcon = /** @class */ (function () {
2
+ function ClusterIcon(cluster, styles) {
3
+ cluster.getClusterer().extend(ClusterIcon, google.maps.OverlayView);
4
+ this.cluster = cluster;
5
+ this.className = this.cluster.getClusterer().getClusterClass();
6
+ this.styles = styles;
7
+ this.center = undefined;
8
+ this.div = null;
9
+ this.sums = null;
10
+ this.visible = false;
11
+ this.boundsChangedListener = null;
12
+ this.url = '';
13
+ this.height = 0;
14
+ this.width = 0;
15
+ this.anchorText = [0, 0];
16
+ this.anchorIcon = [0, 0];
17
+ this.textColor = 'black';
18
+ this.textSize = 11;
19
+ this.textDecoration = 'none';
20
+ this.fontWeight = 'bold';
21
+ this.fontStyle = 'normal';
22
+ this.fontFamily = 'Arial,sans-serif';
23
+ this.backgroundPosition = '0 0';
24
+ // eslint-disable-next-line @typescript-eslint/ban-ts-ignore
25
+ // @ts-ignore
26
+ this.setMap(cluster.getMap()); // Note: this causes onAdd to be called
27
+ }
28
+ ClusterIcon.prototype.onAdd = function () {
29
+ var _this = this;
30
+ var cMouseDownInCluster;
31
+ var cDraggingMapByCluster;
32
+ this.div = document.createElement('div');
33
+ this.div.className = this.className;
34
+ if (this.visible) {
35
+ this.show();
36
+ }
37
+ // eslint-disable-next-line @typescript-eslint/ban-ts-ignore
38
+ // @ts-ignore
39
+ this.getPanes().overlayMouseTarget.appendChild(this.div);
40
+ // Fix for Issue 157
41
+ this.boundsChangedListener = google.maps.event.addListener(
42
+ // eslint-disable-next-line @typescript-eslint/ban-ts-ignore
43
+ // @ts-ignore
44
+ this.getMap(), 'boundschanged', function boundsChanged() {
45
+ cDraggingMapByCluster = cMouseDownInCluster;
46
+ });
47
+ google.maps.event.addDomListener(this.div, 'mousedown', function onMouseDown() {
48
+ cMouseDownInCluster = true;
49
+ cDraggingMapByCluster = false;
50
+ });
51
+ // eslint-disable-next-line @getify/proper-arrows/this, @getify/proper-arrows/name
52
+ google.maps.event.addDomListener(this.div, 'click',
53
+ // eslint-disable-next-line @getify/proper-arrows/this, @getify/proper-arrows/name
54
+ function (event) {
55
+ cMouseDownInCluster = false;
56
+ if (!cDraggingMapByCluster) {
57
+ var markerClusterer_1 = _this.cluster.getClusterer();
58
+ /**
59
+ * This event is fired when a cluster marker is clicked.
60
+ * @name MarkerClusterer#click
61
+ * @param {Cluster} c The cluster that was clicked.
62
+ * @event
63
+ */
64
+ google.maps.event.trigger(markerClusterer_1, 'click', _this.cluster);
65
+ google.maps.event.trigger(markerClusterer_1, 'clusterclick', _this.cluster); // deprecated name
66
+ // The default click handler follows. Disable it by setting
67
+ // the zoomOnClick property to false.
68
+ if (markerClusterer_1.getZoomOnClick()) {
69
+ // Zoom into the cluster.
70
+ var maxZoom_1 = markerClusterer_1.getMaxZoom();
71
+ var bounds_1 = _this.cluster.getBounds();
72
+ // eslint-disable-next-line @typescript-eslint/ban-ts-ignore
73
+ // @ts-ignore
74
+ markerClusterer_1.getMap().fitBounds(bounds_1);
75
+ // There is a fix for Issue 170 here:
76
+ setTimeout(function timeout() {
77
+ // eslint-disable-next-line @typescript-eslint/ban-ts-ignore
78
+ // @ts-ignore
79
+ markerClusterer_1.getMap().fitBounds(bounds_1);
80
+ // Don't zoom beyond the max zoom level
81
+ // eslint-disable-next-line @typescript-eslint/ban-ts-ignore
82
+ // @ts-ignore
83
+ if (maxZoom_1 !== null && markerClusterer_1.getMap().getZoom() > maxZoom_1) {
84
+ // eslint-disable-next-line @typescript-eslint/ban-ts-ignore
85
+ // @ts-ignore
86
+ markerClusterer_1.getMap().setZoom(maxZoom_1 + 1);
87
+ }
88
+ }, 100);
89
+ }
90
+ // Prevent event propagation to the map:
91
+ event.cancelBubble = true;
92
+ if (event.stopPropagation) {
93
+ event.stopPropagation();
94
+ }
95
+ }
96
+ });
97
+ google.maps.event.addDomListener(this.div, 'mouseover',
98
+ // eslint-disable-next-line @getify/proper-arrows/this, @getify/proper-arrows/name
99
+ function () {
100
+ /**
101
+ * This event is fired when the mouse moves over a cluster marker.
102
+ * @name MarkerClusterer#mouseover
103
+ * @param {Cluster} c The cluster that the mouse moved over.
104
+ * @event
105
+ */
106
+ google.maps.event.trigger(_this.cluster.getClusterer(), 'mouseover', _this.cluster);
107
+ });
108
+ // eslint-disable-next-line @getify/proper-arrows/this, @getify/proper-arrows/name
109
+ google.maps.event.addDomListener(this.div, 'mouseout',
110
+ // eslint-disable-next-line @getify/proper-arrows/this, @getify/proper-arrows/name
111
+ function () {
112
+ /**
113
+ * This event is fired when the mouse moves out of a cluster marker.
114
+ * @name MarkerClusterer#mouseout
115
+ * @param {Cluster} c The cluster that the mouse moved out of.
116
+ * @event
117
+ */
118
+ google.maps.event.trigger(_this.cluster.getClusterer(), 'mouseout', _this.cluster);
119
+ });
120
+ };
121
+ ClusterIcon.prototype.onRemove = function () {
122
+ if (this.div && this.div.parentNode) {
123
+ this.hide();
124
+ if (this.boundsChangedListener !== null) {
125
+ google.maps.event.removeListener(this.boundsChangedListener);
126
+ }
127
+ google.maps.event.clearInstanceListeners(this.div);
128
+ this.div.parentNode.removeChild(this.div);
129
+ this.div = null;
130
+ }
131
+ };
132
+ ClusterIcon.prototype.draw = function () {
133
+ if (this.visible && this.div !== null && this.center) {
134
+ var _a = this.getPosFromLatLng(this.center), x = _a.x, y = _a.y;
135
+ this.div.style.top = y + 'px';
136
+ this.div.style.left = x + 'px';
137
+ }
138
+ };
139
+ ClusterIcon.prototype.hide = function () {
140
+ if (this.div) {
141
+ this.div.style.display = 'none';
142
+ }
143
+ this.visible = false;
144
+ };
145
+ ClusterIcon.prototype.show = function () {
146
+ if (this.div && this.center) {
147
+ var img = '', divTitle = '';
148
+ // NOTE: values must be specified in px units
149
+ var bp = this.backgroundPosition.split(' ');
150
+ var spriteH = parseInt(bp[0].replace(/^\s+|\s+$/g, ''), 10);
151
+ var spriteV = parseInt(bp[1].replace(/^\s+|\s+$/g, ''), 10);
152
+ var pos = this.getPosFromLatLng(this.center);
153
+ if (this.sums === null || typeof this.sums.title === 'undefined' || this.sums.title === '') {
154
+ divTitle = this.cluster.getClusterer().getTitle();
155
+ }
156
+ else {
157
+ divTitle = this.sums.title;
158
+ }
159
+ this.div.style.cssText = this.createCss(pos);
160
+ img =
161
+ "<img alt='" +
162
+ divTitle +
163
+ "' src='" +
164
+ this.url +
165
+ "' style='position: absolute; top: " +
166
+ spriteV +
167
+ 'px; left: ' +
168
+ spriteH +
169
+ 'px; ';
170
+ // eslint-disable-next-line @typescript-eslint/ban-ts-ignore
171
+ //@ts-ignore
172
+ if (!this.cluster.getClusterer().enableRetinaIcons) {
173
+ img +=
174
+ 'clip: rect(' +
175
+ -1 * spriteV +
176
+ 'px, ' +
177
+ (-1 * spriteH + this.width) +
178
+ 'px, ' +
179
+ (-1 * spriteV + this.height) +
180
+ 'px, ' +
181
+ -1 * spriteH +
182
+ 'px);';
183
+ }
184
+ img += "'>";
185
+ this.div.innerHTML =
186
+ img +
187
+ "<div style='" +
188
+ 'position: absolute;' +
189
+ 'top: ' +
190
+ this.anchorText[0] +
191
+ 'px;' +
192
+ 'left: ' +
193
+ this.anchorText[1] +
194
+ 'px;' +
195
+ 'color: ' +
196
+ this.textColor +
197
+ ';' +
198
+ 'font-size: ' +
199
+ this.textSize +
200
+ 'px;' +
201
+ 'font-family: ' +
202
+ this.fontFamily +
203
+ ';' +
204
+ 'font-weight: ' +
205
+ this.fontWeight +
206
+ ';' +
207
+ 'font-style: ' +
208
+ this.fontStyle +
209
+ ';' +
210
+ 'text-decoration: ' +
211
+ this.textDecoration +
212
+ ';' +
213
+ 'text-align: center;' +
214
+ 'width: ' +
215
+ this.width +
216
+ 'px;' +
217
+ 'line-height:' +
218
+ this.height +
219
+ 'px;' +
220
+ "'>" +
221
+ // eslint-disable-next-line @typescript-eslint/ban-ts-ignore
222
+ // @ts-ignore
223
+ this.sums.text +
224
+ '</div>';
225
+ this.div.title = divTitle;
226
+ this.div.style.display = '';
227
+ }
228
+ this.visible = true;
229
+ };
230
+ ClusterIcon.prototype.useStyle = function (sums) {
231
+ this.sums = sums;
232
+ var style = this.styles[Math.min(this.styles.length - 1, Math.max(0, sums.index - 1))];
233
+ this.url = style.url;
234
+ this.height = style.height;
235
+ this.width = style.width;
236
+ if (style.className)
237
+ this.className = this.className + " " + style.className;
238
+ this.anchorText = style.anchorText || [0, 0];
239
+ this.anchorIcon = style.anchorIcon || [this.height / 2, this.width / 2];
240
+ this.textColor = style.textColor || 'black';
241
+ this.textSize = style.textSize || 11;
242
+ this.textDecoration = style.textDecoration || 'none';
243
+ this.fontWeight = style.fontWeight || 'bold';
244
+ this.fontStyle = style.fontStyle || 'normal';
245
+ this.fontFamily = style.fontFamily || 'Arial,sans-serif';
246
+ this.backgroundPosition = style.backgroundPosition || '0 0';
247
+ };
248
+ ClusterIcon.prototype.setCenter = function (center) {
249
+ this.center = center;
250
+ };
251
+ ClusterIcon.prototype.createCss = function (pos) {
252
+ var style = [];
253
+ style.push('cursor: pointer;');
254
+ style.push('position: absolute; top: ' + pos.y + 'px; left: ' + pos.x + 'px;');
255
+ style.push('width: ' + this.width + 'px; height: ' + this.height + 'px;');
256
+ return style.join('');
257
+ };
258
+ ClusterIcon.prototype.getPosFromLatLng = function (latlng) {
259
+ // eslint-disable-next-line @typescript-eslint/ban-ts-ignore
260
+ // @ts-ignore
261
+ var pos = this.getProjection().fromLatLngToDivPixel(latlng);
262
+ pos.x -= this.anchorIcon[1];
263
+ pos.y -= this.anchorIcon[0];
264
+ // pos.x = pos.x
265
+ // pos.y = pos.y
266
+ return pos;
267
+ };
268
+ return ClusterIcon;
269
+ }());
270
+
271
+ var Cluster = /** @class */ (function () {
272
+ function Cluster(markerClusterer) {
273
+ this.markerClusterer = markerClusterer;
274
+ // eslint-disable-next-line @typescript-eslint/ban-ts-ignore
275
+ // @ts-ignore
276
+ this.map = this.markerClusterer.getMap();
277
+ this.gridSize = this.markerClusterer.getGridSize();
278
+ this.minClusterSize = this.markerClusterer.getMinimumClusterSize();
279
+ this.averageCenter = this.markerClusterer.getAverageCenter();
280
+ this.markers = [];
281
+ this.center = undefined;
282
+ this.bounds = null;
283
+ this.clusterIcon = new ClusterIcon(this, this.markerClusterer.getStyles());
284
+ }
285
+ Cluster.prototype.getSize = function () {
286
+ return this.markers.length;
287
+ };
288
+ Cluster.prototype.getMarkers = function () {
289
+ return this.markers;
290
+ };
291
+ Cluster.prototype.getCenter = function () {
292
+ return this.center;
293
+ };
294
+ Cluster.prototype.getMap = function () {
295
+ return this.map;
296
+ };
297
+ Cluster.prototype.getClusterer = function () {
298
+ return this.markerClusterer;
299
+ };
300
+ Cluster.prototype.getBounds = function () {
301
+ var bounds = new google.maps.LatLngBounds(this.center, this.center);
302
+ var markers = this.getMarkers();
303
+ for (var i = 0; i < markers.length; i++) {
304
+ var position = markers[i].getPosition();
305
+ if (position) {
306
+ bounds.extend(position);
307
+ }
308
+ }
309
+ return bounds;
310
+ };
311
+ Cluster.prototype.remove = function () {
312
+ // eslint-disable-next-line @typescript-eslint/ban-ts-ignore
313
+ // @ts-ignore
314
+ this.clusterIcon.setMap(null);
315
+ this.markers = [];
316
+ // @ts-ignore
317
+ delete this.markers;
318
+ };
319
+ Cluster.prototype.addMarker = function (marker) {
320
+ if (this.isMarkerAlreadyAdded(marker)) {
321
+ return false;
322
+ }
323
+ if (!this.center) {
324
+ var position = marker.getPosition();
325
+ if (position) {
326
+ this.center = position;
327
+ this.calculateBounds();
328
+ }
329
+ }
330
+ else {
331
+ if (this.averageCenter) {
332
+ var position = marker.getPosition();
333
+ if (position) {
334
+ var length_1 = this.markers.length + 1;
335
+ this.center = new google.maps.LatLng((this.center.lat() * (length_1 - 1) + position.lat()) / length_1, (this.center.lng() * (length_1 - 1) + position.lng()) / length_1);
336
+ this.calculateBounds();
337
+ }
338
+ }
339
+ }
340
+ marker.isAdded = true;
341
+ this.markers.push(marker);
342
+ var mCount = this.markers.length;
343
+ var maxZoom = this.markerClusterer.getMaxZoom();
344
+ if (maxZoom !== null && this.map.getZoom() > maxZoom) {
345
+ // Zoomed in past max zoom, so show the marker.
346
+ if (marker.getMap() !== this.map) {
347
+ marker.setMap(this.map);
348
+ }
349
+ }
350
+ else if (mCount < this.minClusterSize) {
351
+ // Min cluster size not reached so show the marker.
352
+ if (marker.getMap() !== this.map) {
353
+ marker.setMap(this.map);
354
+ }
355
+ }
356
+ else if (mCount === this.minClusterSize) {
357
+ // Hide the markers that were showing.
358
+ for (var i = 0; i < mCount; i++) {
359
+ this.markers[i].setMap(null);
360
+ }
361
+ }
362
+ else {
363
+ marker.setMap(null);
364
+ }
365
+ return true;
366
+ };
367
+ Cluster.prototype.isMarkerInClusterBounds = function (marker) {
368
+ if (this.bounds !== null) {
369
+ var position = marker.getPosition();
370
+ if (position) {
371
+ return this.bounds.contains(position);
372
+ }
373
+ }
374
+ return false;
375
+ };
376
+ Cluster.prototype.calculateBounds = function () {
377
+ this.bounds = this.markerClusterer.getExtendedBounds(new google.maps.LatLngBounds(this.center, this.center));
378
+ };
379
+ Cluster.prototype.updateIcon = function () {
380
+ var mCount = this.markers.length;
381
+ var maxZoom = this.markerClusterer.getMaxZoom();
382
+ if (maxZoom !== null && this.map.getZoom() > maxZoom) {
383
+ this.clusterIcon.hide();
384
+ return;
385
+ }
386
+ if (mCount < this.minClusterSize) {
387
+ // Min cluster size not yet reached.
388
+ this.clusterIcon.hide();
389
+ return;
390
+ }
391
+ if (this.center) {
392
+ this.clusterIcon.setCenter(this.center);
393
+ }
394
+ this.clusterIcon.useStyle(this.markerClusterer.getCalculator()(this.markers, this.markerClusterer.getStyles().length));
395
+ this.clusterIcon.show();
396
+ };
397
+ Cluster.prototype.isMarkerAlreadyAdded = function (marker) {
398
+ if (this.markers.includes) {
399
+ return this.markers.includes(marker);
400
+ }
401
+ else {
402
+ for (var i = 0; i < this.markers.length; i++) {
403
+ if (marker === this.markers[i]) {
404
+ return true;
405
+ }
406
+ }
407
+ }
408
+ return false;
409
+ };
410
+ return Cluster;
411
+ }());
412
+
413
+ /* global google */
414
+ /**
415
+ * Supports up to 9007199254740991 (Number.MAX_SAFE_INTEGER) markers
416
+ * which is not a problem as max array length is 4294967296 (2**32)
417
+ */
418
+ var CALCULATOR = function CALCULATOR(markers, numStyles) {
419
+ var count = markers.length;
420
+ var numberOfDigits = count.toString().length;
421
+ var index = Math.min(numberOfDigits, numStyles);
422
+ return {
423
+ text: count.toString(),
424
+ index: index,
425
+ title: '',
426
+ };
427
+ };
428
+ var BATCH_SIZE = 2000;
429
+ var BATCH_SIZE_IE = 500;
430
+ var IMAGE_PATH = 'https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/m';
431
+ var IMAGE_EXTENSION = 'png';
432
+ var IMAGE_SIZES = [53, 56, 66, 78, 90];
433
+ var CLUSTERER_CLASS = 'cluster';
434
+ var Clusterer = /** @class */ (function () {
435
+ function Clusterer(map, optMarkers, optOptions) {
436
+ if (optMarkers === void 0) { optMarkers = []; }
437
+ if (optOptions === void 0) { optOptions = {}; }
438
+ this.extend(Clusterer, google.maps.OverlayView);
439
+ this.markers = [];
440
+ this.clusters = [];
441
+ this.listeners = [];
442
+ this.activeMap = null;
443
+ this.ready = false;
444
+ this.gridSize = optOptions.gridSize || 60;
445
+ this.minClusterSize = optOptions.minimumClusterSize || 2;
446
+ this.maxZoom = optOptions.maxZoom || null;
447
+ this.styles = optOptions.styles || [];
448
+ this.title = optOptions.title || '';
449
+ this.zoomOnClick = true;
450
+ if (optOptions.zoomOnClick !== undefined) {
451
+ this.zoomOnClick = optOptions.zoomOnClick;
452
+ }
453
+ this.averageCenter = false;
454
+ if (optOptions.averageCenter !== undefined) {
455
+ this.averageCenter = optOptions.averageCenter;
456
+ }
457
+ this.ignoreHidden = false;
458
+ if (optOptions.ignoreHidden !== undefined) {
459
+ this.ignoreHidden = optOptions.ignoreHidden;
460
+ }
461
+ this.enableRetinaIcons = false;
462
+ if (optOptions.enableRetinaIcons !== undefined) {
463
+ this.enableRetinaIcons = optOptions.enableRetinaIcons;
464
+ }
465
+ this.imagePath = optOptions.imagePath || IMAGE_PATH;
466
+ this.imageExtension = optOptions.imageExtension || IMAGE_EXTENSION;
467
+ this.imageSizes = optOptions.imageSizes || IMAGE_SIZES;
468
+ this.calculator = optOptions.calculator || CALCULATOR;
469
+ this.batchSize = optOptions.batchSize || BATCH_SIZE;
470
+ this.batchSizeIE = optOptions.batchSizeIE || BATCH_SIZE_IE;
471
+ this.clusterClass = optOptions.clusterClass || CLUSTERER_CLASS;
472
+ if (navigator.userAgent.toLowerCase().indexOf('msie') !== -1) {
473
+ // Try to avoid IE timeout when processing a huge number of markers:
474
+ this.batchSize = this.batchSizeIE;
475
+ }
476
+ this.timerRefStatic = null;
477
+ this.setupStyles();
478
+ this.addMarkers(optMarkers, true);
479
+ // eslint-disable-next-line @typescript-eslint/ban-ts-ignore
480
+ // @ts-ignore
481
+ this.setMap(map); // Note: this causes onAdd to be called
482
+ }
483
+ Clusterer.prototype.onAdd = function () {
484
+ var _this = this;
485
+ // eslint-disable-next-line @typescript-eslint/ban-ts-ignore
486
+ // @ts-ignore
487
+ this.activeMap = this.getMap();
488
+ this.ready = true;
489
+ this.repaint();
490
+ // Add the map event listeners
491
+ this.listeners = [
492
+ google.maps.event.addListener(
493
+ // eslint-disable-next-line @typescript-eslint/ban-ts-ignore
494
+ // @ts-ignore
495
+ this.getMap(), 'zoom_changed',
496
+ // eslint-disable-next-line @getify/proper-arrows/this, @getify/proper-arrows/name
497
+ function () {
498
+ _this.resetViewport(false);
499
+ // Workaround for this Google bug: when map is at level 0 and "-" of
500
+ // zoom slider is clicked, a "zoom_changed" event is fired even though
501
+ // the map doesn't zoom out any further. In this situation, no "idle"
502
+ // event is triggered so the cluster markers that have been removed
503
+ // do not get redrawn. Same goes for a zoom in at maxZoom.
504
+ if (
505
+ // eslint-disable-next-line @typescript-eslint/ban-ts-ignore
506
+ // @ts-ignore
507
+ _this.getMap().getZoom() === (_this.get('minZoom') || 0) ||
508
+ // eslint-disable-next-line @typescript-eslint/ban-ts-ignore
509
+ // @ts-ignore
510
+ _this.getMap().getZoom() === _this.get('maxZoom')) {
511
+ google.maps.event.trigger(_this, 'idle');
512
+ }
513
+ }),
514
+ google.maps.event.addListener(
515
+ // eslint-disable-next-line @typescript-eslint/ban-ts-ignore
516
+ // @ts-ignore
517
+ this.getMap(), 'idle',
518
+ // eslint-disable-next-line @getify/proper-arrows/this, @getify/proper-arrows/name
519
+ function () {
520
+ _this.redraw();
521
+ }),
522
+ ];
523
+ };
524
+ // eslint-disable-next-line @getify/proper-arrows/this
525
+ Clusterer.prototype.onRemove = function () {
526
+ // Put all the managed markers back on the map:
527
+ for (var i = 0; i < this.markers.length; i++) {
528
+ if (this.markers[i].getMap() !== this.activeMap) {
529
+ this.markers[i].setMap(this.activeMap);
530
+ }
531
+ }
532
+ // Remove all clusters:
533
+ for (var i = 0; i < this.clusters.length; i++) {
534
+ this.clusters[i].remove();
535
+ }
536
+ this.clusters = [];
537
+ // Remove map event listeners:
538
+ for (var i = 0; i < this.listeners.length; i++) {
539
+ google.maps.event.removeListener(this.listeners[i]);
540
+ }
541
+ this.listeners = [];
542
+ this.activeMap = null;
543
+ this.ready = false;
544
+ };
545
+ // eslint-disable-next-line @typescript-eslint/no-empty-function
546
+ Clusterer.prototype.draw = function () { };
547
+ Clusterer.prototype.setupStyles = function () {
548
+ if (this.styles.length > 0) {
549
+ return;
550
+ }
551
+ for (var i = 0; i < this.imageSizes.length; i++) {
552
+ this.styles.push({
553
+ url: this.imagePath + (i + 1) + '.' + this.imageExtension,
554
+ height: this.imageSizes[i],
555
+ width: this.imageSizes[i],
556
+ });
557
+ }
558
+ };
559
+ Clusterer.prototype.fitMapToMarkers = function () {
560
+ var markers = this.getMarkers();
561
+ var bounds = new google.maps.LatLngBounds();
562
+ for (var i = 0; i < markers.length; i++) {
563
+ var position = markers[i].getPosition();
564
+ if (position) {
565
+ bounds.extend(position);
566
+ }
567
+ }
568
+ // eslint-disable-next-line @typescript-eslint/ban-ts-ignore
569
+ // @ts-ignore
570
+ this.getMap().fitBounds(bounds);
571
+ };
572
+ Clusterer.prototype.getGridSize = function () {
573
+ return this.gridSize;
574
+ };
575
+ Clusterer.prototype.setGridSize = function (gridSize) {
576
+ this.gridSize = gridSize;
577
+ };
578
+ Clusterer.prototype.getMinimumClusterSize = function () {
579
+ return this.minClusterSize;
580
+ };
581
+ Clusterer.prototype.setMinimumClusterSize = function (minimumClusterSize) {
582
+ this.minClusterSize = minimumClusterSize;
583
+ };
584
+ Clusterer.prototype.getMaxZoom = function () {
585
+ return this.maxZoom;
586
+ };
587
+ Clusterer.prototype.setMaxZoom = function (maxZoom) {
588
+ this.maxZoom = maxZoom;
589
+ };
590
+ Clusterer.prototype.getStyles = function () {
591
+ return this.styles;
592
+ };
593
+ Clusterer.prototype.setStyles = function (styles) {
594
+ this.styles = styles;
595
+ };
596
+ Clusterer.prototype.getTitle = function () {
597
+ return this.title;
598
+ };
599
+ Clusterer.prototype.setTitle = function (title) {
600
+ this.title = title;
601
+ };
602
+ Clusterer.prototype.getZoomOnClick = function () {
603
+ return this.zoomOnClick;
604
+ };
605
+ Clusterer.prototype.setZoomOnClick = function (zoomOnClick) {
606
+ this.zoomOnClick = zoomOnClick;
607
+ };
608
+ Clusterer.prototype.getAverageCenter = function () {
609
+ return this.averageCenter;
610
+ };
611
+ Clusterer.prototype.setAverageCenter = function (averageCenter) {
612
+ this.averageCenter = averageCenter;
613
+ };
614
+ Clusterer.prototype.getIgnoreHidden = function () {
615
+ return this.ignoreHidden;
616
+ };
617
+ Clusterer.prototype.setIgnoreHidden = function (ignoreHidden) {
618
+ this.ignoreHidden = ignoreHidden;
619
+ };
620
+ Clusterer.prototype.getEnableRetinaIcons = function () {
621
+ return this.enableRetinaIcons;
622
+ };
623
+ Clusterer.prototype.setEnableRetinaIcons = function (enableRetinaIcons) {
624
+ this.enableRetinaIcons = enableRetinaIcons;
625
+ };
626
+ Clusterer.prototype.getImageExtension = function () {
627
+ return this.imageExtension;
628
+ };
629
+ Clusterer.prototype.setImageExtension = function (imageExtension) {
630
+ this.imageExtension = imageExtension;
631
+ };
632
+ Clusterer.prototype.getImagePath = function () {
633
+ return this.imagePath;
634
+ };
635
+ Clusterer.prototype.setImagePath = function (imagePath) {
636
+ this.imagePath = imagePath;
637
+ };
638
+ Clusterer.prototype.getImageSizes = function () {
639
+ return this.imageSizes;
640
+ };
641
+ Clusterer.prototype.setImageSizes = function (imageSizes) {
642
+ this.imageSizes = imageSizes;
643
+ };
644
+ Clusterer.prototype.getCalculator = function () {
645
+ return this.calculator;
646
+ };
647
+ Clusterer.prototype.setCalculator = function (calculator) {
648
+ this.calculator = calculator;
649
+ };
650
+ Clusterer.prototype.getBatchSizeIE = function () {
651
+ return this.batchSizeIE;
652
+ };
653
+ Clusterer.prototype.setBatchSizeIE = function (batchSizeIE) {
654
+ this.batchSizeIE = batchSizeIE;
655
+ };
656
+ Clusterer.prototype.getClusterClass = function () {
657
+ return this.clusterClass;
658
+ };
659
+ Clusterer.prototype.setClusterClass = function (clusterClass) {
660
+ this.clusterClass = clusterClass;
661
+ };
662
+ Clusterer.prototype.getMarkers = function () {
663
+ return this.markers;
664
+ };
665
+ Clusterer.prototype.getTotalMarkers = function () {
666
+ return this.markers.length;
667
+ };
668
+ Clusterer.prototype.getClusters = function () {
669
+ return this.clusters;
670
+ };
671
+ Clusterer.prototype.getTotalClusters = function () {
672
+ return this.clusters.length;
673
+ };
674
+ Clusterer.prototype.addMarker = function (marker, optNoDraw) {
675
+ this.pushMarkerTo(marker);
676
+ if (!optNoDraw) {
677
+ this.redraw();
678
+ }
679
+ };
680
+ Clusterer.prototype.addMarkers = function (markers, optNoDraw) {
681
+ for (var key in markers) {
682
+ if (markers.hasOwnProperty(key)) {
683
+ this.pushMarkerTo(markers[key]);
684
+ }
685
+ }
686
+ if (!optNoDraw) {
687
+ this.redraw();
688
+ }
689
+ };
690
+ Clusterer.prototype.pushMarkerTo = function (marker) {
691
+ var _this = this;
692
+ // If the marker is draggable add a listener so we can update the clusters on the dragend:
693
+ if (marker.getDraggable()) {
694
+ // eslint-disable-next-line @getify/proper-arrows/name, @getify/proper-arrows/this
695
+ google.maps.event.addListener(marker, 'dragend', function () {
696
+ if (_this.ready) {
697
+ marker.isAdded = false;
698
+ _this.repaint();
699
+ }
700
+ });
701
+ }
702
+ marker.isAdded = false;
703
+ this.markers.push(marker);
704
+ };
705
+ Clusterer.prototype.removeMarker_ = function (marker) {
706
+ var index = -1;
707
+ if (this.markers.indexOf) {
708
+ index = this.markers.indexOf(marker);
709
+ }
710
+ else {
711
+ for (var i = 0; i < this.markers.length; i++) {
712
+ if (marker === this.markers[i]) {
713
+ index = i;
714
+ break;
715
+ }
716
+ }
717
+ }
718
+ if (index === -1) {
719
+ // Marker is not in our list of markers, so do nothing:
720
+ return false;
721
+ }
722
+ marker.setMap(null);
723
+ this.markers.splice(index, 1); // Remove the marker from the list of managed markers
724
+ return true;
725
+ };
726
+ Clusterer.prototype.removeMarker = function (marker, optNoDraw) {
727
+ var removed = this.removeMarker_(marker);
728
+ if (!optNoDraw && removed) {
729
+ this.repaint();
730
+ }
731
+ return removed;
732
+ };
733
+ Clusterer.prototype.removeMarkers = function (markers, optNoDraw) {
734
+ var removed = false;
735
+ for (var i = 0; i < markers.length; i++) {
736
+ removed = removed || this.removeMarker_(markers[i]);
737
+ }
738
+ if (!optNoDraw && removed) {
739
+ this.repaint();
740
+ }
741
+ return removed;
742
+ };
743
+ Clusterer.prototype.clearMarkers = function () {
744
+ this.resetViewport(true);
745
+ this.markers = [];
746
+ };
747
+ Clusterer.prototype.repaint = function () {
748
+ var oldClusters = this.clusters.slice();
749
+ this.clusters = [];
750
+ this.resetViewport(false);
751
+ this.redraw();
752
+ // Remove the old clusters.
753
+ // Do it in a timeout to prevent blinking effect.
754
+ setTimeout(function timeout() {
755
+ for (var i = 0; i < oldClusters.length; i++) {
756
+ oldClusters[i].remove();
757
+ }
758
+ }, 0);
759
+ };
760
+ Clusterer.prototype.getExtendedBounds = function (bounds) {
761
+ // eslint-disable-next-line @typescript-eslint/ban-ts-ignore
762
+ // @ts-ignore
763
+ var projection = this.getProjection();
764
+ // Convert the points to pixels and the extend out by the grid size.
765
+ var trPix = projection.fromLatLngToDivPixel(
766
+ // Turn the bounds into latlng.
767
+ new google.maps.LatLng(bounds.getNorthEast().lat(), bounds.getNorthEast().lng()));
768
+ trPix.x += this.gridSize;
769
+ trPix.y -= this.gridSize;
770
+ var blPix = projection.fromLatLngToDivPixel(
771
+ // Turn the bounds into latlng.
772
+ new google.maps.LatLng(bounds.getSouthWest().lat(), bounds.getSouthWest().lng()));
773
+ blPix.x -= this.gridSize;
774
+ blPix.y += this.gridSize;
775
+ // Extend the bounds to contain the new bounds.
776
+ bounds.extend(
777
+ // Convert the pixel points back to LatLng nw
778
+ projection.fromDivPixelToLatLng(trPix));
779
+ bounds.extend(
780
+ // Convert the pixel points back to LatLng sw
781
+ projection.fromDivPixelToLatLng(blPix));
782
+ return bounds;
783
+ };
784
+ Clusterer.prototype.redraw = function () {
785
+ // Redraws all the clusters.
786
+ this.createClusters(0);
787
+ };
788
+ Clusterer.prototype.resetViewport = function (optHide) {
789
+ // Remove all the clusters
790
+ for (var i = 0; i < this.clusters.length; i++) {
791
+ this.clusters[i].remove();
792
+ }
793
+ this.clusters = [];
794
+ // Reset the markers to not be added and to be removed from the map.
795
+ for (var i = 0; i < this.markers.length; i++) {
796
+ var marker = this.markers[i];
797
+ marker.isAdded = false;
798
+ if (optHide) {
799
+ marker.setMap(null);
800
+ }
801
+ }
802
+ };
803
+ Clusterer.prototype.distanceBetweenPoints = function (p1, p2) {
804
+ var R = 6371; // Radius of the Earth in km
805
+ var dLat = ((p2.lat() - p1.lat()) * Math.PI) / 180;
806
+ var dLon = ((p2.lng() - p1.lng()) * Math.PI) / 180;
807
+ var a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
808
+ Math.cos((p1.lat() * Math.PI) / 180) *
809
+ Math.cos((p2.lat() * Math.PI) / 180) *
810
+ Math.sin(dLon / 2) *
811
+ Math.sin(dLon / 2);
812
+ return R * (2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)));
813
+ };
814
+ Clusterer.prototype.isMarkerInBounds = function (marker, bounds) {
815
+ var position = marker.getPosition();
816
+ if (position) {
817
+ return bounds.contains(position);
818
+ }
819
+ return false;
820
+ };
821
+ Clusterer.prototype.addToClosestCluster = function (marker) {
822
+ var cluster;
823
+ var distance = 40000; // Some large number
824
+ var clusterToAddTo = null;
825
+ for (var i = 0; i < this.clusters.length; i++) {
826
+ cluster = this.clusters[i];
827
+ var center = cluster.getCenter();
828
+ var position = marker.getPosition();
829
+ if (center && position) {
830
+ var d = this.distanceBetweenPoints(center, position);
831
+ if (d < distance) {
832
+ distance = d;
833
+ clusterToAddTo = cluster;
834
+ }
835
+ }
836
+ }
837
+ if (clusterToAddTo && clusterToAddTo.isMarkerInClusterBounds(marker)) {
838
+ clusterToAddTo.addMarker(marker);
839
+ }
840
+ else {
841
+ cluster = new Cluster(this);
842
+ cluster.addMarker(marker);
843
+ this.clusters.push(cluster);
844
+ }
845
+ };
846
+ Clusterer.prototype.createClusters = function (iFirst) {
847
+ var _this = this;
848
+ if (!this.ready) {
849
+ return;
850
+ }
851
+ // Cancel previous batch processing if we're working on the first batch:
852
+ if (iFirst === 0) {
853
+ /**
854
+ * This event is fired when the <code>Clusterer</code> begins
855
+ * clustering markers.
856
+ * @name Clusterer#clusteringbegin
857
+ * @param {Clusterer} mc The Clusterer whose markers are being clustered.
858
+ * @event
859
+ */
860
+ google.maps.event.trigger(this, 'clusteringbegin', this);
861
+ if (this.timerRefStatic !== null) {
862
+ window.clearTimeout(this.timerRefStatic);
863
+ // @ts-ignore
864
+ delete this.timerRefStatic;
865
+ }
866
+ }
867
+ // Get our current map view bounds.
868
+ // Create a new bounds object so we don't affect the map.
869
+ //
870
+ // See Comments 9 & 11 on Issue 3651 relating to this workaround for a Google Maps bug:
871
+ var mapBounds =
872
+ // eslint-disable-next-line @typescript-eslint/ban-ts-ignore
873
+ // @ts-ignore
874
+ this.getMap().getZoom() > 3
875
+ ? new google.maps.LatLngBounds(
876
+ // eslint-disable-next-line @typescript-eslint/ban-ts-ignore
877
+ // @ts-ignore
878
+ this.getMap()
879
+ .getBounds()
880
+ .getSouthWest(),
881
+ // eslint-disable-next-line @typescript-eslint/ban-ts-ignore
882
+ // @ts-ignore
883
+ this.getMap()
884
+ .getBounds()
885
+ .getNorthEast())
886
+ : new google.maps.LatLngBounds(new google.maps.LatLng(85.02070771743472, -178.48388434375), new google.maps.LatLng(-85.08136444384544, 178.00048865625));
887
+ var bounds = this.getExtendedBounds(mapBounds);
888
+ var iLast = Math.min(iFirst + this.batchSize, this.markers.length);
889
+ for (var i = iFirst; i < iLast; i++) {
890
+ var marker = this.markers[i];
891
+ if (!marker.isAdded && this.isMarkerInBounds(marker, bounds)) {
892
+ if (!this.ignoreHidden || (this.ignoreHidden && marker.getVisible())) {
893
+ this.addToClosestCluster(marker);
894
+ }
895
+ }
896
+ }
897
+ if (iLast < this.markers.length) {
898
+ this.timerRefStatic = window.setTimeout(
899
+ // eslint-disable-next-line @getify/proper-arrows/this, @getify/proper-arrows/name
900
+ function () {
901
+ _this.createClusters(iLast);
902
+ }, 0);
903
+ }
904
+ else {
905
+ this.timerRefStatic = null;
906
+ /**
907
+ * This event is fired when the <code>Clusterer</code> stops
908
+ * clustering markers.
909
+ * @name Clusterer#clusteringend
910
+ * @param {Clusterer} mc The Clusterer whose markers are being clustered.
911
+ * @event
912
+ */
913
+ google.maps.event.trigger(this, 'clusteringend', this);
914
+ for (var i = 0; i < this.clusters.length; i++) {
915
+ this.clusters[i].updateIcon();
916
+ }
917
+ }
918
+ };
919
+ Clusterer.prototype.extend = function (obj1, obj2) {
920
+ return function applyExtend(object) {
921
+ // eslint-disable-next-line guard-for-in
922
+ for (var property in object.prototype) {
923
+ // eslint-disable-next-line @typescript-eslint/ban-ts-ignore
924
+ // @ts-ignore
925
+ this.prototype[property] = object.prototype[property];
926
+ }
927
+ // eslint-disable-next-line @typescript-eslint/ban-ts-ignore
928
+ // @ts-ignore
929
+ return this;
930
+ }.apply(obj1, [obj2]);
931
+ };
932
+ return Clusterer;
933
+ }());
934
+
935
+ export { Cluster, ClusterIcon, Clusterer };
936
+ //# sourceMappingURL=esm.js.map