-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
1168 lines (1032 loc) · 28.4 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* A Marker Clusterer that clusters markers.
*
* @param {google.maps.Map} map The Google map to attach to.
* @param {Array.<google.maps.Marker>=} optMarkers Optional markers to add to
* the cluster.
* @param {Object=} optOptions support the following options:
* 'gridSize': (number) The grid size of a cluster in pixels.
* 'maxZoom': (number) The maximum zoom level that a marker can be part of a
* cluster.
* 'zoomOnClick': (boolean) Whether the default behaviour of clicking on a
* cluster is to zoom into it.
* 'averageCenter': (boolean) Whether the center of each cluster should be
* the average of all markers in the cluster.
* 'minimumClusterSize': (number) The minimum number of markers to be in a
* cluster before the markers are hidden and a count
* is shown.
* 'styles': (object) An object that has style properties:
* 'url': (string) The image url.
* 'height': (number) The image height.
* 'width': (number) The image width.
* 'anchor': (Array) The anchor position of the label text.
* 'textColor': (string) The text color.
* 'textSize': (number) The text size.
* 'backgroundPosition': (string) The position of the backgound x, y.
* 'iconAnchor': (Array) The anchor position of the icon x, y.
* @constructor
* @extends google.maps.OverlayView
*/
class MarkerClusterer {
constructor (map, optMarkers, optOptions) {
// MarkerClusterer implements google.maps.OverlayView interface. We use the
// extend function to extend MarkerClusterer with google.maps.OverlayView
// because it might not always be available when the code is defined so we
// look for it at the last possible moment. If it doesn't exist now then
// there is no point going ahead :)
this.extend(MarkerClusterer, google.maps.OverlayView)
this.map_ = map
/**
* @type {Array.<google.maps.Marker>}
* @private
*/
this.markers_ = []
/**
* @type {Array.<Cluster>}
*/
this.clusters_ = []
this.sizes = [53, 56, 66, 78, 90]
/**
* @private
*/
this.styles_ = []
/**
* @type {boolean}
* @private
*/
this.ready_ = false
const options = optOptions || {}
/**
* @type {number}
* @private
*/
this.gridSize_ = options['gridSize'] || 60
/**
* @private
*/
this.minClusterSize_ = options['minimumClusterSize'] || 2
/**
* @type {?number}
* @private
*/
this.maxZoom_ = options['maxZoom'] || null
this.styles_ = options['styles'] || []
/**
* @type {string}
* @private
*/
this.imagePath_ = options['imagePath'] ||
this.MARKER_CLUSTER_IMAGE_PATH_
/**
* @type {string}
* @private
*/
this.imageExtension_ = options['imageExtension'] ||
this.MARKER_CLUSTER_IMAGE_EXTENSION_
/**
* @type {boolean}
* @private
*/
this.zoomOnClick_ = true
if (options['zoomOnClick'] !== undefined) {
this.zoomOnClick_ = options['zoomOnClick']
}
/**
* @type {boolean}
* @private
*/
this.averageCenter_ = false
if (options['averageCenter'] !== undefined) {
this.averageCenter_ = options['averageCenter']
}
this.setupStyles_()
this.setMap(map)
/**
* @type {number}
* @private
*/
this.prevZoom_ = this.map_.getZoom()
// Add the map event listeners
const that = this
google.maps.event.addListener(this.map_, 'zoom_changed', () => {
const zoom = that.map_.getZoom()
if (that.prevZoom_ !== zoom) {
that.prevZoom_ = zoom
that.resetViewport()
}
})
google.maps.event.addListener(this.map_, 'idle', () => {
that.redraw()
})
// Finally, add the markers
if (optMarkers && optMarkers.length) {
this.addMarkers(optMarkers, false)
}
}
/**
* Extends a objects prototype by anothers.
*
* @param {Object} obj1 The object to be extended.
* @param {Object} obj2 The object to extend with.
* @return {Object} The new extended object.
* @ignore
*/
extend (obj1, obj2) {
return (function (object) {
for (const property in object.prototype) {
this.prototype[property] = object.prototype[property]
}
return this
}.apply(obj1, [obj2]))
}
/**
* Implementaion of the interface method.
* @ignore
*/
onAdd () {
this.setReady_(true)
}
/**
* Implementaion of the interface method.
* @ignore
*/
draw () {}
/**
* Sets up the styles object.
*
* @private
*/
setupStyles_ () {
if (this.styles_.length) {
return
}
for (let i = 0, size; size = this.sizes[i]; i++) {
this.styles_.push({
url: `${this.imagePath_ + (i + 1)}.${this.imageExtension_}`,
height: size,
width: size
})
}
}
/**
* Fit the map to the bounds of the markers in the clusterer.
*/
fitMapToMarkers () {
const markers = this.getMarkers()
const bounds = new google.maps.LatLngBounds()
for (let i = 0, marker; marker = markers[i]; i++) {
bounds.extend(marker.getPosition())
}
this.map_.fitBounds(bounds)
}
/**
* Sets the styles.
*
* @param {Object} styles The style to set.
*/
setStyles (styles) {
this.styles_ = styles
}
/**
* Gets the styles.
*
* @return {Object} The styles object.
*/
getStyles () {
return this.styles_
}
/**
* Whether zoom on click is set.
*
* @return {boolean} True if zoomOnClick_ is set.
*/
isZoomOnClick () {
return this.zoomOnClick_
}
/**
* Whether average center is set.
*
* @return {boolean} True if averageCenter_ is set.
*/
isAverageCenter () {
return this.averageCenter_
}
/**
* Returns the array of markers in the clusterer.
*
* @return {Array.<google.maps.Marker>} The markers.
*/
getMarkers () {
return this.markers_
}
/**
* Returns the number of markers in the clusterer
*
* @return {Number} The number of markers.
*/
getTotalMarkers () {
return this.markers_.length
}
/**
* Sets the max zoom for the clusterer.
*
* @param {number} maxZoom The max zoom level.
*/
setMaxZoom (maxZoom) {
this.maxZoom_ = maxZoom
}
/**
* Gets the max zoom for the clusterer.
*
* @return {number} The max zoom level.
*/
getMaxZoom () {
return this.maxZoom_
}
/**
* The function for calculating the cluster icon image.
*
* @param {Array.<google.maps.Marker>} markers The markers in the clusterer.
* @param {number} numStyles The number of styles available.
* @return {Object} A object properties: 'text' (string) and 'index' (number).
* @private
*/
calculator_ (markers, numStyles) {
let index = 0
const count = markers.length
let dv = count
while (dv !== 0) {
dv = parseInt(dv / 10, 10)
index++
}
index = Math.min(index, numStyles)
return {
text: count,
index
}
}
/**
* Set the calculator function.
*
* @param {function(Array, number)} calculator The function to set as the
* calculator. The function should return a object properties:
* 'text' (string) and 'index' (number).
*
*/
setCalculator (calculator) {
this.calculator_ = calculator
}
/**
* Get the calculator function.
*
* @return {function(Array, number)} the calculator function.
*/
getCalculator () {
return this.calculator_
}
/**
* Add an array of markers to the clusterer.
*
* @param {Array.<google.maps.Marker>} markers The markers to add.
* @param {boolean=} optNodraw Whether to redraw the clusters.
*/
addMarkers (markers, optNodraw) {
for (let i = 0, marker; marker = markers[i]; i++) {
this.pushMarkerTo_(marker)
}
if (!optNodraw) {
this.redraw()
}
}
/**
* Pushes a marker to the clusterer.
*
* @param {google.maps.Marker} marker The marker to add.
* @private
*/
pushMarkerTo_ (marker) {
marker.isAdded = false
if (marker['draggable']) {
// If the marker is draggable add a listener so we update the clusters on
// the drag end.
const that = this
google.maps.event.addListener(marker, 'dragend', () => {
marker.isAdded = false
that.repaint()
})
}
this.markers_.push(marker)
}
/**
* Adds a marker to the clusterer and redraws if needed.
*
* @param {google.maps.Marker} marker The marker to add.
* @param {boolean=} optNodraw Whether to redraw the clusters.
*/
addMarker (marker, optNodraw) {
this.pushMarkerTo_(marker)
if (!optNodraw) {
this.redraw()
}
}
/**
* Removes a marker and returns true if removed, false if not
*
* @param {google.maps.Marker} marker The marker to remove
* @return {boolean} Whether the marker was removed or not
* @private
*/
removeMarker_ (marker) {
let index = -1
if (this.markers_.indexOf) {
index = this.markers_.indexOf(marker)
} else {
for (let i = 0, m; m = this.markers_[i]; i++) {
if (m === marker) {
index = i
break
}
}
}
if (index === -1) {
// Marker is not in our list of markers.
return false
}
marker.setMap(null)
this.markers_.splice(index, 1)
return true
}
/**
* Remove a marker from the cluster.
*
* @param {google.maps.Marker} marker The marker to remove.
* @param {boolean=} optNodraw Optional boolean to force no redraw.
* @return {boolean} True if the marker was removed.
*/
removeMarker (marker, optNodraw) {
const removed = this.removeMarker_(marker)
if (!optNodraw && removed) {
this.resetViewport()
this.redraw()
return true
} else {
return false
}
}
/**
* Removes an array of markers from the cluster.
*
* @param {Array.<google.maps.Marker>} markers The markers to remove.
* @param {boolean=} optNodraw Optional boolean to force no redraw.
*/
removeMarkers (markers, optNodraw) {
let removed = false
for (let i = 0, marker; marker = markers[i]; i++) {
const r = this.removeMarker_(marker)
removed = removed || r
}
if (!optNodraw && removed) {
this.resetViewport()
this.redraw()
return true
}
}
/**
* Sets the clusterer's ready state.
*
* @param {boolean} ready The state.
* @private
*/
setReady_ (ready) {
if (!this.ready_) {
this.ready_ = ready
this.createClusters_()
}
}
/**
* Returns the number of clusters in the clusterer.
*
* @return {number} The number of clusters.
*/
getTotalClusters () {
return this.clusters_.length
}
/**
* Returns the google map that the clusterer is associated with.
*
* @return {google.maps.Map} The map.
*/
getMap () {
return this.map_
}
/**
* Sets the google map that the clusterer is associated with.
*
* @param {google.maps.Map} map The map.
*/
setMap (map) {
this.map_ = map
}
/**
* Returns the size of the grid.
*
* @return {number} The grid size.
*/
getGridSize () {
return this.gridSize_
}
/**
* Sets the size of the grid.
*
* @param {number} size The grid size.
*/
setGridSize (size) {
this.gridSize_ = size
}
/**
* Returns the min cluster size.
*
* @return {number} The grid size.
*/
getMinClusterSize () {
return this.minClusterSize_
}
/**
* Sets the min cluster size.
*
* @param {number} size The grid size.
*/
setMinClusterSize (size) {
this.minClusterSize_ = size
}
/**
* Extends a bounds object by the grid size.
*
* @param {google.maps.LatLngBounds} bounds The bounds to extend.
* @return {google.maps.LatLngBounds} The extended bounds.
*/
getExtendedBounds (bounds) {
const projection = this.getProjection()
// Turn the bounds into latlng.
const tr = new google.maps.LatLng(bounds.getNorthEast().lat(),
bounds.getNorthEast().lng())
const bl = new google.maps.LatLng(bounds.getSouthWest().lat(),
bounds.getSouthWest().lng())
// Convert the points to pixels and the extend out by the grid size.
const trPix = projection.fromLatLngToDivPixel(tr)
trPix.x += this.gridSize_
trPix.y -= this.gridSize_
const blPix = projection.fromLatLngToDivPixel(bl)
blPix.x -= this.gridSize_
blPix.y += this.gridSize_
// Convert the pixel points back to LatLng
const ne = projection.fromDivPixelToLatLng(trPix)
const sw = projection.fromDivPixelToLatLng(blPix)
// Extend the bounds to contain the new bounds.
bounds.extend(ne)
bounds.extend(sw)
return bounds
}
/**
* Determins if a marker is contained in a bounds.
*
* @param {google.maps.Marker} marker The marker to check.
* @param {google.maps.LatLngBounds} bounds The bounds to check against.
* @return {boolean} True if the marker is in the bounds.
* @private
*/
isMarkerInBounds_ (marker, bounds) {
return bounds.contains(marker.getPosition())
}
/**
* Clears all clusters and markers from the clusterer.
*/
clearMarkers () {
this.resetViewport(true)
// Set the markers a empty array.
this.markers_ = []
}
/**
* Clears all existing clusters and recreates them.
* @param {boolean} optHide To also hide the marker.
*/
resetViewport (optHide) {
// Remove all the clusters
for (var i = 0, cluster; cluster = this.clusters_[i]; i++) {
cluster.remove()
}
// Reset the markers to not be added and to be invisible.
for (let i = 0, marker; marker = this.markers_[i]; i++) {
marker.isAdded = false
if (optHide) {
marker.setMap(null)
}
}
this.clusters_ = []
}
/**
*
*/
repaint () {
const oldClusters = this.clusters_.slice()
this.clusters_.length = 0
this.resetViewport()
this.redraw()
// Remove the old clusters.
// Do it in a timeout so the other clusters have been drawn first.
window.setTimeout(() => {
for (let i = 0, cluster; cluster = oldClusters[i]; i++) {
cluster.remove()
}
}, 0)
}
/**
* Redraws the clusters.
*/
redraw () {
this.createClusters_()
}
/**
* Calculates the distance between two latlng locations in km.
* @see http://www.movable-type.co.uk/scripts/latlong.html
*
* @param {google.maps.LatLng} p1 The first lat lng point.
* @param {google.maps.LatLng} p2 The second lat lng point.
* @return {number} The distance between the two points in km.
* @private
*/
distanceBetweenPoints_ (p1, p2) {
if (!p1 || !p2) {
return 0
}
const R = 6371 // Radius of the Earth in km
const dLat = (p2.lat() - p1.lat()) * Math.PI / 180
const dLon = (p2.lng() - p1.lng()) * Math.PI / 180
const a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
Math.cos(p1.lat() * Math.PI / 180) * Math.cos(p2.lat() * Math.PI / 180) *
Math.sin(dLon / 2) * Math.sin(dLon / 2)
const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a))
const d = R * c
return d
}
/**
* Add a marker to a cluster, or creates a new cluster.
*
* @param {google.maps.Marker} marker The marker to add.
* @private
*/
addToClosestCluster_ (marker) {
let distance = 40000 // Some large number
let clusterToAddTo = null
for (var i = 0, cluster; cluster = this.clusters_[i]; i++) {
const center = cluster.getCenter()
if (center) {
const d = this.distanceBetweenPoints_(center, marker.getPosition())
if (d < distance) {
distance = d
clusterToAddTo = cluster
}
}
}
if (clusterToAddTo && clusterToAddTo.isMarkerInClusterBounds(marker)) {
clusterToAddTo.addMarker(marker)
} else {
var c = new Cluster(this)
c.addMarker(marker)
this.clusters_.push(c)
}
}
/**
* Creates the clusters.
*
* @private
*/
createClusters_ () {
if (!this.ready_) {
return
}
// Get our current map view bounds.
// Create a new bounds object so we don't affect the map.
const mapBounds = new google.maps.LatLngBounds(this.map_.getBounds().getSouthWest(),
this.map_.getBounds().getNorthEast())
const bounds = this.getExtendedBounds(mapBounds)
for (let i = 0, marker; marker = this.markers_[i]; i++) {
if (!marker.isAdded && this.isMarkerInBounds_(marker, bounds)) {
this.addToClosestCluster_(marker)
}
}
}
}
/**
* The marker cluster image path.
*
* @type {string}
* @private
*/
MarkerClusterer.prototype.MARKER_CLUSTER_IMAGE_PATH_ = '../images/m'
/**
* The marker cluster image path.
*
* @type {string}
* @private
*/
MarkerClusterer.prototype.MARKER_CLUSTER_IMAGE_EXTENSION_ = 'png'
/**
* A cluster that contains markers.
*
* @param {MarkerClusterer} markerClusterer The markerclusterer that this
* cluster is associated with.
* @constructor
* @ignore
*/
class Cluster {
constructor (markerClusterer) {
this.markerClusterer_ = markerClusterer
this.map_ = markerClusterer.getMap()
this.gridSize_ = markerClusterer.getGridSize()
this.minClusterSize_ = markerClusterer.getMinClusterSize()
this.averageCenter_ = markerClusterer.isAverageCenter()
this.center_ = null
this.markers_ = []
this.bounds_ = null
this.clusterIcon_ = new ClusterIcon(this, markerClusterer.getStyles(),
markerClusterer.getGridSize())
}
/**
* Determins if a marker is already added to the cluster.
*
* @param {google.maps.Marker} marker The marker to check.
* @return {boolean} True if the marker is already added.
*/
isMarkerAlreadyAdded (marker) {
if (this.markers_.indexOf) {
return this.markers_.includes(marker)
} else {
for (let i = 0, m; m = this.markers_[i]; i++) {
if (m === marker) {
return true
}
}
}
return false
}
/**
* Add a marker the cluster.
*
* @param {google.maps.Marker} marker The marker to add.
* @return {boolean} True if the marker was added.
*/
addMarker (marker) {
if (this.isMarkerAlreadyAdded(marker)) {
return false
}
if (!this.center_) {
this.center_ = marker.getPosition()
this.calculateBounds_()
} else {
if (this.averageCenter_) {
const l = this.markers_.length + 1
const lat = (this.center_.lat() * (l - 1) + marker.getPosition().lat()) / l
const lng = (this.center_.lng() * (l - 1) + marker.getPosition().lng()) / l
this.center_ = new google.maps.LatLng(lat, lng)
this.calculateBounds_()
}
}
marker.isAdded = true
this.markers_.push(marker)
const len = this.markers_.length
if (len < this.minClusterSize_ && marker.getMap() !== this.map_) {
// Min cluster size not reached so show the marker.
marker.setMap(this.map_)
}
if (len === this.minClusterSize_) {
// Hide the markers that were showing.
for (let i = 0; i < len; i++) {
this.markers_[i].setMap(null)
}
}
if (len >= this.minClusterSize_) {
marker.setMap(null)
}
this.updateIcon()
return true
}
/**
* Returns the marker clusterer that the cluster is associated with.
*
* @return {MarkerClusterer} The associated marker clusterer.
*/
getMarkerClusterer () {
return this.markerClusterer_
}
/**
* Returns the bounds of the cluster.
*
* @return {google.maps.LatLngBounds} the cluster bounds.
*/
getBounds () {
const bounds = new google.maps.LatLngBounds(this.center_, this.center_)
const markers = this.getMarkers()
for (let i = 0, marker; marker = markers[i]; i++) {
bounds.extend(marker.getPosition())
}
return bounds
}
/**
* Removes the cluster
*/
remove () {
this.clusterIcon_.remove()
this.markers_.length = 0
delete this.markers_
}
/**
* Returns the center of the cluster.
*
* @return {number} The cluster center.
*/
getSize () {
return this.markers_.length
}
/**
* Returns the center of the cluster.
*
* @return {Array.<google.maps.Marker>} The cluster center.
*/
getMarkers () {
return this.markers_
}
/**
* Returns the center of the cluster.
*
* @return {google.maps.LatLng} The cluster center.
*/
getCenter () {
return this.center_
}
/**
* Calculated the extended bounds of the cluster with the grid.
*
* @private
*/
calculateBounds_ () {
const bounds = new google.maps.LatLngBounds(this.center_, this.center_)
this.bounds_ = this.markerClusterer_.getExtendedBounds(bounds)
}
/**
* Determines if a marker lies in the clusters bounds.
*
* @param {google.maps.Marker} marker The marker to check.
* @return {boolean} True if the marker lies in the bounds.
*/
isMarkerInClusterBounds (marker) {
return this.bounds_.contains(marker.getPosition())
}
/**
* Returns the map that the cluster is associated with.
*
* @return {google.maps.Map} The map.
*/
getMap () {
return this.map_
}
/**
* Updates the cluster icon
*/
updateIcon () {
const zoom = this.map_.getZoom()
const mz = this.markerClusterer_.getMaxZoom()
if (mz && zoom > mz) {
// The zoom is greater than our max zoom so show all the markers in cluster.
for (let i = 0, marker; marker = this.markers_[i]; i++) {
marker.setMap(this.map_)
}
return
}
if (this.markers_.length < this.minClusterSize_) {
// Min cluster size not yet reached.
this.clusterIcon_.hide()
return
}
const numStyles = this.markerClusterer_.getStyles().length
const sums = this.markerClusterer_.getCalculator()(this.markers_, numStyles)
this.clusterIcon_.setCenter(this.center_)
this.clusterIcon_.setSums(sums)
this.clusterIcon_.show()
}
}
/**
* A cluster icon
*
* @param {Cluster} cluster The cluster to be associated with.
* @param {Object} styles An object that has style properties:
* 'url': (string) The image url.
* 'height': (number) The image height.
* 'width': (number) The image width.
* 'anchor': (Array) The anchor position of the label text.
* 'textColor': (string) The text color.
* 'textSize': (number) The text size.
* 'backgroundPosition: (string) The background postition x, y.
* @param {number=} optPadding Optional padding to apply to the cluster icon.
* @constructor
* @extends google.maps.OverlayView
* @ignore
*/
class ClusterIcon {
constructor (cluster, styles, optPadding) {
cluster.getMarkerClusterer().extend(ClusterIcon, google.maps.OverlayView)
this.styles_ = styles
this.padding_ = optPadding || 0
this.cluster_ = cluster
this.center_ = null
this.map_ = cluster.getMap()
this.div_ = null
this.sums_ = null
this.visible_ = false
this.setMap(this.map_)
}
/**
* Triggers the clusterclick event and zoom's if the option is set.
*
* @param {google.maps.MouseEvent} event The event to propagate
*/
triggerClusterClick (event) {
const markerClusterer = this.cluster_.getMarkerClusterer()
// Trigger the clusterclick event.
google.maps.event.trigger(markerClusterer, 'clusterclick', this.cluster_, event)
if (markerClusterer.isZoomOnClick()) {
// Zoom into the cluster.
this.map_.fitBounds(this.cluster_.getBounds())
}
}
/**
* Adding the cluster icon to the dom.
* @ignore
*/
onAdd () {
this.div_ = document.createElement('DIV')
if (this.visible_) {
const pos = this.getPosFromLatLng_(this.center_)
this.div_.style.cssText = this.createCss(pos)
this.div_.innerHTML = this.sums_.text
}
const panes = this.getPanes()
panes.overlayMouseTarget.appendChild(this.div_)
const that = this
let isDragging = false
google.maps.event.addDomListener(this.div_, 'click', event => {
// Only perform click when not preceded by a drag
if (!isDragging) {
that.triggerClusterClick(event)