-
Notifications
You must be signed in to change notification settings - Fork 0
/
leaflet.markercluster-src.js
1893 lines (1539 loc) · 56.1 KB
/
leaflet.markercluster-src.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
/*
Copyright (c) 2012, Smartrak, David Leaver
Leaflet.markercluster is an open-source JavaScript library for Marker Clustering on leaflet powered maps.
https://github.com/danzel/Leaflet.markercluster
*/
(function () {
/*
* L.MarkerClusterGroup extends L.FeatureGroup by clustering the markers contained within
*/
L.MarkerClusterGroup = L.FeatureGroup.extend({
options: {
maxClusterRadius: 80, //A cluster will cover at most this many pixels from its center
iconCreateFunction: null,
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,
//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,
//Increase to increase the distance away that spiderfied markers appear from the center
spiderfyDistanceMultiplier: 1,
//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;
}
L.FeatureGroup.prototype.initialize.call(this, []);
this._inZoomAnimation = 0;
this._needsClustering = [];
//The bounds of the currently shown area (from _getExpandedVisibleBounds) Updated on zoom/move
this._currentShownBounds = null;
},
addLayer: function (layer) {
if (layer instanceof L.LayerGroup) {
var array = [];
for (var i in layer._layers) {
array.push(layer._layers[i]);
}
return this.addLayers(array);
}
if (!this._map) {
this._needsClustering.push(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);
//Work out what is visible
var visibleLayer = layer,
currentZoom = this._map.getZoom();
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 (!this._map) {
this._arraySplice(this._needsClustering, 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);
if (layer._icon) {
L.FeatureGroup.prototype.removeLayer.call(this, layer);
layer.setOpacity(1);
}
return this;
},
//Takes an array of markers and adds them in bulk
addLayers: function (layersArray) {
var i, l, m;
if (!this._map) {
this._needsClustering = this._needsClustering.concat(layersArray);
return this;
}
for (i = 0, l = layersArray.length; i < l; i++) {
m = layersArray[i];
if (this.hasLayer(m)) {
continue;
}
this._addLayer(m, this._maxZoom);
//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];
L.FeatureGroup.prototype.removeLayer.call(this, otherMarker);
}
}
}
//Update the icons of all those visible clusters that were affected
for (i in this._layers) {
m = this._layers[i];
if (m instanceof L.MarkerCluster && m._iconNeedsUpdate) {
m._updateIcon();
}
}
this._topClusterLevel._recursivelyAddChildrenToMap(null, this._zoom, this._currentShownBounds);
return this;
},
//Takes an array of markers and removes them in bulk
removeLayers: function (layersArray) {
var i, l, m;
if (!this._map) {
for (i = 0, l = layersArray.length; i < l; i++) {
this._arraySplice(this._needsClustering, layersArray[i]);
}
return this;
}
for (i = 0, l = layersArray.length; i < l; i++) {
m = layersArray[i];
if (!m.__parent) {
continue;
}
this._removeLayer(m, true, true);
if (m._icon) {
L.FeatureGroup.prototype.removeLayer.call(this, m);
m.setOpacity(1);
}
}
//Fix up the clusters and markers on the map
this._topClusterLevel._recursivelyAddChildrenToMap(null, this._zoom, this._currentShownBounds);
for (i in this._layers) {
m = this._layers[i];
if (m instanceof L.MarkerCluster) {
m._updateIcon();
}
}
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 = [];
delete this._gridClusters;
delete this._gridUnclustered;
}
if (this._noanimationUnspiderfy) {
this._noanimationUnspiderfy();
}
//Remove all the visible layers
for (var i in this._layers) {
L.FeatureGroup.prototype.removeLayer.call(this, this._layers[i]);
}
this.eachLayer(function (marker) {
delete marker.__parent;
});
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);
} else {
for (var i = this._needsClustering.length - 1; i >= 0; i--) {
bounds.extend(this._needsClustering[i].getLatLng());
}
}
return bounds;
},
//Overrides LayerGroup.eachLayer
eachLayer: function (method, context) {
var markers = this._needsClustering.slice(),
i;
if (this._topClusterLevel) {
this._topClusterLevel.getAllChildMarkers(markers);
}
for (i = markers.length - 1; i >= 0; i--) {
method.call(context, markers[i]);
}
},
//Returns true if the given layer is in this MarkerClusterGroup
hasLayer: function (layer) {
if (layer._noHas) {
return false;
}
if (this._needsClustering.length > 0) {
var anArray = this._needsClustering;
for (var i = anArray.length - 1; i >= 0; i--) {
if (anArray[i] === layer) {
return true;
}
}
}
return !!(layer.__parent && layer.__parent._group === this);
},
//Zoom down to show the given layer (spiderfying if necessary) then calls the callback
zoomToShowLayer: function (layer, callback) {
var showMarker = function () {
if ((layer._icon || layer.__parent._icon) && !this._inZoomAnimation) {
this._map.off('moveend', showMarker, this);
this.off('animationend', showMarker, this);
if (layer._icon) {
callback();
} else if (layer.__parent._icon) {
var afterSpiderfy = function () {
this.off('spiderfied', afterSpiderfy, this);
callback();
};
this.on('spiderfied', afterSpiderfy, this);
layer.__parent.spiderfy();
}
}
};
if (layer._icon) {
callback();
} else if (layer.__parent._zoom < this._map.getZoom()) {
//Layer should be visible now but isn't on screen, just pan over to it
this._map.on('moveend', showMarker, this);
if (!layer._icon) {
this._map.panTo(layer.getLatLng());
}
} else {
this._map.on('moveend', showMarker, this);
this.on('animationend', showMarker, this);
this._map.setView(layer.getLatLng(), layer.__parent._zoom + 1);
layer.__parent.zoomToBounds();
}
},
//Overrides FeatureGroup.onAdd
onAdd: function (map) {
this._map = map;
if (!this._gridClusters) {
this._generateInitialClusters();
}
for (var i = 0, l = this._needsClustering.length; i < l; i++) {
var layer = this._needsClustering[i];
if (layer.__parent) {
continue;
}
this._addLayer(layer, this._maxZoom);
}
this._needsClustering = [];
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:
//Remember the current zoom level and bounds
this._zoom = this._map.getZoom();
this._currentShownBounds = this._getExpandedVisibleBounds();
//Make things appear on the map
this._topClusterLevel._recursivelyAddChildrenToMap(null, this._zoom, this._currentShownBounds);
},
//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();
}
//Clean up all the layers we added to the map
for (var i in this._layers) {
L.FeatureGroup.prototype.removeLayer.call(this, this._layers[i]);
}
this._map = 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;
}
}
},
//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,
map = this._map;
//Remove the marker from distance clusters it might be in
if (removeFromDistanceGrid) {
for (var z = this._maxZoom; z >= 0; z--) {
if (!gridUnclustered[z].removeObject(marker, map.project(marker.getLatLng(), z))) {
break;
}
}
}
//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--;
if (cluster._zoom < 0) {
//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
L.FeatureGroup.prototype.removeLayer.call(this, cluster);
if (!dontUpdateMap) {
L.FeatureGroup.prototype.addLayer.call(this, otherMarker);
}
}
} else {
cluster._recalculateBounds();
if (!dontUpdateMap || !cluster._icon) {
cluster._updateIcon();
}
}
cluster = cluster.__parent;
}
delete marker.__parent;
},
//Overrides FeatureGroup._propagateEvent
_propagateEvent: function (e) {
if (e.target instanceof L.MarkerCluster) {
e.type = 'cluster' + e.type;
}
L.FeatureGroup.prototype._propagateEvent.call(this, e);
},
//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 shownPolygon = null,
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', function (a) {
if (map.getMaxZoom() === map.getZoom()) {
if (spiderfyOnMaxZoom) {
a.layer.spiderfy();
}
} else if (zoomToBoundsOnClick) {
a.layer.zoomToBounds();
}
}, this);
}
//Show convex hull (boundary) polygon on mouse over
if (showCoverageOnHover) {
this.on('clustermouseover', function (a) {
if (this._inZoomAnimation) {
return;
}
if (shownPolygon) {
map.removeLayer(shownPolygon);
}
if (a.layer.getChildCount() > 2 && a.layer !== this._spiderfied) {
shownPolygon = new L.Polygon(a.layer.getConvexHull(), this.options.polygonOptions);
map.addLayer(shownPolygon);
}
}, this);
this.on('clustermouseout', function () {
if (shownPolygon) {
map.removeLayer(shownPolygon);
shownPolygon = null;
}
}, this);
map.on('zoomend', function () {
if (shownPolygon) {
map.removeLayer(shownPolygon);
shownPolygon = null;
}
}, this);
map.on('layerremove', function (opt) {
if (shownPolygon && opt.layer === this) {
map.removeLayer(shownPolygon);
shownPolygon = null;
}
}, this);
}
},
_unbindEvents: function () {
var spiderfyOnMaxZoom = this.options.spiderfyOnMaxZoom,
showCoverageOnHover = this.options.showCoverageOnHover,
zoomToBoundsOnClick = this.options.zoomToBoundsOnClick,
map = this._map;
if (spiderfyOnMaxZoom || zoomToBoundsOnClick) {
this.off('clusterclick', null, this);
}
if (showCoverageOnHover) {
this.off('clustermouseover', null, this);
this.off('clustermouseout', null, this);
map.off('zoomend', null, this);
map.off('layerremove', null, this);
}
},
_zoomEnd: function () {
if (!this._map) { //May have been removed from the map by a zoomEnd handler
return;
}
this._mergeSplitClusters();
this._zoom = this._map._zoom;
this._currentShownBounds = this._getExpandedVisibleBounds();
},
_moveEnd: function () {
if (this._inZoomAnimation) {
return;
}
var newBounds = this._getExpandedVisibleBounds();
this._topClusterLevel._recursivelyRemoveChildrenFromMap(this._currentShownBounds, this._zoom, newBounds);
this._topClusterLevel._recursivelyAddChildrenToMap(null, this._zoom, newBounds);
this._currentShownBounds = newBounds;
return;
},
_generateInitialClusters: function () {
var maxZoom = this._map.getMaxZoom(),
radius = this.options.maxClusterRadius;
if (this.options.disableClusteringAtZoom) {
maxZoom = this.options.disableClusteringAtZoom - 1;
}
this._maxZoom = maxZoom;
this._gridClusters = {};
this._gridUnclustered = {};
//Set up DistanceGrids for each zoom
for (var zoom = maxZoom; zoom >= 0; zoom--) {
this._gridClusters[zoom] = new L.DistanceGrid(radius);
this._gridUnclustered[zoom] = new L.DistanceGrid(radius);
}
this._topClusterLevel = new L.MarkerCluster(this, -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,
markerPoint, z;
if (this.options.singleMarkerMode) {
layer.options.icon = this.options.iconCreateFunction({
getChildCount: function () {
return 1;
},
getAllChildMarkers: function () {
return [layer];
}
});
}
//Find the lowest zoom level to slot this one in
for (; zoom >= 0; 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 L.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 L.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
for (z = zoom; z >= 0; z--) {
if (!gridUnclustered[z].removeObject(closest, this._map.project(closest.getLatLng(), z))) {
break;
}
}
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;
},
//Merge and split any existing clusters that are too big or small
_mergeSplitClusters: function () {
if (this._zoom < this._map._zoom) { //Zoom in, split
this._animationStart();
//Remove clusters now off screen
this._topClusterLevel._recursivelyRemoveChildrenFromMap(this._currentShownBounds, this._zoom, this._getExpandedVisibleBounds());
this._animationZoomIn(this._zoom, this._map._zoom);
} else if (this._zoom > this._map._zoom) { //Zoom out, merge
this._animationStart();
this._animationZoomOut(this._zoom, this._map._zoom);
} 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.getBounds();
}
var map = this._map,
bounds = map.getBounds(),
sw = bounds._southWest,
ne = bounds._northEast,
latDiff = L.Browser.mobile ? 0 : Math.abs(sw.lat - ne.lat),
lngDiff = L.Browser.mobile ? 0 : Math.abs(sw.lng - ne.lng);
return new L.LatLngBounds(
new L.LatLng(sw.lat - latDiff, sw.lng - lngDiff, true),
new L.LatLng(ne.lat + latDiff, ne.lng + lngDiff, true));
},
//Shared animation code
_animationAddLayerNonAnimated: function (layer, newCluster) {
if (newCluster === layer) {
L.FeatureGroup.prototype.addLayer.call(this, layer);
} else if (newCluster._childCount === 2) {
newCluster._addToMap();
var markers = newCluster.getAllChildMarkers();
L.FeatureGroup.prototype.removeLayer.call(this, markers[0]);
L.FeatureGroup.prototype.removeLayer.call(this, markers[1]);
} else {
newCluster._updateIcon();
}
}
});
L.MarkerClusterGroup.include(!L.DomUtil.TRANSITION ? {
//Non Animated versions of everything
_animationStart: function () {
//Do nothing...
},
_animationZoomIn: function (previousZoomLevel, newZoomLevel) {
this._topClusterLevel._recursivelyRemoveChildrenFromMap(this._currentShownBounds, previousZoomLevel);
this._topClusterLevel._recursivelyAddChildrenToMap(null, newZoomLevel, this._getExpandedVisibleBounds());
},
_animationZoomOut: function (previousZoomLevel, newZoomLevel) {
this._topClusterLevel._recursivelyRemoveChildrenFromMap(this._currentShownBounds, previousZoomLevel);
this._topClusterLevel._recursivelyAddChildrenToMap(null, newZoomLevel, this._getExpandedVisibleBounds());
},
_animationAddLayer: function (layer, newCluster) {
this._animationAddLayerNonAnimated(layer, newCluster);
}
} : {
//Animated versions here
_animationStart: function () {
this._map._mapPane.className += ' leaflet-cluster-anim';
this._inZoomAnimation++;
},
_animationEnd: function () {
if (this._map) {
this._map._mapPane.className = this._map._mapPane.className.replace(' leaflet-cluster-anim', '');
}
this._inZoomAnimation--;
this.fire('animationend');
},
_animationZoomIn: function (previousZoomLevel, newZoomLevel) {
var me = this,
bounds = this._getExpandedVisibleBounds(),
i;
//Add all children of current clusters to map and remove those clusters from map
this._topClusterLevel._recursively(bounds, previousZoomLevel, 0, function (c) {
var startPos = c._latlng,
markers = c._markers,
m;
if (c._isSingleParent() && previousZoomLevel + 1 === newZoomLevel) { //Immediately add the new child and remove us
L.FeatureGroup.prototype.removeLayer.call(me, c);
c._recursivelyAddChildrenToMap(null, newZoomLevel, bounds);
} else {
//Fade out old cluster
c.setOpacity(0);
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)) {
L.FeatureGroup.prototype.removeLayer.call(me, m);
}
}
});
this._forceLayout();
var j, n;
//Update opacities
me._topClusterLevel._recursivelyBecomeVisible(bounds, newZoomLevel);
//TODO Maybe? Update markers in _recursivelyBecomeVisible
for (j in me._layers) {
n = me._layers[j];
if (!(n instanceof L.MarkerCluster) && n._icon) {
n.setOpacity(1);
}
}
//update the positions of the just added clusters/markers
me._topClusterLevel._recursively(bounds, previousZoomLevel, newZoomLevel, function (c) {
c._recursivelyRestoreChildPositions(newZoomLevel);
});
//Remove the old clusters and close the zoom animation
setTimeout(function () {
//update the positions of the just added clusters/markers
me._topClusterLevel._recursively(bounds, previousZoomLevel, 0, function (c) {
L.FeatureGroup.prototype.removeLayer.call(me, c);
c.setOpacity(1);
});
me._animationEnd();
}, 200);
},
_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, previousZoomLevel, this._getExpandedVisibleBounds());
},
_animationZoomOutSingle: function (cluster, previousZoomLevel, newZoomLevel) {
var bounds = this._getExpandedVisibleBounds();
//Animate all of the markers in the clusters to move to their cluster center point
cluster._recursivelyAnimateChildrenInAndAddSelfToMap(bounds, 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
setTimeout(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
m.setLatLng(m.getLatLng());
m.setOpacity(1);
} else {
cluster._recursively(bounds, newZoomLevel, 0, function (c) {
c._recursivelyRemoveChildrenFromMap(bounds, previousZoomLevel + 1);
});
}
me._animationEnd();
}, 200);
},
_animationAddLayer: function (layer, newCluster) {
var me = this;
L.FeatureGroup.prototype.addLayer.call(this, 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.setOpacity(0);
setTimeout(function () {
L.FeatureGroup.prototype.removeLayer.call(me, layer);
layer.setOpacity(1);
me._animationEnd();
}, 200);
} else { //Just became a cluster
this._forceLayout();
me._animationStart();
me._animationZoomOutSingle(newCluster, this._map.getMaxZoom(), this._map.getZoom());
}
}
},
//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.MarkerCluster = L.Marker.extend({
initialize: function (group, zoom, a, b) {
L.Marker.prototype.initialize.call(this, a ? (a._cLatLng || a.getLatLng()) : new L.LatLng(0, 0), { icon: this });
this._group = group;
this._zoom = zoom;
this._markers = [];
this._childClusters = [];
this._childCount = 0;
this._iconNeedsUpdate = true;
this._bounds = new L.LatLngBounds();
if (a) {
this._addChild(a);
}
if (b) {
this._addChild(b);
}
},
//Recursively retrieve all child markers of this cluster
getAllChildMarkers: function (storageArray) {
storageArray = storageArray || [];
for (var i = this._childClusters.length - 1; i >= 0; i--) {
this._childClusters[i].getAllChildMarkers(storageArray);
}
for (var j = this._markers.length - 1; j >= 0; j--) {
storageArray.push(this._markers[j]);
}
return storageArray;
},
//Returns the count of how many child markers we have
getChildCount: function () {
return this._childCount;
},
//Zoom to the extents of this cluster
zoomToBounds: function () {
this._group._map.fitBounds(this._bounds);
},
getBounds: function () {
var bounds = new L.LatLngBounds();
bounds.extend(this._bounds);
return bounds;
},
_updateIcon: function () {
this._iconNeedsUpdate = true;
if (this._icon) {
this.setIcon(this);
}
},
//Cludge for Icon, we pretend to be an icon for performance
createIcon: function () {
if (this._iconNeedsUpdate) {
this._iconObj = this._group.options.iconCreateFunction(this);
this._iconNeedsUpdate = false;
}
return this._iconObj.createIcon();
},
createShadow: function () {
return this._iconObj.createShadow();
},
_addChild: function (new1, isNotificationFromChild) {
this._iconNeedsUpdate = true;
this._expandBounds(new1);
if (new1 instanceof L.MarkerCluster) {
if (!isNotificationFromChild) {
this._childClusters.push(new1);
new1.__parent = this;
}
this._childCount += new1._childCount;
} else {
if (!isNotificationFromChild) {
this._markers.push(new1);
}
this._childCount++;
}
if (this.__parent) {
this.__parent._addChild(new1, true);
}
},
//Expand our bounds and tell our parent to
_expandBounds: function (marker) {
var addedCount,
addedLatLng = marker._wLatLng || marker._latlng;
if (marker instanceof L.MarkerCluster) {
this._bounds.extend(marker._bounds);
addedCount = marker._childCount;
} else {
this._bounds.extend(addedLatLng);