@react-google-maps/marker-clusterer 2.8.0 → 2.10.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.
@@ -25,6 +25,9 @@ export class ClusterIcon {
25
25
  fontStyle: string
26
26
  fontFamily: string
27
27
  backgroundPosition: string
28
+ cMouseDownInCluster: boolean | null
29
+ cDraggingMapByCluster: boolean | null
30
+ timeOut: number | null
28
31
 
29
32
  boundsChangedListener: google.maps.MapsEventListener | null
30
33
 
@@ -51,118 +54,135 @@ export class ClusterIcon {
51
54
  this.fontStyle = 'normal'
52
55
  this.fontFamily = 'Arial,sans-serif'
53
56
  this.backgroundPosition = '0 0'
57
+
58
+ this.cMouseDownInCluster = null
59
+ this.cDraggingMapByCluster = null
60
+ this.timeOut = null
61
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
54
62
  // @ts-ignore
55
63
  this.setMap(cluster.getMap()) // Note: this causes onAdd to be called
56
64
  }
57
65
 
58
- onAdd() {
59
- let cMouseDownInCluster: boolean
60
- let cDraggingMapByCluster: boolean
66
+ onBoundsChanged() {
67
+ this.cDraggingMapByCluster = this.cMouseDownInCluster
68
+ }
69
+
70
+ onMouseDown() {
71
+ this.cMouseDownInCluster = true
72
+
73
+ this.cDraggingMapByCluster = false
74
+ }
75
+
76
+ onClick(event: Event) {
77
+ this.cMouseDownInCluster = false
78
+
79
+ if (!this.cDraggingMapByCluster) {
80
+ const markerClusterer = this.cluster.getClusterer()
81
+
82
+ /**
83
+ * This event is fired when a cluster marker is clicked.
84
+ * @name MarkerClusterer#click
85
+ * @param {Cluster} c The cluster that was clicked.
86
+ * @event
87
+ */
88
+ google.maps.event.trigger(markerClusterer, 'click', this.cluster)
89
+ google.maps.event.trigger(markerClusterer, 'clusterclick', this.cluster) // deprecated name
90
+
91
+ // The default click handler follows. Disable it by setting
92
+ // the zoomOnClick property to false.
93
+ if (markerClusterer.getZoomOnClick()) {
94
+ // Zoom into the cluster.
95
+ const maxZoom = markerClusterer.getMaxZoom()
96
+
97
+ const bounds = this.cluster.getBounds()
98
+
99
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
100
+ // @ts-ignore
101
+ markerClusterer.getMap().fitBounds(bounds)
102
+
103
+ // There is a fix for Issue 170 here:
104
+ this.timeOut = window.setTimeout(() => {
105
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
106
+ // @ts-ignore
107
+ markerClusterer.getMap().fitBounds(bounds)
108
+
109
+ // Don't zoom beyond the max zoom level
110
+ if (
111
+ maxZoom !== null &&
112
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
113
+ // @ts-ignore
114
+ markerClusterer.getMap().getZoom() > maxZoom
115
+ ) {
116
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
117
+ // @ts-ignore
118
+ markerClusterer.getMap().setZoom(maxZoom + 1)
119
+ }
120
+ }, 100)
121
+ }
61
122
 
123
+ // Prevent event propagation to the map:
124
+ event.cancelBubble = true
125
+
126
+ if (event.stopPropagation) {
127
+ event.stopPropagation()
128
+ }
129
+ }
130
+ }
131
+
132
+ onMouseOver() {
133
+ /**
134
+ * This event is fired when the mouse moves over a cluster marker.
135
+ * @name MarkerClusterer#mouseover
136
+ * @param {Cluster} c The cluster that the mouse moved over.
137
+ * @event
138
+ */
139
+ google.maps.event.trigger(
140
+ this.cluster.getClusterer(),
141
+ 'mouseover',
142
+ this.cluster
143
+ )
144
+ }
145
+
146
+ onMouseOut() {
147
+ /**
148
+ * This event is fired when the mouse moves out of a cluster marker.
149
+ * @name MarkerClusterer#mouseout
150
+ * @param {Cluster} c The cluster that the mouse moved out of.
151
+ * @event
152
+ */
153
+ google.maps.event.trigger(
154
+ this.cluster.getClusterer(),
155
+ 'mouseout',
156
+ this.cluster
157
+ )
158
+ }
159
+
160
+ onAdd() {
62
161
  this.div = document.createElement('div')
63
162
  this.div.className = this.className
64
163
  if (this.visible) {
65
164
  this.show()
66
165
  }
67
166
 
167
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
68
168
  // @ts-ignore
69
169
  this.getPanes().overlayMouseTarget.appendChild(this.div)
70
-
71
170
  // Fix for Issue 157
72
171
  this.boundsChangedListener = google.maps.event.addListener(
172
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
73
173
  // @ts-ignore
74
174
  this.getMap(),
75
- 'boundschanged',
76
- function boundsChanged() {
77
- cDraggingMapByCluster = cMouseDownInCluster
78
- }
175
+ 'bounds_changed',
176
+ this.onBoundsChanged
79
177
  )
80
178
 
81
- google.maps.event.addDomListener(this.div, 'mousedown', function onMouseDown() {
82
- cMouseDownInCluster = true
83
- cDraggingMapByCluster = false
84
- })
85
-
86
- google.maps.event.addDomListener(
87
- this.div,
88
- 'click',
89
- (event: Event) => {
90
- cMouseDownInCluster = false
91
-
92
- if (!cDraggingMapByCluster) {
93
- const markerClusterer = this.cluster.getClusterer()
94
-
95
- /**
96
- * This event is fired when a cluster marker is clicked.
97
- * @name MarkerClusterer#click
98
- * @param {Cluster} c The cluster that was clicked.
99
- * @event
100
- */
101
- google.maps.event.trigger(markerClusterer, 'click', this.cluster)
102
- google.maps.event.trigger(markerClusterer, 'clusterclick', this.cluster) // deprecated name
103
-
104
- // The default click handler follows. Disable it by setting
105
- // the zoomOnClick property to false.
106
- if (markerClusterer.getZoomOnClick()) {
107
- // Zoom into the cluster.
108
- const maxZoom = markerClusterer.getMaxZoom()
109
-
110
- const bounds = this.cluster.getBounds()
179
+ this.div.addEventListener('mousedown', this.onMouseDown)
111
180
 
112
- // @ts-ignore
113
- markerClusterer.getMap().fitBounds(bounds)
114
-
115
- // There is a fix for Issue 170 here:
116
- setTimeout(function timeout() {
117
- // @ts-ignore
118
- markerClusterer.getMap().fitBounds(bounds)
119
-
120
- // Don't zoom beyond the max zoom level
121
- // @ts-ignore
122
- if (maxZoom !== null && markerClusterer.getMap().getZoom() > maxZoom) {
123
- // @ts-ignore
124
- markerClusterer.getMap().setZoom(maxZoom + 1)
125
- }
126
- }, 100)
127
- }
128
-
129
- // Prevent event propagation to the map:
130
- event.cancelBubble = true
131
-
132
- if (event.stopPropagation) {
133
- event.stopPropagation()
134
- }
135
- }
136
- }
137
- )
181
+ this.div.addEventListener('click', this.onClick)
138
182
 
139
- google.maps.event.addDomListener(
140
- this.div,
141
- 'mouseover',
142
- () => {
143
- /**
144
- * This event is fired when the mouse moves over a cluster marker.
145
- * @name MarkerClusterer#mouseover
146
- * @param {Cluster} c The cluster that the mouse moved over.
147
- * @event
148
- */
149
- google.maps.event.trigger(this.cluster.getClusterer(), 'mouseover', this.cluster)
150
- }
151
- )
183
+ this.div.addEventListener('mouseover', this.onMouseOver)
152
184
 
153
- google.maps.event.addDomListener(
154
- this.div,
155
- 'mouseout',
156
- () => {
157
- /**
158
- * This event is fired when the mouse moves out of a cluster marker.
159
- * @name MarkerClusterer#mouseout
160
- * @param {Cluster} c The cluster that the mouse moved out of.
161
- * @event
162
- */
163
- google.maps.event.trigger(this.cluster.getClusterer(), 'mouseout', this.cluster)
164
- }
165
- )
185
+ this.div.addEventListener('mouseout', this.onMouseOut)
166
186
  }
167
187
 
168
188
  onRemove() {
@@ -173,10 +193,22 @@ export class ClusterIcon {
173
193
  google.maps.event.removeListener(this.boundsChangedListener)
174
194
  }
175
195
 
176
- google.maps.event.clearInstanceListeners(this.div)
196
+ this.div.removeEventListener('mousedown', this.onMouseDown)
197
+
198
+ this.div.removeEventListener('click', this.onClick)
199
+
200
+ this.div.removeEventListener('mouseover', this.onMouseOver)
201
+
202
+ this.div.removeEventListener('mouseout', this.onMouseOut)
177
203
 
178
204
  this.div.parentNode.removeChild(this.div)
179
205
 
206
+ if (this.timeOut !== null) {
207
+ window.clearTimeout(this.timeOut)
208
+
209
+ this.timeOut = null
210
+ }
211
+
180
212
  this.div = null
181
213
  }
182
214
  }
@@ -185,8 +217,8 @@ export class ClusterIcon {
185
217
  if (this.visible && this.div !== null && this.center) {
186
218
  const { x, y } = this.getPosFromLatLng(this.center)
187
219
 
188
- this.div.style.top = y + 'px'
189
- this.div.style.left = x + 'px'
220
+ this.div.style.top = `${y}px`
221
+ this.div.style.left = `${x}px`
190
222
  }
191
223
  }
192
224
 
@@ -210,7 +242,11 @@ export class ClusterIcon {
210
242
 
211
243
  const pos = this.getPosFromLatLng(this.center)
212
244
 
213
- if (this.sums === null || typeof this.sums.title === 'undefined' || this.sums.title === '') {
245
+ if (
246
+ this.sums === null ||
247
+ typeof this.sums.title === 'undefined' ||
248
+ this.sums.title === ''
249
+ ) {
214
250
  divTitle = this.cluster.getClusterer().getTitle()
215
251
  } else {
216
252
  divTitle = this.sums.title
@@ -231,10 +267,13 @@ export class ClusterIcon {
231
267
  img.style.left = `${spriteH}px`
232
268
 
233
269
  if (!this.cluster.getClusterer().enableRetinaIcons) {
234
- img.style.clip = `rect(-${spriteV}px, -${spriteH + this.width}px, -${spriteV + this.height}, -${spriteH})`
270
+ img.style.clip = `rect(-${spriteV}px, -${spriteH + this.width}px, -${
271
+ spriteV + this.height
272
+ }, -${spriteH})`
235
273
  }
236
274
 
237
275
  const textElm = document.createElement('div')
276
+
238
277
  textElm.style.position = 'absolute'
239
278
  textElm.style.top = `${this.anchorText[0]}px`
240
279
  textElm.style.left = `${this.anchorText[1]}px`
@@ -250,9 +289,12 @@ export class ClusterIcon {
250
289
  textElm.innerText = `${this.sums?.text}`
251
290
 
252
291
  this.div.innerHTML = ''
292
+
253
293
  this.div.appendChild(img)
254
294
  this.div.appendChild(textElm)
295
+
255
296
  this.div.title = divTitle
297
+
256
298
  this.div.style.display = ''
257
299
  }
258
300
 
@@ -261,14 +303,18 @@ export class ClusterIcon {
261
303
 
262
304
  useStyle(sums: ClusterIconInfo) {
263
305
  this.sums = sums
306
+
264
307
  const styles = this.cluster.getClusterer().getStyles()
265
- const style = styles[Math.min(styles.length - 1, Math.max(0, sums.index - 1))]
308
+
309
+ const style =
310
+ styles[Math.min(styles.length - 1, Math.max(0, sums.index - 1))]
266
311
 
267
312
  this.url = style.url
268
313
  this.height = style.height
269
314
  this.width = style.width
270
315
 
271
- if (style.className) this.className = `${this.clusterClassName} ${style.className}`
316
+ if (style.className)
317
+ this.className = `${this.clusterClassName} ${style.className}`
272
318
 
273
319
  this.anchorText = style.anchorText || [0, 0]
274
320
  this.anchorIcon = style.anchorIcon || [this.height / 2, this.width / 2]
@@ -300,10 +346,6 @@ export class ClusterIcon {
300
346
 
301
347
  pos.y -= this.anchorIcon[0]
302
348
 
303
- // pos.x = pos.x
304
-
305
- // pos.y = pos.y
306
-
307
349
  return pos
308
350
  }
309
351
  }
package/src/Clusterer.tsx CHANGED
@@ -134,11 +134,13 @@ export class Clusterer {
134
134
  this.setupStyles()
135
135
 
136
136
  this.addMarkers(optMarkers, true)
137
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
137
138
  // @ts-ignore
138
139
  this.setMap(map) // Note: this causes onAdd to be called
139
140
  }
140
141
 
141
142
  onAdd() {
143
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
142
144
  // @ts-ignore
143
145
  this.activeMap = this.getMap()
144
146
 
@@ -149,6 +151,7 @@ export class Clusterer {
149
151
  // Add the map event listeners
150
152
  this.listeners = [
151
153
  google.maps.event.addListener(
154
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
152
155
  // @ts-ignore
153
156
  this.getMap(),
154
157
  'zoom_changed',
@@ -160,8 +163,10 @@ export class Clusterer {
160
163
  // event is triggered so the cluster markers that have been removed
161
164
  // do not get redrawn. Same goes for a zoom in at maxZoom.
162
165
  if (
166
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
163
167
  // @ts-ignore
164
168
  this.getMap().getZoom() === (this.get('minZoom') || 0) ||
169
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
165
170
  // @ts-ignore
166
171
  this.getMap().getZoom() === this.get('maxZoom')
167
172
  ) {
@@ -170,6 +175,7 @@ export class Clusterer {
170
175
  }
171
176
  ),
172
177
  google.maps.event.addListener(
178
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
173
179
  // @ts-ignore
174
180
  this.getMap(),
175
181
  'idle',
@@ -236,6 +242,7 @@ export class Clusterer {
236
242
  }
237
243
  }
238
244
 
245
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
239
246
  // @ts-ignore
240
247
  this.getMap().fitBounds(bounds)
241
248
  }
@@ -489,6 +496,7 @@ export class Clusterer {
489
496
  }
490
497
 
491
498
  getExtendedBounds(bounds: google.maps.LatLngBounds): google.maps.LatLngBounds {
499
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
492
500
  // @ts-ignore
493
501
  const projection = this.getProjection()
494
502
  // Convert the points to pixels and the extend out by the grid size.
@@ -628,6 +636,7 @@ export class Clusterer {
628
636
  if (this.timerRefStatic !== null) {
629
637
  window.clearTimeout(this.timerRefStatic)
630
638
 
639
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
631
640
  // @ts-ignore
632
641
  delete this.timerRefStatic
633
642
  }
@@ -638,13 +647,16 @@ export class Clusterer {
638
647
  //
639
648
  // See Comments 9 & 11 on Issue 3651 relating to this workaround for a Google Maps bug:
640
649
  const mapBounds =
650
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
641
651
  // @ts-ignore
642
652
  this.getMap().getZoom() > 3
643
653
  ? new google.maps.LatLngBounds(
654
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
644
655
  // @ts-ignore
645
656
  this.getMap()
646
657
  .getBounds()
647
658
  .getSouthWest(),
659
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
648
660
  // @ts-ignore
649
661
  this.getMap()
650
662
  .getBounds()
@@ -662,10 +674,8 @@ export class Clusterer {
662
674
  for (let i = iFirst; i < iLast; i++) {
663
675
  const marker = this.markers[i]
664
676
 
665
- if (!marker.isAdded && this.isMarkerInBounds(marker, bounds)) {
666
- if (!this.ignoreHidden || (this.ignoreHidden && marker.getVisible())) {
667
- this.addToClosestCluster(marker)
668
- }
677
+ if (!marker.isAdded && this.isMarkerInBounds(marker, bounds) && (!this.ignoreHidden || (this.ignoreHidden && marker.getVisible()))) {
678
+ this.addToClosestCluster(marker)
669
679
  }
670
680
  }
671
681
 
@@ -698,10 +708,12 @@ export class Clusterer {
698
708
  return function applyExtend(object: any) {
699
709
  // eslint-disable-next-line guard-for-in
700
710
  for (const property in object.prototype) {
711
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
701
712
  // @ts-ignore
702
713
  this.prototype[property] = object.prototype[property]
703
714
  }
704
715
 
716
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
705
717
  // @ts-ignore
706
718
  return this
707
719
  }.apply(obj1, [obj2])
package/src/index.ts CHANGED
@@ -43,9 +43,9 @@ export { Cluster } from './Cluster'
43
43
  export { ClusterIcon } from './ClusterIcon'
44
44
 
45
45
  export {
46
- ClusterIconInfo,
47
- ClusterIconStyle,
48
- MarkerExtended,
49
- TCalculator,
50
- ClustererOptions,
46
+ type ClusterIconInfo,
47
+ type ClusterIconStyle,
48
+ type MarkerExtended,
49
+ type TCalculator,
50
+ type ClustererOptions,
51
51
  } from './types'