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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
|
/*
* L.MarkerClusterGroup extends L.FeatureGroup by clustering the markers contained within
*/
export var MarkerClusterGroup = L.MarkerClusterGroup = L.FeatureGroup.extend({
options: {
maxClusterRadius: 80, //A cluster will cover at most this many pixels from its center
iconCreateFunction: null,
clusterPane: L.Marker.prototype.options.pane,
spiderfyOnMaxZoom: true,
showCoverageOnHover: true,
zoomToBoundsOnClick: true,
singleMarkerMode: false,
disableClusteringAtZoom: null,
// Setting this to false prevents the removal of any clusters outside of the viewpoint, which
// is the default behaviour for performance reasons.
removeOutsideVisibleBounds: true,
// Set to false to disable all animations (zoom and spiderfy).
// If false, option animateAddingMarkers below has no effect.
// If L.DomUtil.TRANSITION is falsy, this option has no effect.
animate: true,
//Whether to animate adding markers after adding the MarkerClusterGroup to the map
// If you are adding individual markers set to true, if adding bulk markers leave false for massive performance gains.
animateAddingMarkers: false,
// Make it possible to provide custom function to calculate spiderfy shape positions
spiderfyShapePositions: null,
//Increase to increase the distance away that spiderfied markers appear from the center
spiderfyDistanceMultiplier: 1,
// Make it possible to specify a polyline options on a spider leg
spiderLegPolylineOptions: { weight: 1.5, color: '#222', opacity: 0.5 },
// When bulk adding layers, adds markers in chunks. Means addLayers may not add all the layers in the call, others will be loaded during setTimeouts
chunkedLoading: false,
chunkInterval: 200, // process markers for a maximum of ~ n milliseconds (then trigger the chunkProgress callback)
chunkDelay: 50, // at the end of each interval, give n milliseconds back to system/browser
chunkProgress: null, // progress callback: function(processed, total, elapsed) (e.g. for a progress indicator)
//Options to pass to the L.Polygon constructor
polygonOptions: {}
},
initialize: function (options) {
L.Util.setOptions(this, options);
if (!this.options.iconCreateFunction) {
this.options.iconCreateFunction = this._defaultIconCreateFunction;
}
this._featureGroup = L.featureGroup();
this._featureGroup.addEventParent(this);
this._nonPointGroup = L.featureGroup();
this._nonPointGroup.addEventParent(this);
this._inZoomAnimation = 0;
this._needsClustering = [];
this._needsRemoving = []; //Markers removed while we aren't on the map need to be kept track of
//The bounds of the currently shown area (from _getExpandedVisibleBounds) Updated on zoom/move
this._currentShownBounds = null;
this._queue = [];
this._childMarkerEventHandlers = {
'dragstart': this._childMarkerDragStart,
'move': this._childMarkerMoved,
'dragend': this._childMarkerDragEnd,
};
// Hook the appropriate animation methods.
var animate = L.DomUtil.TRANSITION && this.options.animate;
L.extend(this, animate ? this._withAnimation : this._noAnimation);
// Remember which MarkerCluster class to instantiate (animated or not).
this._markerCluster = animate ? L.MarkerCluster : L.MarkerClusterNonAnimated;
},
addLayer: function (layer) {
if (layer instanceof L.LayerGroup) {
return this.addLayers([layer]);
}
//Don't cluster non point data
if (!layer.getLatLng) {
this._nonPointGroup.addLayer(layer);
this.fire('layeradd', { layer: layer });
return this;
}
if (!this._map) {
this._needsClustering.push(layer);
this.fire('layeradd', { layer: layer });
return this;
}
if (this.hasLayer(layer)) {
return this;
}
//If we have already clustered we'll need to add this one to a cluster
if (this._unspiderfy) {
this._unspiderfy();
}
this._addLayer(layer, this._maxZoom);
this.fire('layeradd', { layer: layer });
// Refresh bounds and weighted positions.
this._topClusterLevel._recalculateBounds();
this._refreshClustersIcons();
//Work out what is visible
var visibleLayer = layer,
currentZoom = this._zoom;
if (layer.__parent) {
while (visibleLayer.__parent._zoom >= currentZoom) {
visibleLayer = visibleLayer.__parent;
}
}
if (this._currentShownBounds.contains(visibleLayer.getLatLng())) {
if (this.options.animateAddingMarkers) {
this._animationAddLayer(layer, visibleLayer);
} else {
this._animationAddLayerNonAnimated(layer, visibleLayer);
}
}
return this;
},
removeLayer: function (layer) {
if (layer instanceof L.LayerGroup) {
return this.removeLayers([layer]);
}
//Non point layers
if (!layer.getLatLng) {
this._nonPointGroup.removeLayer(layer);
this.fire('layerremove', { layer: layer });
return this;
}
if (!this._map) {
if (!this._arraySplice(this._needsClustering, layer) && this.hasLayer(layer)) {
this._needsRemoving.push({ layer: layer, latlng: layer._latlng });
}
this.fire('layerremove', { layer: layer });
return this;
}
if (!layer.__parent) {
return this;
}
if (this._unspiderfy) {
this._unspiderfy();
this._unspiderfyLayer(layer);
}
//Remove the marker from clusters
this._removeLayer(layer, true);
this.fire('layerremove', { layer: layer });
// Refresh bounds and weighted positions.
this._topClusterLevel._recalculateBounds();
this._refreshClustersIcons();
layer.off(this._childMarkerEventHandlers, this);
if (this._featureGroup.hasLayer(layer)) {
this._featureGroup.removeLayer(layer);
if (layer.clusterShow) {
layer.clusterShow();
}
}
return this;
},
//Takes an array of markers and adds them in bulk
addLayers: function (layersArray, skipLayerAddEvent) {
if (!L.Util.isArray(layersArray)) {
return this.addLayer(layersArray);
}
var fg = this._featureGroup,
npg = this._nonPointGroup,
chunked = this.options.chunkedLoading,
chunkInterval = this.options.chunkInterval,
chunkProgress = this.options.chunkProgress,
l = layersArray.length,
offset = 0,
originalArray = true,
m;
if (this._map) {
var started = (new Date()).getTime();
var process = L.bind(function () {
var start = (new Date()).getTime();
// Make sure to unspiderfy before starting to add some layers
if (this._map && this._unspiderfy) {
this._unspiderfy();
}
for (; offset < l; offset++) {
if (chunked && offset % 200 === 0) {
// every couple hundred markers, instrument the time elapsed since processing started:
var elapsed = (new Date()).getTime() - start;
if (elapsed > chunkInterval) {
break; // been working too hard, time to take a break :-)
}
}
m = layersArray[offset];
// Group of layers, append children to layersArray and skip.
// Side effects:
// - Total increases, so chunkProgress ratio jumps backward.
// - Groups are not included in this group, only their non-group child layers (hasLayer).
// Changing array length while looping does not affect performance in current browsers:
// http://jsperf.com/for-loop-changing-length/6
if (m instanceof L.LayerGroup) {
if (originalArray) {
layersArray = layersArray.slice();
originalArray = false;
}
this._extractNonGroupLayers(m, layersArray);
l = layersArray.length;
continue;
}
//Not point data, can't be clustered
if (!m.getLatLng) {
npg.addLayer(m);
if (!skipLayerAddEvent) {
this.fire('layeradd', { layer: m });
}
continue;
}
if (this.hasLayer(m)) {
continue;
}
this._addLayer(m, this._maxZoom);
if (!skipLayerAddEvent) {
this.fire('layeradd', { layer: m });
}
//If we just made a cluster of size 2 then we need to remove the other marker from the map (if it is) or we never will
if (m.__parent) {
if (m.__parent.getChildCount() === 2) {
var markers = m.__parent.getAllChildMarkers(),
otherMarker = markers[0] === m ? markers[1] : markers[0];
fg.removeLayer(otherMarker);
}
}
}
if (chunkProgress) {
// report progress and time elapsed:
chunkProgress(offset, l, (new Date()).getTime() - started);
}
// Completed processing all markers.
if (offset === l) {
// Refresh bounds and weighted positions.
this._topClusterLevel._recalculateBounds();
this._refreshClustersIcons();
this._topClusterLevel._recursivelyAddChildrenToMap(null, this._zoom, this._currentShownBounds);
} else {
setTimeout(process, this.options.chunkDelay);
}
}, this);
process();
} else {
var needsClustering = this._needsClustering;
for (; offset < l; offset++) {
m = layersArray[offset];
// Group of layers, append children to layersArray and skip.
if (m instanceof L.LayerGroup) {
if (originalArray) {
layersArray = layersArray.slice();
originalArray = false;
}
this._extractNonGroupLayers(m, layersArray);
l = layersArray.length;
continue;
}
//Not point data, can't be clustered
if (!m.getLatLng) {
npg.addLayer(m);
continue;
}
if (this.hasLayer(m)) {
continue;
}
needsClustering.push(m);
}
}
return this;
},
//Takes an array of markers and removes them in bulk
removeLayers: function (layersArray) {
var i, m,
l = layersArray.length,
fg = this._featureGroup,
npg = this._nonPointGroup,
originalArray = true;
if (!this._map) {
for (i = 0; i < l; i++) {
m = layersArray[i];
// Group of layers, append children to layersArray and skip.
if (m instanceof L.LayerGroup) {
if (originalArray) {
layersArray = layersArray.slice();
originalArray = false;
}
this._extractNonGroupLayers(m, layersArray);
l = layersArray.length;
continue;
}
this._arraySplice(this._needsClustering, m);
npg.removeLayer(m);
if (this.hasLayer(m)) {
this._needsRemoving.push({ layer: m, latlng: m._latlng });
}
this.fire('layerremove', { layer: m });
}
return this;
}
if (this._unspiderfy) {
this._unspiderfy();
// Work on a copy of the array, so that next loop is not affected.
var layersArray2 = layersArray.slice(),
l2 = l;
for (i = 0; i < l2; i++) {
m = layersArray2[i];
// Group of layers, append children to layersArray and skip.
if (m instanceof L.LayerGroup) {
this._extractNonGroupLayers(m, layersArray2);
l2 = layersArray2.length;
continue;
}
this._unspiderfyLayer(m);
}
}
for (i = 0; i < l; i++) {
m = layersArray[i];
// Group of layers, append children to layersArray and skip.
if (m instanceof L.LayerGroup) {
if (originalArray) {
layersArray = layersArray.slice();
originalArray = false;
}
this._extractNonGroupLayers(m, layersArray);
l = layersArray.length;
continue;
}
if (!m.__parent) {
npg.removeLayer(m);
this.fire('layerremove', { layer: m });
continue;
}
this._removeLayer(m, true, true);
this.fire('layerremove', { layer: m });
if (fg.hasLayer(m)) {
fg.removeLayer(m);
if (m.clusterShow) {
m.clusterShow();
}
}
}
// Refresh bounds and weighted positions.
this._topClusterLevel._recalculateBounds();
this._refreshClustersIcons();
//Fix up the clusters and markers on the map
this._topClusterLevel._recursivelyAddChildrenToMap(null, this._zoom, this._currentShownBounds);
return this;
},
//Removes all layers from the MarkerClusterGroup
clearLayers: function () {
//Need our own special implementation as the LayerGroup one doesn't work for us
//If we aren't on the map (yet), blow away the markers we know of
if (!this._map) {
this._needsClustering = [];
this._needsRemoving = [];
delete this._gridClusters;
delete this._gridUnclustered;
}
if (this._noanimationUnspiderfy) {
this._noanimationUnspiderfy();
}
//Remove all the visible layers
this._featureGroup.clearLayers();
this._nonPointGroup.clearLayers();
this.eachLayer(function (marker) {
marker.off(this._childMarkerEventHandlers, this);
delete marker.__parent;
}, this);
if (this._map) {
//Reset _topClusterLevel and the DistanceGrids
this._generateInitialClusters();
}
return this;
},
//Override FeatureGroup.getBounds as it doesn't work
getBounds: function () {
var bounds = new L.LatLngBounds();
if (this._topClusterLevel) {
bounds.extend(this._topClusterLevel._bounds);
}
for (var i = this._needsClustering.length - 1; i >= 0; i--) {
bounds.extend(this._needsClustering[i].getLatLng());
}
bounds.extend(this._nonPointGroup.getBounds());
return bounds;
},
//Overrides LayerGroup.eachLayer
eachLayer: function (method, context) {
var markers = this._needsClustering.slice(),
needsRemoving = this._needsRemoving,
thisNeedsRemoving, i, j;
if (this._topClusterLevel) {
this._topClusterLevel.getAllChildMarkers(markers);
}
for (i = markers.length - 1; i >= 0; i--) {
thisNeedsRemoving = true;
for (j = needsRemoving.length - 1; j >= 0; j--) {
if (needsRemoving[j].layer === markers[i]) {
thisNeedsRemoving = false;
break;
}
}
if (thisNeedsRemoving) {
method.call(context, markers[i]);
}
}
this._nonPointGroup.eachLayer(method, context);
},
//Overrides LayerGroup.getLayers
getLayers: function () {
var layers = [];
this.eachLayer(function (l) {
layers.push(l);
});
return layers;
},
//Overrides LayerGroup.getLayer, WARNING: Really bad performance
getLayer: function (id) {
var result = null;
id = parseInt(id, 10);
this.eachLayer(function (l) {
if (L.stamp(l) === id) {
result = l;
}
});
return result;
},
//Returns true if the given layer is in this MarkerClusterGroup
hasLayer: function (layer) {
if (!layer) {
return false;
}
var i, anArray = this._needsClustering;
for (i = anArray.length - 1; i >= 0; i--) {
if (anArray[i] === layer) {
return true;
}
}
anArray = this._needsRemoving;
for (i = anArray.length - 1; i >= 0; i--) {
if (anArray[i].layer === layer) {
return false;
}
}
return !!(layer.__parent && layer.__parent._group === this) || this._nonPointGroup.hasLayer(layer);
},
//Zoom down to show the given layer (spiderfying if necessary) then calls the callback
zoomToShowLayer: function (layer, callback) {
var map = this._map;
if (typeof callback !== 'function') {
callback = function () {};
}
var showMarker = function () {
// Assumes that map.hasLayer checks for direct appearance on map, not recursively calling
// hasLayer on Layer Groups that are on map (typically not calling this MarkerClusterGroup.hasLayer, which would always return true)
if ((map.hasLayer(layer) || map.hasLayer(layer.__parent)) && !this._inZoomAnimation) {
this._map.off('moveend', showMarker, this);
this.off('animationend', showMarker, this);
if (map.hasLayer(layer)) {
callback();
} else if (layer.__parent._icon) {
this.once('spiderfied', callback, this);
layer.__parent.spiderfy();
}
}
};
if (layer._icon && this._map.getBounds().contains(layer.getLatLng())) {
//Layer is visible ond on screen, immediate return
callback();
} else if (layer.__parent._zoom < Math.round(this._map._zoom)) {
//Layer should be visible at this zoom level. It must not be on screen so just pan over to it
this._map.on('moveend', showMarker, this);
this._map.panTo(layer.getLatLng());
} else {
this._map.on('moveend', showMarker, this);
this.on('animationend', showMarker, this);
layer.__parent.zoomToBounds();
}
},
//Overrides FeatureGroup.onAdd
onAdd: function (map) {
this._map = map;
var i, l, layer;
if (!isFinite(this._map.getMaxZoom())) {
throw "Map has no maxZoom specified";
}
this._featureGroup.addTo(map);
this._nonPointGroup.addTo(map);
if (!this._gridClusters) {
this._generateInitialClusters();
}
this._maxLat = map.options.crs.projection.MAX_LATITUDE;
//Restore all the positions as they are in the MCG before removing them
for (i = 0, l = this._needsRemoving.length; i < l; i++) {
layer = this._needsRemoving[i];
layer.newlatlng = layer.layer._latlng;
layer.layer._latlng = layer.latlng;
}
//Remove them, then restore their new positions
for (i = 0, l = this._needsRemoving.length; i < l; i++) {
layer = this._needsRemoving[i];
this._removeLayer(layer.layer, true);
layer.layer._latlng = layer.newlatlng;
}
this._needsRemoving = [];
//Remember the current zoom level and bounds
this._zoom = Math.round(this._map._zoom);
this._currentShownBounds = this._getExpandedVisibleBounds();
this._map.on('zoomend', this._zoomEnd, this);
this._map.on('moveend', this._moveEnd, this);
if (this._spiderfierOnAdd) { //TODO FIXME: Not sure how to have spiderfier add something on here nicely
this._spiderfierOnAdd();
}
this._bindEvents();
//Actually add our markers to the map:
l = this._needsClustering;
this._needsClustering = [];
this.addLayers(l, true);
},
//Overrides FeatureGroup.onRemove
onRemove: function (map) {
map.off('zoomend', this._zoomEnd, this);
map.off('moveend', this._moveEnd, this);
this._unbindEvents();
//In case we are in a cluster animation
this._map._mapPane.className = this._map._mapPane.className.replace(' leaflet-cluster-anim', '');
if (this._spiderfierOnRemove) { //TODO FIXME: Not sure how to have spiderfier add something on here nicely
this._spiderfierOnRemove();
}
delete this._maxLat;
//Clean up all the layers we added to the map
this._hideCoverage();
this._featureGroup.remove();
this._nonPointGroup.remove();
this._featureGroup.clearLayers();
this._map = null;
},
getVisibleParent: function (marker) {
var vMarker = marker;
while (vMarker && !vMarker._icon) {
vMarker = vMarker.__parent;
}
return vMarker || null;
},
//Remove the given object from the given array
_arraySplice: function (anArray, obj) {
for (var i = anArray.length - 1; i >= 0; i--) {
if (anArray[i] === obj) {
anArray.splice(i, 1);
return true;
}
}
},
/**
* Removes a marker from all _gridUnclustered zoom levels, starting at the supplied zoom.
* @param marker to be removed from _gridUnclustered.
* @param z integer bottom start zoom level (included)
* @private
*/
_removeFromGridUnclustered: function (marker, z) {
var map = this._map,
gridUnclustered = this._gridUnclustered,
minZoom = Math.floor(this._map.getMinZoom());
for (; z >= minZoom; z--) {
if (!gridUnclustered[z].removeObject(marker, map.project(marker.getLatLng(), z))) {
break;
}
}
},
_childMarkerDragStart: function (e) {
e.target.__dragStart = e.target._latlng;
},
_childMarkerMoved: function (e) {
if (!this._ignoreMove && !e.target.__dragStart) {
var isPopupOpen = e.target._popup && e.target._popup.isOpen();
this._moveChild(e.target, e.oldLatLng, e.latlng);
if (isPopupOpen) {
e.target.openPopup();
}
}
},
_moveChild: function (layer, from, to) {
layer._latlng = from;
this.removeLayer(layer);
layer._latlng = to;
this.addLayer(layer);
},
_childMarkerDragEnd: function (e) {
var dragStart = e.target.__dragStart;
delete e.target.__dragStart;
if (dragStart) {
this._moveChild(e.target, dragStart, e.target._latlng);
}
},
//Internal function for removing a marker from everything.
//dontUpdateMap: set to true if you will handle updating the map manually (for bulk functions)
_removeLayer: function (marker, removeFromDistanceGrid, dontUpdateMap) {
var gridClusters = this._gridClusters,
gridUnclustered = this._gridUnclustered,
fg = this._featureGroup,
map = this._map,
minZoom = Math.floor(this._map.getMinZoom());
//Remove the marker from distance clusters it might be in
if (removeFromDistanceGrid) {
this._removeFromGridUnclustered(marker, this._maxZoom);
}
//Work our way up the clusters removing them as we go if required
var cluster = marker.__parent,
markers = cluster._markers,
otherMarker;
//Remove the marker from the immediate parents marker list
this._arraySplice(markers, marker);
while (cluster) {
cluster._childCount--;
cluster._boundsNeedUpdate = true;
if (cluster._zoom < minZoom) {
//Top level, do nothing
break;
} else if (removeFromDistanceGrid && cluster._childCount <= 1) { //Cluster no longer required
//We need to push the other marker up to the parent
otherMarker = cluster._markers[0] === marker ? cluster._markers[1] : cluster._markers[0];
//Update distance grid
gridClusters[cluster._zoom].removeObject(cluster, map.project(cluster._cLatLng, cluster._zoom));
gridUnclustered[cluster._zoom].addObject(otherMarker, map.project(otherMarker.getLatLng(), cluster._zoom));
//Move otherMarker up to parent
this._arraySplice(cluster.__parent._childClusters, cluster);
cluster.__parent._markers.push(otherMarker);
otherMarker.__parent = cluster.__parent;
if (cluster._icon) {
//Cluster is currently on the map, need to put the marker on the map instead
fg.removeLayer(cluster);
if (!dontUpdateMap) {
fg.addLayer(otherMarker);
}
}
} else {
cluster._iconNeedsUpdate = true;
}
cluster = cluster.__parent;
}
delete marker.__parent;
},
_isOrIsParent: function (el, oel) {
while (oel) {
if (el === oel) {
return true;
}
oel = oel.parentNode;
}
return false;
},
//Override L.Evented.fire
fire: function (type, data, propagate) {
if (data && data.layer instanceof L.MarkerCluster) {
//Prevent multiple clustermouseover/off events if the icon is made up of stacked divs (Doesn't work in ie <= 8, no relatedTarget)
if (data.originalEvent && this._isOrIsParent(data.layer._icon, data.originalEvent.relatedTarget)) {
return;
}
type = 'cluster' + type;
}
L.FeatureGroup.prototype.fire.call(this, type, data, propagate);
},
//Override L.Evented.listens
listens: function (type, propagate) {
return L.FeatureGroup.prototype.listens.call(this, type, propagate) || L.FeatureGroup.prototype.listens.call(this, 'cluster' + type, propagate);
},
//Default functionality
_defaultIconCreateFunction: function (cluster) {
var childCount = cluster.getChildCount();
var c = ' marker-cluster-';
if (childCount < 10) {
c += 'small';
} else if (childCount < 100) {
c += 'medium';
} else {
c += 'large';
}
return new L.DivIcon({ html: '<div><span>' + childCount + '</span></div>', className: 'marker-cluster' + c, iconSize: new L.Point(40, 40) });
},
_bindEvents: function () {
var map = this._map,
spiderfyOnMaxZoom = this.options.spiderfyOnMaxZoom,
showCoverageOnHover = this.options.showCoverageOnHover,
zoomToBoundsOnClick = this.options.zoomToBoundsOnClick;
//Zoom on cluster click or spiderfy if we are at the lowest level
if (spiderfyOnMaxZoom || zoomToBoundsOnClick) {
this.on('clusterclick', this._zoomOrSpiderfy, this);
}
//Show convex hull (boundary) polygon on mouse over
if (showCoverageOnHover) {
this.on('clustermouseover', this._showCoverage, this);
this.on('clustermouseout', this._hideCoverage, this);
map.on('zoomend', this._hideCoverage, this);
}
},
_zoomOrSpiderfy: function (e) {
var cluster = e.layer,
bottomCluster = cluster;
while (bottomCluster._childClusters.length === 1) {
bottomCluster = bottomCluster._childClusters[0];
}
if (bottomCluster._zoom === this._maxZoom &&
bottomCluster._childCount === cluster._childCount &&
this.options.spiderfyOnMaxZoom) {
// All child markers are contained in a single cluster from this._maxZoom to this cluster.
cluster.spiderfy();
} else if (this.options.zoomToBoundsOnClick) {
cluster.zoomToBounds();
}
// Focus the map again for keyboard users.
if (e.originalEvent && e.originalEvent.keyCode === 13) {
this._map._container.focus();
}
},
_showCoverage: function (e) {
var map = this._map;
if (this._inZoomAnimation) {
return;
}
if (this._shownPolygon) {
map.removeLayer(this._shownPolygon);
}
if (e.layer.getChildCount() > 2 && e.layer !== this._spiderfied) {
this._shownPolygon = new L.Polygon(e.layer.getConvexHull(), this.options.polygonOptions);
map.addLayer(this._shownPolygon);
}
},
_hideCoverage: function () {
if (this._shownPolygon) {
this._map.removeLayer(this._shownPolygon);
this._shownPolygon = null;
}
},
_unbindEvents: function () {
var spiderfyOnMaxZoom = this.options.spiderfyOnMaxZoom,
showCoverageOnHover = this.options.showCoverageOnHover,
zoomToBoundsOnClick = this.options.zoomToBoundsOnClick,
map = this._map;
if (spiderfyOnMaxZoom || zoomToBoundsOnClick) {
this.off('clusterclick', this._zoomOrSpiderfy, this);
}
if (showCoverageOnHover) {
this.off('clustermouseover', this._showCoverage, this);
this.off('clustermouseout', this._hideCoverage, this);
map.off('zoomend', this._hideCoverage, this);
}
},
_zoomEnd: function () {
if (!this._map) { //May have been removed from the map by a zoomEnd handler
return;
}
this._mergeSplitClusters();
this._zoom = Math.round(this._map._zoom);
this._currentShownBounds = this._getExpandedVisibleBounds();
},
_moveEnd: function () {
if (this._inZoomAnimation) {
return;
}
var newBounds = this._getExpandedVisibleBounds();
this._topClusterLevel._recursivelyRemoveChildrenFromMap(this._currentShownBounds, Math.floor(this._map.getMinZoom()), this._zoom, newBounds);
this._topClusterLevel._recursivelyAddChildrenToMap(null, Math.round(this._map._zoom), newBounds);
this._currentShownBounds = newBounds;
return;
},
_generateInitialClusters: function () {
var maxZoom = Math.ceil(this._map.getMaxZoom()),
minZoom = Math.floor(this._map.getMinZoom()),
radius = this.options.maxClusterRadius,
radiusFn = radius;
//If we just set maxClusterRadius to a single number, we need to create
//a simple function to return that number. Otherwise, we just have to
//use the function we've passed in.
if (typeof radius !== "function") {
radiusFn = function () { return radius; };
}
if (this.options.disableClusteringAtZoom !== null) {
maxZoom = this.options.disableClusteringAtZoom - 1;
}
this._maxZoom = maxZoom;
this._gridClusters = {};
this._gridUnclustered = {};
//Set up DistanceGrids for each zoom
for (var zoom = maxZoom; zoom >= minZoom; zoom--) {
this._gridClusters[zoom] = new L.DistanceGrid(radiusFn(zoom));
this._gridUnclustered[zoom] = new L.DistanceGrid(radiusFn(zoom));
}
// Instantiate the appropriate L.MarkerCluster class (animated or not).
this._topClusterLevel = new this._markerCluster(this, minZoom - 1);
},
//Zoom: Zoom to start adding at (Pass this._maxZoom to start at the bottom)
_addLayer: function (layer, zoom) {
var gridClusters = this._gridClusters,
gridUnclustered = this._gridUnclustered,
minZoom = Math.floor(this._map.getMinZoom()),
markerPoint, z;
if (this.options.singleMarkerMode) {
this._overrideMarkerIcon(layer);
}
layer.on(this._childMarkerEventHandlers, this);
//Find the lowest zoom level to slot this one in
for (; zoom >= minZoom; zoom--) {
markerPoint = this._map.project(layer.getLatLng(), zoom); // calculate pixel position
//Try find a cluster close by
var closest = gridClusters[zoom].getNearObject(markerPoint);
if (closest) {
closest._addChild(layer);
layer.__parent = closest;
return;
}
//Try find a marker close by to form a new cluster with
closest = gridUnclustered[zoom].getNearObject(markerPoint);
if (closest) {
var parent = closest.__parent;
if (parent) {
this._removeLayer(closest, false);
}
//Create new cluster with these 2 in it
var newCluster = new this._markerCluster(this, zoom, closest, layer);
gridClusters[zoom].addObject(newCluster, this._map.project(newCluster._cLatLng, zoom));
closest.__parent = newCluster;
layer.__parent = newCluster;
//First create any new intermediate parent clusters that don't exist
var lastParent = newCluster;
for (z = zoom - 1; z > parent._zoom; z--) {
lastParent = new this._markerCluster(this, z, lastParent);
gridClusters[z].addObject(lastParent, this._map.project(closest.getLatLng(), z));
}
parent._addChild(lastParent);
//Remove closest from this zoom level and any above that it is in, replace with newCluster
this._removeFromGridUnclustered(closest, zoom);
return;
}
//Didn't manage to cluster in at this zoom, record us as a marker here and continue upwards
gridUnclustered[zoom].addObject(layer, markerPoint);
}
//Didn't get in anything, add us to the top
this._topClusterLevel._addChild(layer);
layer.__parent = this._topClusterLevel;
return;
},
/**
* Refreshes the icon of all "dirty" visible clusters.
* Non-visible "dirty" clusters will be updated when they are added to the map.
* @private
*/
_refreshClustersIcons: function () {
this._featureGroup.eachLayer(function (c) {
if (c instanceof L.MarkerCluster && c._iconNeedsUpdate) {
c._updateIcon();
}
});
},
//Enqueue code to fire after the marker expand/contract has happened
_enqueue: function (fn) {
this._queue.push(fn);
if (!this._queueTimeout) {
this._queueTimeout = setTimeout(L.bind(this._processQueue, this), 300);
}
},
_processQueue: function () {
for (var i = 0; i < this._queue.length; i++) {
this._queue[i].call(this);
}
this._queue.length = 0;
clearTimeout(this._queueTimeout);
this._queueTimeout = null;
},
//Merge and split any existing clusters that are too big or small
_mergeSplitClusters: function () {
var mapZoom = Math.round(this._map._zoom);
//In case we are starting to split before the animation finished
this._processQueue();
if (this._zoom < mapZoom && this._currentShownBounds.intersects(this._getExpandedVisibleBounds())) { //Zoom in, split
this._animationStart();
//Remove clusters now off screen
this._topClusterLevel._recursivelyRemoveChildrenFromMap(this._currentShownBounds, Math.floor(this._map.getMinZoom()), this._zoom, this._getExpandedVisibleBounds());
this._animationZoomIn(this._zoom, mapZoom);
} else if (this._zoom > mapZoom) { //Zoom out, merge
this._animationStart();
this._animationZoomOut(this._zoom, mapZoom);
} else {
this._moveEnd();
}
},
//Gets the maps visible bounds expanded in each direction by the size of the screen (so the user cannot see an area we do not cover in one pan)
_getExpandedVisibleBounds: function () {
if (!this.options.removeOutsideVisibleBounds) {
return this._mapBoundsInfinite;
} else if (L.Browser.mobile) {
return this._checkBoundsMaxLat(this._map.getBounds());
}
return this._checkBoundsMaxLat(this._map.getBounds().pad(1)); // Padding expands the bounds by its own dimensions but scaled with the given factor.
},
/**
* Expands the latitude to Infinity (or -Infinity) if the input bounds reach the map projection maximum defined latitude
* (in the case of Web/Spherical Mercator, it is 85.0511287798 / see https://en.wikipedia.org/wiki/Web_Mercator#Formulas).
* Otherwise, the removeOutsideVisibleBounds option will remove markers beyond that limit, whereas the same markers without
* this option (or outside MCG) will have their position floored (ceiled) by the projection and rendered at that limit,
* making the user think that MCG "eats" them and never displays them again.
* @param bounds L.LatLngBounds
* @returns {L.LatLngBounds}
* @private
*/
_checkBoundsMaxLat: function (bounds) {
var maxLat = this._maxLat;
if (maxLat !== undefined) {
if (bounds.getNorth() >= maxLat) {
bounds._northEast.lat = Infinity;
}
if (bounds.getSouth() <= -maxLat) {
bounds._southWest.lat = -Infinity;
}
}
return bounds;
},
//Shared animation code
_animationAddLayerNonAnimated: function (layer, newCluster) {
if (newCluster === layer) {
this._featureGroup.addLayer(layer);
} else if (newCluster._childCount === 2) {
newCluster._addToMap();
var markers = newCluster.getAllChildMarkers();
this._featureGroup.removeLayer(markers[0]);
this._featureGroup.removeLayer(markers[1]);
} else {
newCluster._updateIcon();
}
},
/**
* Extracts individual (i.e. non-group) layers from a Layer Group.
* @param group to extract layers from.
* @param output {Array} in which to store the extracted layers.
* @returns {*|Array}
* @private
*/
_extractNonGroupLayers: function (group, output) {
var layers = group.getLayers(),
i = 0,
layer;
output = output || [];
for (; i < layers.length; i++) {
layer = layers[i];
if (layer instanceof L.LayerGroup) {
this._extractNonGroupLayers(layer, output);
continue;
}
output.push(layer);
}
return output;
},
/**
* Implements the singleMarkerMode option.
* @param layer Marker to re-style using the Clusters iconCreateFunction.
* @returns {L.Icon} The newly created icon.
* @private
*/
_overrideMarkerIcon: function (layer) {
var icon = layer.options.icon = this.options.iconCreateFunction({
getChildCount: function () {
return 1;
},
getAllChildMarkers: function () {
return [layer];
}
});
return icon;
}
});
// Constant bounds used in case option "removeOutsideVisibleBounds" is set to false.
L.MarkerClusterGroup.include({
_mapBoundsInfinite: new L.LatLngBounds(new L.LatLng(-Infinity, -Infinity), new L.LatLng(Infinity, Infinity))
});
L.MarkerClusterGroup.include({
_noAnimation: {
//Non Animated versions of everything
_animationStart: function () {
//Do nothing...
},
_animationZoomIn: function (previousZoomLevel, newZoomLevel) {
this._topClusterLevel._recursivelyRemoveChildrenFromMap(this._currentShownBounds, Math.floor(this._map.getMinZoom()), previousZoomLevel);
this._topClusterLevel._recursivelyAddChildrenToMap(null, newZoomLevel, this._getExpandedVisibleBounds());
//We didn't actually animate, but we use this event to mean "clustering animations have finished"
this.fire('animationend');
},
_animationZoomOut: function (previousZoomLevel, newZoomLevel) {
this._topClusterLevel._recursivelyRemoveChildrenFromMap(this._currentShownBounds, Math.floor(this._map.getMinZoom()), previousZoomLevel);
this._topClusterLevel._recursivelyAddChildrenToMap(null, newZoomLevel, this._getExpandedVisibleBounds());
//We didn't actually animate, but we use this event to mean "clustering animations have finished"
this.fire('animationend');
},
_animationAddLayer: function (layer, newCluster) {
this._animationAddLayerNonAnimated(layer, newCluster);
}
},
_withAnimation: {
//Animated versions here
_animationStart: function () {
this._map._mapPane.className += ' leaflet-cluster-anim';
this._inZoomAnimation++;
},
_animationZoomIn: function (previousZoomLevel, newZoomLevel) {
var bounds = this._getExpandedVisibleBounds(),
fg = this._featureGroup,
minZoom = Math.floor(this._map.getMinZoom()),
i;
this._ignoreMove = true;
//Add all children of current clusters to map and remove those clusters from map
this._topClusterLevel._recursively(bounds, previousZoomLevel, minZoom, function (c) {
var startPos = c._latlng,
markers = c._markers,
m;
if (!bounds.contains(startPos)) {
startPos = null;
}
if (c._isSingleParent() && previousZoomLevel + 1 === newZoomLevel) { //Immediately add the new child and remove us
fg.removeLayer(c);
c._recursivelyAddChildrenToMap(null, newZoomLevel, bounds);
} else {
//Fade out old cluster
c.clusterHide();
c._recursivelyAddChildrenToMap(startPos, newZoomLevel, bounds);
}
//Remove all markers that aren't visible any more
//TODO: Do we actually need to do this on the higher levels too?
for (i = markers.length - 1; i >= 0; i--) {
m = markers[i];
if (!bounds.contains(m._latlng)) {
fg.removeLayer(m);
}
}
});
this._forceLayout();
//Update opacities
this._topClusterLevel._recursivelyBecomeVisible(bounds, newZoomLevel);
//TODO Maybe? Update markers in _recursivelyBecomeVisible
fg.eachLayer(function (n) {
if (!(n instanceof L.MarkerCluster) && n._icon) {
n.clusterShow();
}
});
//update the positions of the just added clusters/markers
this._topClusterLevel._recursively(bounds, previousZoomLevel, newZoomLevel, function (c) {
c._recursivelyRestoreChildPositions(newZoomLevel);
});
this._ignoreMove = false;
//Remove the old clusters and close the zoom animation
this._enqueue(function () {
//update the positions of the just added clusters/markers
this._topClusterLevel._recursively(bounds, previousZoomLevel, minZoom, function (c) {
fg.removeLayer(c);
c.clusterShow();
});
this._animationEnd();
});
},
_animationZoomOut: function (previousZoomLevel, newZoomLevel) {
this._animationZoomOutSingle(this._topClusterLevel, previousZoomLevel - 1, newZoomLevel);
//Need to add markers for those that weren't on the map before but are now
this._topClusterLevel._recursivelyAddChildrenToMap(null, newZoomLevel, this._getExpandedVisibleBounds());
//Remove markers that were on the map before but won't be now
this._topClusterLevel._recursivelyRemoveChildrenFromMap(this._currentShownBounds, Math.floor(this._map.getMinZoom()), previousZoomLevel, this._getExpandedVisibleBounds());
},
_animationAddLayer: function (layer, newCluster) {
var me = this,
fg = this._featureGroup;
fg.addLayer(layer);
if (newCluster !== layer) {
if (newCluster._childCount > 2) { //Was already a cluster
newCluster._updateIcon();
this._forceLayout();
this._animationStart();
layer._setPos(this._map.latLngToLayerPoint(newCluster.getLatLng()));
layer.clusterHide();
this._enqueue(function () {
fg.removeLayer(layer);
layer.clusterShow();
me._animationEnd();
});
} else { //Just became a cluster
this._forceLayout();
me._animationStart();
me._animationZoomOutSingle(newCluster, this._map.getMaxZoom(), this._zoom);
}
}
}
},
// Private methods for animated versions.
_animationZoomOutSingle: function (cluster, previousZoomLevel, newZoomLevel) {
var bounds = this._getExpandedVisibleBounds(),
minZoom = Math.floor(this._map.getMinZoom());
//Animate all of the markers in the clusters to move to their cluster center point
cluster._recursivelyAnimateChildrenInAndAddSelfToMap(bounds, minZoom, previousZoomLevel + 1, newZoomLevel);
var me = this;
//Update the opacity (If we immediately set it they won't animate)
this._forceLayout();
cluster._recursivelyBecomeVisible(bounds, newZoomLevel);
//TODO: Maybe use the transition timing stuff to make this more reliable
//When the animations are done, tidy up
this._enqueue(function () {
//This cluster stopped being a cluster before the timeout fired
if (cluster._childCount === 1) {
var m = cluster._markers[0];
//If we were in a cluster animation at the time then the opacity and position of our child could be wrong now, so fix it
this._ignoreMove = true;
m.setLatLng(m.getLatLng());
this._ignoreMove = false;
if (m.clusterShow) {
m.clusterShow();
}
} else {
cluster._recursively(bounds, newZoomLevel, minZoom, function (c) {
c._recursivelyRemoveChildrenFromMap(bounds, minZoom, previousZoomLevel + 1);
});
}
me._animationEnd();
});
},
_animationEnd: function () {
if (this._map) {
this._map._mapPane.className = this._map._mapPane.className.replace(' leaflet-cluster-anim', '');
}
this._inZoomAnimation--;
this.fire('animationend');
},
//Force a browser layout of stuff in the map
// Should apply the current opacity and location to all elements so we can update them again for an animation
_forceLayout: function () {
//In my testing this works, infact offsetWidth of any element seems to work.
//Could loop all this._layers and do this for each _icon if it stops working
L.Util.falseFn(document.body.offsetWidth);
}
});
L.markerClusterGroup = function (options) {
return new L.MarkerClusterGroup(options);
};
|