@react-google-maps/marker-clusterer 2.16.1 → 2.19.1
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/Cluster.d.ts +28 -0
- package/dist/ClusterIcon.d.ts +43 -0
- package/dist/Clusterer.d.ts +107 -0
- package/dist/__tests__/clusterer.test.d.ts +4 -0
- package/dist/cjs.js +98 -52
- package/dist/cjs.js.map +1 -1
- package/dist/cjs.min.js +1 -1
- package/dist/cjs.min.js.map +1 -1
- package/dist/esm.js +98 -52
- package/dist/esm.js.map +1 -1
- package/dist/esm.min.js +1 -1
- package/dist/esm.min.js.map +1 -1
- package/dist/index.d.ts +54 -33
- package/dist/types.d.ts +44 -0
- package/dist/umd.js +98 -52
- package/dist/umd.js.map +1 -1
- package/dist/umd.min.js +1 -1
- package/dist/umd.min.js.map +1 -1
- package/package.json +9 -8
- package/src/Cluster.tsx +7 -7
- package/src/ClusterIcon.tsx +23 -20
- package/src/Clusterer.tsx +81 -40
- package/src/types.tsx +29 -29
package/src/Clusterer.tsx
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
/* global google */
|
|
2
2
|
/* eslint-disable filenames/match-regex */
|
|
3
3
|
import { Cluster } from './Cluster'
|
|
4
|
-
import { ClusterIcon } from './ClusterIcon'
|
|
4
|
+
import type { ClusterIcon } from './ClusterIcon'
|
|
5
5
|
|
|
6
|
-
import {
|
|
6
|
+
import type {
|
|
7
7
|
MarkerExtended,
|
|
8
8
|
ClustererOptions,
|
|
9
9
|
ClusterIconStyle,
|
|
@@ -45,7 +45,7 @@ const IMAGE_SIZES = [53, 56, 66, 78, 90]
|
|
|
45
45
|
|
|
46
46
|
const CLUSTERER_CLASS = 'cluster'
|
|
47
47
|
|
|
48
|
-
export class Clusterer {
|
|
48
|
+
export class Clusterer implements google.maps.OverlayView {
|
|
49
49
|
markers: MarkerExtended[]
|
|
50
50
|
clusters: Cluster[]
|
|
51
51
|
listeners: google.maps.MapsEventListener[]
|
|
@@ -125,10 +125,10 @@ export class Clusterer {
|
|
|
125
125
|
this.repaint = this.repaint.bind(this)
|
|
126
126
|
this.onIdle = this.onIdle.bind(this)
|
|
127
127
|
this.redraw = this.redraw.bind(this)
|
|
128
|
-
this.extend = this.extend.bind(this)
|
|
129
128
|
this.onAdd = this.onAdd.bind(this)
|
|
130
129
|
this.draw = this.draw.bind(this)
|
|
131
130
|
|
|
131
|
+
this.extend = this.extend.bind(this)
|
|
132
132
|
this.extend(Clusterer, google.maps.OverlayView)
|
|
133
133
|
|
|
134
134
|
this.markers = []
|
|
@@ -194,7 +194,7 @@ export class Clusterer {
|
|
|
194
194
|
(this as unknown as google.maps.OverlayView).setMap(map) // Note: this causes onAdd to be called
|
|
195
195
|
}
|
|
196
196
|
|
|
197
|
-
onZoomChanged() {
|
|
197
|
+
onZoomChanged(): void {
|
|
198
198
|
this.resetViewport(false)
|
|
199
199
|
|
|
200
200
|
// Workaround for this Google bug: when map is at level 0 and "-" of
|
|
@@ -210,11 +210,11 @@ export class Clusterer {
|
|
|
210
210
|
}
|
|
211
211
|
}
|
|
212
212
|
|
|
213
|
-
onIdle() {
|
|
213
|
+
onIdle(): void {
|
|
214
214
|
this.redraw()
|
|
215
215
|
}
|
|
216
216
|
|
|
217
|
-
onAdd() {
|
|
217
|
+
onAdd(): void {
|
|
218
218
|
const map = (this as unknown as google.maps.OverlayView).getMap()
|
|
219
219
|
|
|
220
220
|
this.activeMap = map
|
|
@@ -240,24 +240,24 @@ export class Clusterer {
|
|
|
240
240
|
}
|
|
241
241
|
}
|
|
242
242
|
|
|
243
|
-
onRemove() {
|
|
243
|
+
onRemove(): void {
|
|
244
244
|
// Put all the managed markers back on the map:
|
|
245
|
-
for (
|
|
246
|
-
if (
|
|
247
|
-
|
|
245
|
+
for (const marker of this.markers) {
|
|
246
|
+
if (marker.getMap() !== this.activeMap) {
|
|
247
|
+
marker.setMap(this.activeMap)
|
|
248
248
|
}
|
|
249
249
|
}
|
|
250
250
|
|
|
251
251
|
// Remove all clusters:
|
|
252
|
-
for (
|
|
253
|
-
|
|
252
|
+
for (const cluster of this.clusters) {
|
|
253
|
+
cluster.remove()
|
|
254
254
|
}
|
|
255
255
|
|
|
256
256
|
this.clusters = []
|
|
257
257
|
|
|
258
258
|
// Remove map event listeners:
|
|
259
|
-
for (
|
|
260
|
-
google.maps.event.removeListener(
|
|
259
|
+
for (const listener of this.listeners) {
|
|
260
|
+
google.maps.event.removeListener(listener)
|
|
261
261
|
}
|
|
262
262
|
|
|
263
263
|
this.listeners = []
|
|
@@ -267,9 +267,43 @@ export class Clusterer {
|
|
|
267
267
|
this.ready = false
|
|
268
268
|
}
|
|
269
269
|
|
|
270
|
-
draw() { return }
|
|
270
|
+
draw(): void { return }
|
|
271
|
+
|
|
272
|
+
getMap(): null { return null }
|
|
273
|
+
|
|
274
|
+
getPanes(): null { return null }
|
|
275
|
+
|
|
276
|
+
getProjection() {
|
|
277
|
+
return {
|
|
278
|
+
fromContainerPixelToLatLng(): null { return null },
|
|
279
|
+
fromDivPixelToLatLng(): null { return null},
|
|
280
|
+
fromLatLngToContainerPixel(): null { return null},
|
|
281
|
+
fromLatLngToDivPixel(): null { return null},
|
|
282
|
+
getVisibleRegion(): null { return null },
|
|
283
|
+
getWorldWidth(): number { return 0 }
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
setMap(): void { return }
|
|
288
|
+
|
|
289
|
+
addListener() {
|
|
290
|
+
return {
|
|
291
|
+
remove() { return }
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
bindTo(): void { return }
|
|
271
296
|
|
|
272
|
-
|
|
297
|
+
get(): void { return }
|
|
298
|
+
|
|
299
|
+
notify(): void { return }
|
|
300
|
+
|
|
301
|
+
set(): void { return }
|
|
302
|
+
setValues(): void { return }
|
|
303
|
+
unbind(): void { return }
|
|
304
|
+
unbindAll(): void { return }
|
|
305
|
+
|
|
306
|
+
setupStyles(): void {
|
|
273
307
|
if (this.styles.length > 0) {
|
|
274
308
|
return
|
|
275
309
|
}
|
|
@@ -277,19 +311,19 @@ export class Clusterer {
|
|
|
277
311
|
for (let i = 0; i < this.imageSizes.length; i++) {
|
|
278
312
|
this.styles.push({
|
|
279
313
|
url: `${this.imagePath + (i + 1)}.${this.imageExtension}`,
|
|
280
|
-
height: this.imageSizes[i],
|
|
281
|
-
width: this.imageSizes[i],
|
|
314
|
+
height: this.imageSizes[i] || 0,
|
|
315
|
+
width: this.imageSizes[i] || 0,
|
|
282
316
|
})
|
|
283
317
|
}
|
|
284
318
|
}
|
|
285
319
|
|
|
286
|
-
fitMapToMarkers() {
|
|
320
|
+
fitMapToMarkers(): void {
|
|
287
321
|
const markers = this.getMarkers()
|
|
288
322
|
|
|
289
323
|
const bounds = new google.maps.LatLngBounds()
|
|
290
324
|
|
|
291
|
-
for (
|
|
292
|
-
const position =
|
|
325
|
+
for (const marker of markers) {
|
|
326
|
+
const position = marker.getPosition()
|
|
293
327
|
|
|
294
328
|
if (position) {
|
|
295
329
|
bounds.extend(position)
|
|
@@ -451,7 +485,11 @@ export class Clusterer {
|
|
|
451
485
|
addMarkers(markers: MarkerExtended[], optNoDraw: boolean) {
|
|
452
486
|
for (const key in markers) {
|
|
453
487
|
if (Object.prototype.hasOwnProperty.call(markers, key)) {
|
|
454
|
-
|
|
488
|
+
const marker = markers[key]
|
|
489
|
+
|
|
490
|
+
if (marker) {
|
|
491
|
+
this.pushMarkerTo(marker)
|
|
492
|
+
}
|
|
455
493
|
}
|
|
456
494
|
}
|
|
457
495
|
|
|
@@ -517,8 +555,8 @@ export class Clusterer {
|
|
|
517
555
|
removeMarkers(markers: MarkerExtended[], optNoDraw: boolean): boolean {
|
|
518
556
|
let removed = false
|
|
519
557
|
|
|
520
|
-
for (
|
|
521
|
-
removed = removed || this.removeMarker_(
|
|
558
|
+
for (const marker of markers) {
|
|
559
|
+
removed = removed || this.removeMarker_(marker)
|
|
522
560
|
}
|
|
523
561
|
|
|
524
562
|
if (!optNoDraw && removed) {
|
|
@@ -546,8 +584,8 @@ export class Clusterer {
|
|
|
546
584
|
// Remove the old clusters.
|
|
547
585
|
// Do it in a timeout to prevent blinking effect.
|
|
548
586
|
setTimeout(function timeout() {
|
|
549
|
-
for (
|
|
550
|
-
|
|
587
|
+
for (const oldCluster of oldClusters) {
|
|
588
|
+
oldCluster.remove()
|
|
551
589
|
}
|
|
552
590
|
}, 0)
|
|
553
591
|
}
|
|
@@ -609,16 +647,14 @@ export class Clusterer {
|
|
|
609
647
|
|
|
610
648
|
resetViewport(optHide: boolean) {
|
|
611
649
|
// Remove all the clusters
|
|
612
|
-
for (
|
|
613
|
-
|
|
650
|
+
for (const cluster of this.clusters) {
|
|
651
|
+
cluster.remove()
|
|
614
652
|
}
|
|
615
653
|
|
|
616
654
|
this.clusters = []
|
|
617
655
|
|
|
618
656
|
// Reset the markers to not be added and to be removed from the map.
|
|
619
|
-
for (
|
|
620
|
-
const marker = this.markers[i]
|
|
621
|
-
|
|
657
|
+
for (const marker of this.markers) {
|
|
622
658
|
marker.isAdded = false
|
|
623
659
|
|
|
624
660
|
if (optHide) {
|
|
@@ -660,8 +696,8 @@ export class Clusterer {
|
|
|
660
696
|
|
|
661
697
|
let clusterToAddTo = null
|
|
662
698
|
|
|
663
|
-
for (
|
|
664
|
-
cluster =
|
|
699
|
+
for (const clusterElement of this.clusters) {
|
|
700
|
+
cluster = clusterElement
|
|
665
701
|
|
|
666
702
|
const center = cluster.getCenter()
|
|
667
703
|
|
|
@@ -740,7 +776,7 @@ export class Clusterer {
|
|
|
740
776
|
for (let i = iFirst; i < iLast; i++) {
|
|
741
777
|
const marker = this.markers[i]
|
|
742
778
|
|
|
743
|
-
if (!marker.isAdded && this.isMarkerInBounds(marker, extendedMapBounds) && (!this.ignoreHidden || (this.ignoreHidden && marker.getVisible()))) {
|
|
779
|
+
if (marker && !marker.isAdded && this.isMarkerInBounds(marker, extendedMapBounds) && (!this.ignoreHidden || (this.ignoreHidden && marker.getVisible()))) {
|
|
744
780
|
this.addToClosestCluster(marker)
|
|
745
781
|
}
|
|
746
782
|
}
|
|
@@ -764,20 +800,25 @@ export class Clusterer {
|
|
|
764
800
|
*/
|
|
765
801
|
google.maps.event.trigger(this, 'clusteringend', this)
|
|
766
802
|
|
|
767
|
-
for (
|
|
768
|
-
|
|
803
|
+
for (const cluster of this.clusters) {
|
|
804
|
+
cluster.updateIcon()
|
|
769
805
|
}
|
|
770
806
|
}
|
|
771
807
|
}
|
|
772
808
|
|
|
773
|
-
extend<A extends typeof
|
|
809
|
+
extend<A extends typeof Clusterer | typeof ClusterIcon>(obj1: A, obj2: typeof google.maps.OverlayView): A {
|
|
774
810
|
return function applyExtend(this: A, object: typeof google.maps.OverlayView): A {
|
|
775
811
|
for (const property in object.prototype) {
|
|
812
|
+
|
|
813
|
+
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
814
|
+
const prop = property as keyof google.maps.OverlayView & (string & {})
|
|
815
|
+
|
|
816
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
776
817
|
// @ts-ignore
|
|
777
|
-
this.prototype[
|
|
818
|
+
this.prototype[prop] = object.prototype[prop]
|
|
778
819
|
}
|
|
779
820
|
|
|
780
821
|
return this
|
|
781
|
-
}.apply<A, [typeof google.maps.OverlayView],
|
|
822
|
+
}.apply<A, [typeof google.maps.OverlayView], A>(obj1, [obj2])
|
|
782
823
|
}
|
|
783
824
|
}
|
package/src/types.tsx
CHANGED
|
@@ -2,47 +2,47 @@
|
|
|
2
2
|
export interface ClusterIconInfo {
|
|
3
3
|
text: string
|
|
4
4
|
index: number
|
|
5
|
-
title?: string
|
|
6
|
-
html?: string
|
|
5
|
+
title?: string | undefined
|
|
6
|
+
html?: string | undefined
|
|
7
7
|
}
|
|
8
8
|
|
|
9
9
|
export type MarkerExtended = google.maps.Marker & {
|
|
10
|
-
isAdded?: boolean
|
|
10
|
+
isAdded?: boolean | undefined
|
|
11
11
|
}
|
|
12
12
|
|
|
13
13
|
export type TCalculator = (markers: MarkerExtended[], num: number) => ClusterIconInfo
|
|
14
14
|
|
|
15
15
|
export interface ClusterIconStyle {
|
|
16
16
|
url: string
|
|
17
|
-
className?: string
|
|
17
|
+
className?: string | undefined
|
|
18
18
|
height: number
|
|
19
19
|
width: number
|
|
20
|
-
anchorText?: number
|
|
21
|
-
anchorIcon?: number
|
|
22
|
-
textColor?: string
|
|
23
|
-
textSize?: number
|
|
24
|
-
textDecoration?: string
|
|
25
|
-
fontWeight?: string
|
|
26
|
-
fontStyle?: string
|
|
27
|
-
fontFamily?: string
|
|
28
|
-
backgroundPosition?: string
|
|
20
|
+
anchorText?: [number, number] | undefined
|
|
21
|
+
anchorIcon?: [number, number] | undefined
|
|
22
|
+
textColor?: string | undefined
|
|
23
|
+
textSize?: number | undefined
|
|
24
|
+
textDecoration?: string | undefined
|
|
25
|
+
fontWeight?: string | undefined
|
|
26
|
+
fontStyle?: string | undefined
|
|
27
|
+
fontFamily?: string | undefined
|
|
28
|
+
backgroundPosition?: string | undefined
|
|
29
29
|
}
|
|
30
30
|
|
|
31
31
|
export interface ClustererOptions {
|
|
32
|
-
gridSize?: number
|
|
33
|
-
maxZoom?: number
|
|
34
|
-
zoomOnClick?: boolean
|
|
35
|
-
averageCenter?: boolean
|
|
36
|
-
minimumClusterSize?: number
|
|
37
|
-
ignoreHidden?: boolean
|
|
38
|
-
title?: string
|
|
39
|
-
calculator?: TCalculator
|
|
40
|
-
clusterClass?: string
|
|
41
|
-
styles?: ClusterIconStyle[]
|
|
42
|
-
enableRetinaIcons?: boolean
|
|
43
|
-
batchSize?: number
|
|
44
|
-
batchSizeIE?: number
|
|
45
|
-
imagePath?: string
|
|
46
|
-
imageExtension?: string
|
|
47
|
-
imageSizes?: number[]
|
|
32
|
+
gridSize?: number | undefined
|
|
33
|
+
maxZoom?: number | undefined
|
|
34
|
+
zoomOnClick?: boolean | undefined
|
|
35
|
+
averageCenter?: boolean | undefined
|
|
36
|
+
minimumClusterSize?: number | undefined
|
|
37
|
+
ignoreHidden?: boolean | undefined
|
|
38
|
+
title?: string | undefined
|
|
39
|
+
calculator?: TCalculator | undefined
|
|
40
|
+
clusterClass?: string | undefined
|
|
41
|
+
styles?: ClusterIconStyle[] | undefined
|
|
42
|
+
enableRetinaIcons?: boolean | undefined
|
|
43
|
+
batchSize?: number | undefined
|
|
44
|
+
batchSizeIE?: number | undefined
|
|
45
|
+
imagePath?: string | undefined
|
|
46
|
+
imageExtension?: string | undefined
|
|
47
|
+
imageSizes?: number[] | undefined
|
|
48
48
|
}
|