leaflet-markercluster-rails 0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Stephen Pike
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,28 @@
1
+ # Leaflet.MarkerCluster for Rails
2
+
3
+ Engine wrapper for the Leaflet MarkerCluster library by @danzel.
4
+
5
+ https://github.com/danzel/Leaflet.markercluster
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'leaflet-markercluster-rails'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install leaflet-markercluster-rails
20
+
21
+ ## Usage
22
+
23
+ Provides the follwoing:
24
+
25
+ leaflet.markercluster.js
26
+ leaflet.markercluster.css
27
+ leaflet.markercluster.default.css
28
+ leaflet.markercluster.ie.css
@@ -0,0 +1,10 @@
1
+ require "leaflet-markercluster-rails/version"
2
+
3
+ module Leaflet
4
+ module Markercluster
5
+ module Rails
6
+ class Engine < ::Rails::Engine
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,7 @@
1
+ module Leaflet
2
+ module Markercluster
3
+ module Rails
4
+ VERSION = "0.2"
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,1874 @@
1
+ /*
2
+ Copyright (c) 2012, Smartrak, David Leaver
3
+ Leaflet.markercluster is an open-source JavaScript library for Marker Clustering on leaflet powered maps.
4
+ https://github.com/danzel/Leaflet.markercluster
5
+ */
6
+ (function (window, undefined) {
7
+
8
+
9
+ /*
10
+ * L.MarkerClusterGroup extends L.FeatureGroup by clustering the markers contained within
11
+ */
12
+
13
+ L.MarkerClusterGroup = L.FeatureGroup.extend({
14
+
15
+ options: {
16
+ maxClusterRadius: 80, //A cluster will cover at most this many pixels from its center
17
+ iconCreateFunction: null,
18
+
19
+ spiderfyOnMaxZoom: true,
20
+ showCoverageOnHover: true,
21
+ zoomToBoundsOnClick: true,
22
+ singleMarkerMode: false,
23
+
24
+ disableClusteringAtZoom: null,
25
+
26
+ //Whether to animate adding markers after adding the MarkerClusterGroup to the map
27
+ // If you are adding individual markers set to true, if adding bulk markers leave false for massive performance gains.
28
+ animateAddingMarkers: false,
29
+
30
+ //Increase to increase the distance away that spiderfied markers appear from the center
31
+ spiderfyDistanceMultiplier: 1,
32
+
33
+ //Options to pass to the L.Polygon constructor
34
+ polygonOptions: {}
35
+ },
36
+
37
+ initialize: function (options) {
38
+ L.Util.setOptions(this, options);
39
+ if (!this.options.iconCreateFunction) {
40
+ this.options.iconCreateFunction = this._defaultIconCreateFunction;
41
+ }
42
+
43
+ L.FeatureGroup.prototype.initialize.call(this, []);
44
+
45
+ this._inZoomAnimation = 0;
46
+ this._needsClustering = [];
47
+ //The bounds of the currently shown area (from _getExpandedVisibleBounds) Updated on zoom/move
48
+ this._currentShownBounds = null;
49
+ },
50
+
51
+ addLayer: function (layer) {
52
+
53
+ if (layer instanceof L.LayerGroup) {
54
+ var array = [];
55
+ for (var i in layer._layers) {
56
+ if (layer._layers.hasOwnProperty(i)) {
57
+ array.push(layer._layers[i]);
58
+ }
59
+ }
60
+ return this.addLayers(array);
61
+ }
62
+
63
+ if (!this._map) {
64
+ this._needsClustering.push(layer);
65
+ return this;
66
+ }
67
+
68
+ if (this.hasLayer(layer)) {
69
+ return this;
70
+ }
71
+
72
+ //If we have already clustered we'll need to add this one to a cluster
73
+
74
+ if (this._unspiderfy) {
75
+ this._unspiderfy();
76
+ }
77
+
78
+ this._addLayer(layer, this._maxZoom);
79
+
80
+ //Work out what is visible
81
+ var visibleLayer = layer,
82
+ currentZoom = this._map.getZoom();
83
+ if (layer.__parent) {
84
+ while (visibleLayer.__parent._zoom >= currentZoom) {
85
+ visibleLayer = visibleLayer.__parent;
86
+ }
87
+ }
88
+
89
+ if (this._currentShownBounds.contains(visibleLayer.getLatLng())) {
90
+ if (this.options.animateAddingMarkers) {
91
+ this._animationAddLayer(layer, visibleLayer);
92
+ } else {
93
+ this._animationAddLayerNonAnimated(layer, visibleLayer);
94
+ }
95
+ }
96
+ return this;
97
+ },
98
+
99
+ removeLayer: function (layer) {
100
+
101
+ if (!this._map) {
102
+ this._arraySplice(this._needsClustering, layer);
103
+ return this;
104
+ }
105
+
106
+ if (!layer.__parent) {
107
+ return this;
108
+ }
109
+
110
+ if (this._unspiderfy) {
111
+ this._unspiderfy();
112
+ this._unspiderfyLayer(layer);
113
+ }
114
+
115
+ //Remove the marker from clusters
116
+ this._removeLayer(layer, true);
117
+
118
+ if (layer._icon) {
119
+ L.FeatureGroup.prototype.removeLayer.call(this, layer);
120
+ layer.setOpacity(1);
121
+ }
122
+
123
+ return this;
124
+ },
125
+
126
+ //Takes an array of markers and adds them in bulk
127
+ addLayers: function (layersArray) {
128
+ var i, l, m;
129
+ if (!this._map) {
130
+ this._needsClustering = this._needsClustering.concat(layersArray);
131
+ return this;
132
+ }
133
+
134
+ for (i = 0, l = layersArray.length; i < l; i++) {
135
+ m = layersArray[i];
136
+
137
+ if (this.hasLayer(m)) {
138
+ continue;
139
+ }
140
+
141
+ this._addLayer(m, this._maxZoom);
142
+
143
+ //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
144
+ if (m.__parent) {
145
+ if (m.__parent.getChildCount() === 2) {
146
+ var markers = m.__parent.getAllChildMarkers(),
147
+ otherMarker = markers[0] === m ? markers[1] : markers[0];
148
+ L.FeatureGroup.prototype.removeLayer.call(this, otherMarker);
149
+ }
150
+ }
151
+ }
152
+
153
+ //Update the icons of all those visible clusters that were affected
154
+ for (i in this._layers) {
155
+ if (this._layers.hasOwnProperty(i)) {
156
+ m = this._layers[i];
157
+ if (m instanceof L.MarkerCluster && m._iconNeedsUpdate) {
158
+ m._updateIcon();
159
+ }
160
+ }
161
+ }
162
+
163
+ this._topClusterLevel._recursivelyAddChildrenToMap(null, this._zoom, this._currentShownBounds);
164
+
165
+ return this;
166
+ },
167
+
168
+ //Takes an array of markers and removes them in bulk
169
+ removeLayers: function (layersArray) {
170
+ var i, l, m;
171
+
172
+ if (!this._map) {
173
+ for (i = 0, l = layersArray.length; i < l; i++) {
174
+ this._arraySplice(this._needsClustering, layersArray[i]);
175
+ }
176
+ return this;
177
+ }
178
+
179
+ for (i = 0, l = layersArray.length; i < l; i++) {
180
+ m = layersArray[i];
181
+
182
+ if (!m.__parent) {
183
+ continue;
184
+ }
185
+
186
+ this._removeLayer(m, true, true);
187
+
188
+ if (m._icon) {
189
+ L.FeatureGroup.prototype.removeLayer.call(this, m);
190
+ m.setOpacity(1);
191
+ }
192
+ }
193
+
194
+ //Fix up the clusters and markers on the map
195
+ this._topClusterLevel._recursivelyAddChildrenToMap(null, this._zoom, this._currentShownBounds);
196
+
197
+ for (i in this._layers) {
198
+ if (this._layers.hasOwnProperty(i)) {
199
+ m = this._layers[i];
200
+ if (m instanceof L.MarkerCluster) {
201
+ m._updateIcon();
202
+ }
203
+ }
204
+ }
205
+
206
+ return this;
207
+ },
208
+
209
+ //Removes all layers from the MarkerClusterGroup
210
+ clearLayers: function () {
211
+ //Need our own special implementation as the LayerGroup one doesn't work for us
212
+
213
+ //If we aren't on the map (yet), blow away the markers we know of
214
+ if (!this._map) {
215
+ this._needsClustering = [];
216
+ delete this._gridClusters;
217
+ delete this._gridUnclustered;
218
+ }
219
+
220
+ if (this._unspiderfy) {
221
+ this._unspiderfy();
222
+ }
223
+
224
+ //Remove all the visible layers
225
+ for (var i in this._layers) {
226
+ if (this._layers.hasOwnProperty(i)) {
227
+ L.FeatureGroup.prototype.removeLayer.call(this, this._layers[i]);
228
+ }
229
+ }
230
+
231
+ this.eachLayer(function (marker) {
232
+ delete marker.__parent;
233
+ });
234
+
235
+ if (this._map) {
236
+ //Reset _topClusterLevel and the DistanceGrids
237
+ this._generateInitialClusters();
238
+ }
239
+
240
+ return this;
241
+ },
242
+
243
+ //Override FeatureGroup.getBounds as it doesn't work
244
+ getBounds: function () {
245
+ var bounds = new L.LatLngBounds();
246
+ if (this._topClusterLevel) {
247
+ bounds.extend(this._topClusterLevel._bounds);
248
+ } else {
249
+ for (var i = this._needsClustering.length - 1; i >= 0; i--) {
250
+ bounds.extend(this._needsClustering[i].getLatLng());
251
+ }
252
+ }
253
+ return bounds;
254
+ },
255
+
256
+ //Overrides LayerGroup.eachLayer
257
+ eachLayer: function (method, context) {
258
+ var markers = this._needsClustering.slice(),
259
+ i;
260
+
261
+ if (this._topClusterLevel) {
262
+ this._topClusterLevel.getAllChildMarkers(markers);
263
+ }
264
+
265
+ for (i = markers.length - 1; i >= 0; i--) {
266
+ method.call(context, markers[i]);
267
+ }
268
+ },
269
+
270
+ //Returns true if the given layer is in this MarkerClusterGroup
271
+ hasLayer: function (layer) {
272
+ if (this._needsClustering.length > 0) {
273
+ var anArray = this._needsClustering;
274
+ for (var i = anArray.length - 1; i >= 0; i--) {
275
+ if (anArray[i] === layer) {
276
+ return true;
277
+ }
278
+ }
279
+ }
280
+
281
+ return !!(layer.__parent && layer.__parent._group === this);
282
+ },
283
+
284
+ //Zoom down to show the given layer (spiderfying if necessary) then calls the callback
285
+ zoomToShowLayer: function (layer, callback) {
286
+
287
+ var showMarker = function () {
288
+ if ((layer._icon || layer.__parent._icon) && !this._inZoomAnimation) {
289
+ this._map.off('moveend', showMarker, this);
290
+ this.off('animationend', showMarker, this);
291
+
292
+ if (layer._icon) {
293
+ callback();
294
+ } else if (layer.__parent._icon) {
295
+ var afterSpiderfy = function () {
296
+ this.off('spiderfied', afterSpiderfy, this);
297
+ callback();
298
+ };
299
+
300
+ this.on('spiderfied', afterSpiderfy, this);
301
+ layer.__parent.spiderfy();
302
+ }
303
+ }
304
+ };
305
+
306
+ if (layer._icon) {
307
+ callback();
308
+ } else if (layer.__parent._zoom < this._map.getZoom()) {
309
+ //Layer should be visible now but isn't on screen, just pan over to it
310
+ this._map.on('moveend', showMarker, this);
311
+ if (!layer._icon) {
312
+ this._map.panTo(layer.getLatLng());
313
+ }
314
+ } else {
315
+ this._map.on('moveend', showMarker, this);
316
+ this.on('animationend', showMarker, this);
317
+ this._map.setView(layer.getLatLng(), layer.__parent._zoom + 1);
318
+ layer.__parent.zoomToBounds();
319
+ }
320
+ },
321
+
322
+ //Overrides FeatureGroup.onAdd
323
+ onAdd: function (map) {
324
+ this._map = map;
325
+
326
+ if (!this._gridClusters) {
327
+ this._generateInitialClusters();
328
+ }
329
+
330
+ for (var i = 0, l = this._needsClustering.length; i < l; i++) {
331
+ var layer = this._needsClustering[i];
332
+ if (layer.__parent) {
333
+ continue;
334
+ }
335
+ this._addLayer(layer, this._maxZoom);
336
+ }
337
+ this._needsClustering = [];
338
+
339
+ this._map.on('zoomend', this._zoomEnd, this);
340
+ this._map.on('moveend', this._moveEnd, this);
341
+
342
+ if (this._spiderfierOnAdd) { //TODO FIXME: Not sure how to have spiderfier add something on here nicely
343
+ this._spiderfierOnAdd();
344
+ }
345
+
346
+ this._bindEvents();
347
+
348
+
349
+ //Actually add our markers to the map:
350
+
351
+ //Remember the current zoom level and bounds
352
+ this._zoom = this._map.getZoom();
353
+ this._currentShownBounds = this._getExpandedVisibleBounds();
354
+
355
+ //Make things appear on the map
356
+ this._topClusterLevel._recursivelyAddChildrenToMap(null, this._zoom, this._currentShownBounds);
357
+ },
358
+
359
+ //Overrides FeatureGroup.onRemove
360
+ onRemove: function (map) {
361
+ this._map.off('zoomend', this._zoomEnd, this);
362
+ this._map.off('moveend', this._moveEnd, this);
363
+
364
+ this._unbindEvents();
365
+
366
+ //In case we are in a cluster animation
367
+ this._map._mapPane.className = this._map._mapPane.className.replace(' leaflet-cluster-anim', '');
368
+
369
+ if (this._spiderfierOnRemove) { //TODO FIXME: Not sure how to have spiderfier add something on here nicely
370
+ this._spiderfierOnRemove();
371
+ }
372
+
373
+ //Clean up all the layers we added to the map
374
+ for (var i in this._layers) {
375
+ if (this._layers.hasOwnProperty(i)) {
376
+ L.FeatureGroup.prototype.removeLayer.call(this, this._layers[i]);
377
+ }
378
+ }
379
+
380
+ this._map = null;
381
+ },
382
+
383
+
384
+ //Remove the given object from the given array
385
+ _arraySplice: function (anArray, obj) {
386
+ for (var i = anArray.length - 1; i >= 0; i--) {
387
+ if (anArray[i] === obj) {
388
+ anArray.splice(i, 1);
389
+ return;
390
+ }
391
+ }
392
+ },
393
+
394
+ //Internal function for removing a marker from everything.
395
+ //dontUpdateMap: set to true if you will handle updating the map manually (for bulk functions)
396
+ _removeLayer: function (marker, removeFromDistanceGrid, dontUpdateMap) {
397
+ var gridClusters = this._gridClusters,
398
+ gridUnclustered = this._gridUnclustered,
399
+ map = this._map;
400
+
401
+ //Remove the marker from distance clusters it might be in
402
+ if (removeFromDistanceGrid) {
403
+ for (var z = this._maxZoom; z >= 0; z--) {
404
+ if (!gridUnclustered[z].removeObject(marker, map.project(marker.getLatLng(), z))) {
405
+ break;
406
+ }
407
+ }
408
+ }
409
+
410
+ //Work our way up the clusters removing them as we go if required
411
+ var cluster = marker.__parent,
412
+ markers = cluster._markers,
413
+ otherMarker;
414
+
415
+ //Remove the marker from the immediate parents marker list
416
+ this._arraySplice(markers, marker);
417
+
418
+ while (cluster) {
419
+ cluster._childCount--;
420
+
421
+ if (cluster._zoom < 0) {
422
+ //Top level, do nothing
423
+ break;
424
+ } else if (removeFromDistanceGrid && cluster._childCount <= 1) { //Cluster no longer required
425
+ //We need to push the other marker up to the parent
426
+ otherMarker = cluster._markers[0] === marker ? cluster._markers[1] : cluster._markers[0];
427
+
428
+ //Update distance grid
429
+ gridClusters[cluster._zoom].removeObject(cluster, map.project(cluster._cLatLng, cluster._zoom));
430
+ gridUnclustered[cluster._zoom].addObject(otherMarker, map.project(otherMarker.getLatLng(), cluster._zoom));
431
+
432
+ //Move otherMarker up to parent
433
+ this._arraySplice(cluster.__parent._childClusters, cluster);
434
+ cluster.__parent._markers.push(otherMarker);
435
+ otherMarker.__parent = cluster.__parent;
436
+
437
+ if (cluster._icon) {
438
+ //Cluster is currently on the map, need to put the marker on the map instead
439
+ L.FeatureGroup.prototype.removeLayer.call(this, cluster);
440
+ if (!dontUpdateMap) {
441
+ L.FeatureGroup.prototype.addLayer.call(this, otherMarker);
442
+ }
443
+ }
444
+ } else {
445
+ cluster._recalculateBounds();
446
+ if (!dontUpdateMap || !cluster._icon) {
447
+ cluster._updateIcon();
448
+ }
449
+ }
450
+
451
+ cluster = cluster.__parent;
452
+ }
453
+
454
+ delete marker.__parent;
455
+ },
456
+
457
+ //Overrides FeatureGroup._propagateEvent
458
+ _propagateEvent: function (e) {
459
+ if (e.target instanceof L.MarkerCluster) {
460
+ e.type = 'cluster' + e.type;
461
+ }
462
+ L.FeatureGroup.prototype._propagateEvent.call(this, e);
463
+ },
464
+
465
+ //Default functionality
466
+ _defaultIconCreateFunction: function (cluster) {
467
+ var childCount = cluster.getChildCount();
468
+
469
+ var c = ' marker-cluster-';
470
+ if (childCount < 10) {
471
+ c += 'small';
472
+ } else if (childCount < 100) {
473
+ c += 'medium';
474
+ } else {
475
+ c += 'large';
476
+ }
477
+
478
+ return new L.DivIcon({ html: '<div><span>' + childCount + '</span></div>', className: 'marker-cluster' + c, iconSize: new L.Point(40, 40) });
479
+ },
480
+
481
+ _bindEvents: function () {
482
+ var shownPolygon = null,
483
+ map = this._map,
484
+
485
+ spiderfyOnMaxZoom = this.options.spiderfyOnMaxZoom,
486
+ showCoverageOnHover = this.options.showCoverageOnHover,
487
+ zoomToBoundsOnClick = this.options.zoomToBoundsOnClick;
488
+
489
+ //Zoom on cluster click or spiderfy if we are at the lowest level
490
+ if (spiderfyOnMaxZoom || zoomToBoundsOnClick) {
491
+ this.on('clusterclick', function (a) {
492
+ if (map.getMaxZoom() === map.getZoom()) {
493
+ if (spiderfyOnMaxZoom) {
494
+ a.layer.spiderfy();
495
+ }
496
+ } else if (zoomToBoundsOnClick) {
497
+ a.layer.zoomToBounds();
498
+ }
499
+ }, this);
500
+ }
501
+
502
+ //Show convex hull (boundary) polygon on mouse over
503
+ if (showCoverageOnHover) {
504
+ this.on('clustermouseover', function (a) {
505
+ if (this._inZoomAnimation) {
506
+ return;
507
+ }
508
+ if (shownPolygon) {
509
+ map.removeLayer(shownPolygon);
510
+ }
511
+ if (a.layer.getChildCount() > 2) {
512
+ shownPolygon = new L.Polygon(a.layer.getConvexHull(), this.options.polygonOptions);
513
+ map.addLayer(shownPolygon);
514
+ }
515
+ }, this);
516
+ this.on('clustermouseout', function () {
517
+ if (shownPolygon) {
518
+ map.removeLayer(shownPolygon);
519
+ shownPolygon = null;
520
+ }
521
+ }, this);
522
+ map.on('zoomend', function () {
523
+ if (shownPolygon) {
524
+ map.removeLayer(shownPolygon);
525
+ shownPolygon = null;
526
+ }
527
+ }, this);
528
+ map.on('layerremove', function (opt) {
529
+ if (shownPolygon && opt.layer === this) {
530
+ map.removeLayer(shownPolygon);
531
+ shownPolygon = null;
532
+ }
533
+ }, this);
534
+ }
535
+ },
536
+
537
+ _unbindEvents: function () {
538
+ var spiderfyOnMaxZoom = this.options.spiderfyOnMaxZoom,
539
+ showCoverageOnHover = this.options.showCoverageOnHover,
540
+ zoomToBoundsOnClick = this.options.zoomToBoundsOnClick,
541
+ map = this._map;
542
+
543
+ if (spiderfyOnMaxZoom || zoomToBoundsOnClick) {
544
+ this.off('clusterclick', null, this);
545
+ }
546
+ if (showCoverageOnHover) {
547
+ this.off('clustermouseover', null, this);
548
+ this.off('clustermouseout', null, this);
549
+ map.off('zoomend', null, this);
550
+ map.off('layerremove', null, this);
551
+ }
552
+ },
553
+
554
+ _zoomEnd: function () {
555
+ if (!this._map) { //May have been removed from the map by a zoomEnd handler
556
+ return;
557
+ }
558
+ this._mergeSplitClusters();
559
+
560
+ this._zoom = this._map._zoom;
561
+ this._currentShownBounds = this._getExpandedVisibleBounds();
562
+ },
563
+
564
+ _moveEnd: function () {
565
+ if (this._inZoomAnimation) {
566
+ return;
567
+ }
568
+
569
+ var newBounds = this._getExpandedVisibleBounds();
570
+
571
+ this._topClusterLevel._recursivelyRemoveChildrenFromMap(this._currentShownBounds, this._zoom, newBounds);
572
+ this._topClusterLevel._recursivelyAddChildrenToMap(null, this._zoom, newBounds);
573
+
574
+ this._currentShownBounds = newBounds;
575
+ return;
576
+ },
577
+
578
+ _generateInitialClusters: function () {
579
+ var maxZoom = this._map.getMaxZoom(),
580
+ radius = this.options.maxClusterRadius;
581
+
582
+ if (this.options.disableClusteringAtZoom) {
583
+ maxZoom = this.options.disableClusteringAtZoom - 1;
584
+ }
585
+ this._maxZoom = maxZoom;
586
+ this._gridClusters = {};
587
+ this._gridUnclustered = {};
588
+
589
+ //Set up DistanceGrids for each zoom
590
+ for (var zoom = maxZoom; zoom >= 0; zoom--) {
591
+ this._gridClusters[zoom] = new L.DistanceGrid(radius);
592
+ this._gridUnclustered[zoom] = new L.DistanceGrid(radius);
593
+ }
594
+
595
+ this._topClusterLevel = new L.MarkerCluster(this, -1);
596
+ },
597
+
598
+ //Zoom: Zoom to start adding at (Pass this._maxZoom to start at the bottom)
599
+ _addLayer: function (layer, zoom) {
600
+ var gridClusters = this._gridClusters,
601
+ gridUnclustered = this._gridUnclustered,
602
+ markerPoint, z;
603
+
604
+ if (this.options.singleMarkerMode) {
605
+ layer.options.icon = this.options.iconCreateFunction({
606
+ getChildCount: function () {
607
+ return 1;
608
+ },
609
+ getAllChildMarkers: function () {
610
+ return [layer];
611
+ }
612
+ });
613
+ }
614
+
615
+ //Find the lowest zoom level to slot this one in
616
+ for (; zoom >= 0; zoom--) {
617
+ markerPoint = this._map.project(layer.getLatLng(), zoom); // calculate pixel position
618
+
619
+ //Try find a cluster close by
620
+ var closest = gridClusters[zoom].getNearObject(markerPoint);
621
+ if (closest) {
622
+ closest._addChild(layer);
623
+ layer.__parent = closest;
624
+ return;
625
+ }
626
+
627
+ //Try find a marker close by to form a new cluster with
628
+ closest = gridUnclustered[zoom].getNearObject(markerPoint);
629
+ if (closest) {
630
+ var parent = closest.__parent;
631
+ if (parent) {
632
+ this._removeLayer(closest, false);
633
+ }
634
+
635
+ //Create new cluster with these 2 in it
636
+
637
+ var newCluster = new L.MarkerCluster(this, zoom, closest, layer);
638
+ gridClusters[zoom].addObject(newCluster, this._map.project(newCluster._cLatLng, zoom));
639
+ closest.__parent = newCluster;
640
+ layer.__parent = newCluster;
641
+
642
+ //First create any new intermediate parent clusters that don't exist
643
+ var lastParent = newCluster;
644
+ for (z = zoom - 1; z > parent._zoom; z--) {
645
+ lastParent = new L.MarkerCluster(this, z, lastParent);
646
+ gridClusters[z].addObject(lastParent, this._map.project(closest.getLatLng(), z));
647
+ }
648
+ parent._addChild(lastParent);
649
+
650
+ //Remove closest from this zoom level and any above that it is in, replace with newCluster
651
+ for (z = zoom; z >= 0; z--) {
652
+ if (!gridUnclustered[z].removeObject(closest, this._map.project(closest.getLatLng(), z))) {
653
+ break;
654
+ }
655
+ }
656
+
657
+ return;
658
+ }
659
+
660
+ //Didn't manage to cluster in at this zoom, record us as a marker here and continue upwards
661
+ gridUnclustered[zoom].addObject(layer, markerPoint);
662
+ }
663
+
664
+ //Didn't get in anything, add us to the top
665
+ this._topClusterLevel._addChild(layer);
666
+ layer.__parent = this._topClusterLevel;
667
+ return;
668
+ },
669
+
670
+ //Merge and split any existing clusters that are too big or small
671
+ _mergeSplitClusters: function () {
672
+ if (this._zoom < this._map._zoom) { //Zoom in, split
673
+ this._animationStart();
674
+ //Remove clusters now off screen
675
+ this._topClusterLevel._recursivelyRemoveChildrenFromMap(this._currentShownBounds, this._zoom, this._getExpandedVisibleBounds());
676
+
677
+ this._animationZoomIn(this._zoom, this._map._zoom);
678
+
679
+ } else if (this._zoom > this._map._zoom) { //Zoom out, merge
680
+ this._animationStart();
681
+
682
+ this._animationZoomOut(this._zoom, this._map._zoom);
683
+ } else {
684
+ this._moveEnd();
685
+ }
686
+ },
687
+
688
+ //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)
689
+ _getExpandedVisibleBounds: function () {
690
+ var map = this._map,
691
+ bounds = map.getBounds(),
692
+ sw = bounds._southWest,
693
+ ne = bounds._northEast,
694
+ latDiff = L.Browser.mobile ? 0 : Math.abs(sw.lat - ne.lat),
695
+ lngDiff = L.Browser.mobile ? 0 : Math.abs(sw.lng - ne.lng);
696
+
697
+ return new L.LatLngBounds(
698
+ new L.LatLng(sw.lat - latDiff, sw.lng - lngDiff, true),
699
+ new L.LatLng(ne.lat + latDiff, ne.lng + lngDiff, true));
700
+ },
701
+
702
+ //Shared animation code
703
+ _animationAddLayerNonAnimated: function (layer, newCluster) {
704
+ if (newCluster === layer) {
705
+ L.FeatureGroup.prototype.addLayer.call(this, layer);
706
+ } else if (newCluster._childCount === 2) {
707
+ newCluster._addToMap();
708
+
709
+ var markers = newCluster.getAllChildMarkers();
710
+ L.FeatureGroup.prototype.removeLayer.call(this, markers[0]);
711
+ L.FeatureGroup.prototype.removeLayer.call(this, markers[1]);
712
+ } else {
713
+ newCluster._updateIcon();
714
+ }
715
+ }
716
+ });
717
+
718
+ L.MarkerClusterGroup.include(!L.DomUtil.TRANSITION ? {
719
+
720
+ //Non Animated versions of everything
721
+ _animationStart: function () {
722
+ //Do nothing...
723
+ },
724
+ _animationZoomIn: function (previousZoomLevel, newZoomLevel) {
725
+ this._topClusterLevel._recursivelyRemoveChildrenFromMap(this._currentShownBounds, previousZoomLevel);
726
+ this._topClusterLevel._recursivelyAddChildrenToMap(null, newZoomLevel, this._getExpandedVisibleBounds());
727
+ },
728
+ _animationZoomOut: function (previousZoomLevel, newZoomLevel) {
729
+ this._topClusterLevel._recursivelyRemoveChildrenFromMap(this._currentShownBounds, previousZoomLevel);
730
+ this._topClusterLevel._recursivelyAddChildrenToMap(null, newZoomLevel, this._getExpandedVisibleBounds());
731
+ },
732
+ _animationAddLayer: function (layer, newCluster) {
733
+ this._animationAddLayerNonAnimated(layer, newCluster);
734
+ }
735
+ } : {
736
+
737
+ //Animated versions here
738
+ _animationStart: function () {
739
+ this._map._mapPane.className += ' leaflet-cluster-anim';
740
+ this._inZoomAnimation++;
741
+ },
742
+ _animationEnd: function () {
743
+ if (this._map) {
744
+ this._map._mapPane.className = this._map._mapPane.className.replace(' leaflet-cluster-anim', '');
745
+ }
746
+ this._inZoomAnimation--;
747
+ this.fire('animationend');
748
+ },
749
+ _animationZoomIn: function (previousZoomLevel, newZoomLevel) {
750
+ var me = this,
751
+ bounds = this._getExpandedVisibleBounds(),
752
+ i;
753
+
754
+ //Add all children of current clusters to map and remove those clusters from map
755
+ this._topClusterLevel._recursively(bounds, previousZoomLevel, 0, function (c) {
756
+ var startPos = c._latlng,
757
+ markers = c._markers,
758
+ m;
759
+
760
+ if (c._isSingleParent() && previousZoomLevel + 1 === newZoomLevel) { //Immediately add the new child and remove us
761
+ L.FeatureGroup.prototype.removeLayer.call(me, c);
762
+ c._recursivelyAddChildrenToMap(null, newZoomLevel, bounds);
763
+ } else {
764
+ //Fade out old cluster
765
+ c.setOpacity(0);
766
+ c._recursivelyAddChildrenToMap(startPos, newZoomLevel, bounds);
767
+ }
768
+
769
+ //Remove all markers that aren't visible any more
770
+ //TODO: Do we actually need to do this on the higher levels too?
771
+ for (i = markers.length - 1; i >= 0; i--) {
772
+ m = markers[i];
773
+ if (!bounds.contains(m._latlng)) {
774
+ L.FeatureGroup.prototype.removeLayer.call(me, m);
775
+ }
776
+ }
777
+
778
+ });
779
+
780
+ this._forceLayout();
781
+ var j, n;
782
+
783
+ //Update opacities
784
+ me._topClusterLevel._recursivelyBecomeVisible(bounds, newZoomLevel);
785
+ //TODO Maybe? Update markers in _recursivelyBecomeVisible
786
+ for (j in me._layers) {
787
+ if (me._layers.hasOwnProperty(j)) {
788
+ n = me._layers[j];
789
+
790
+ if (!(n instanceof L.MarkerCluster) && n._icon) {
791
+ n.setOpacity(1);
792
+ }
793
+ }
794
+ }
795
+
796
+ //update the positions of the just added clusters/markers
797
+ me._topClusterLevel._recursively(bounds, previousZoomLevel, newZoomLevel, function (c) {
798
+ c._recursivelyRestoreChildPositions(newZoomLevel);
799
+ });
800
+
801
+ //Remove the old clusters and close the zoom animation
802
+
803
+ setTimeout(function () {
804
+ //update the positions of the just added clusters/markers
805
+ me._topClusterLevel._recursively(bounds, previousZoomLevel, 0, function (c) {
806
+ L.FeatureGroup.prototype.removeLayer.call(me, c);
807
+ c.setOpacity(1);
808
+ });
809
+
810
+ me._animationEnd();
811
+ }, 250);
812
+ },
813
+
814
+ _animationZoomOut: function (previousZoomLevel, newZoomLevel) {
815
+ this._animationZoomOutSingle(this._topClusterLevel, previousZoomLevel - 1, newZoomLevel);
816
+
817
+ //Need to add markers for those that weren't on the map before but are now
818
+ this._topClusterLevel._recursivelyAddChildrenToMap(null, newZoomLevel, this._getExpandedVisibleBounds());
819
+ //Remove markers that were on the map before but won't be now
820
+ this._topClusterLevel._recursivelyRemoveChildrenFromMap(this._currentShownBounds, previousZoomLevel, this._getExpandedVisibleBounds());
821
+ },
822
+ _animationZoomOutSingle: function (cluster, previousZoomLevel, newZoomLevel) {
823
+ var bounds = this._getExpandedVisibleBounds();
824
+
825
+ //Animate all of the markers in the clusters to move to their cluster center point
826
+ cluster._recursivelyAnimateChildrenInAndAddSelfToMap(bounds, previousZoomLevel + 1, newZoomLevel);
827
+
828
+ var me = this;
829
+
830
+ //Update the opacity (If we immediately set it they won't animate)
831
+ this._forceLayout();
832
+ cluster._recursivelyBecomeVisible(bounds, newZoomLevel);
833
+
834
+ //TODO: Maybe use the transition timing stuff to make this more reliable
835
+ //When the animations are done, tidy up
836
+ setTimeout(function () {
837
+
838
+ //This cluster stopped being a cluster before the timeout fired
839
+ if (cluster._childCount === 1) {
840
+ var m = cluster._markers[0];
841
+ //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
842
+ m.setLatLng(m.getLatLng());
843
+ m.setOpacity(1);
844
+
845
+ return;
846
+ }
847
+
848
+ cluster._recursively(bounds, newZoomLevel, 0, function (c) {
849
+ c._recursivelyRemoveChildrenFromMap(bounds, previousZoomLevel + 1);
850
+ });
851
+ me._animationEnd();
852
+ }, 250);
853
+ },
854
+ _animationAddLayer: function (layer, newCluster) {
855
+ var me = this;
856
+
857
+ L.FeatureGroup.prototype.addLayer.call(this, layer);
858
+ if (newCluster !== layer) {
859
+ if (newCluster._childCount > 2) { //Was already a cluster
860
+
861
+ newCluster._updateIcon();
862
+ this._forceLayout();
863
+ this._animationStart();
864
+
865
+ layer._setPos(this._map.latLngToLayerPoint(newCluster.getLatLng()));
866
+ layer.setOpacity(0);
867
+
868
+ setTimeout(function () {
869
+ L.FeatureGroup.prototype.removeLayer.call(me, layer);
870
+ layer.setOpacity(1);
871
+
872
+ me._animationEnd();
873
+ }, 250);
874
+
875
+ } else { //Just became a cluster
876
+ this._forceLayout();
877
+
878
+ me._animationStart();
879
+ me._animationZoomOutSingle(newCluster, this._map.getMaxZoom(), this._map.getZoom());
880
+ }
881
+ }
882
+ },
883
+
884
+ //Force a browser layout of stuff in the map
885
+ // Should apply the current opacity and location to all elements so we can update them again for an animation
886
+ _forceLayout: function () {
887
+ //In my testing this works, infact offsetWidth of any element seems to work.
888
+ //Could loop all this._layers and do this for each _icon if it stops working
889
+
890
+ L.Util.falseFn(document.body.offsetWidth);
891
+ }
892
+ });
893
+
894
+
895
+ L.MarkerCluster = L.Marker.extend({
896
+ initialize: function (group, zoom, a, b) {
897
+
898
+ L.Marker.prototype.initialize.call(this, a ? (a._cLatLng || a.getLatLng()) : new L.LatLng(0, 0), { icon: this });
899
+
900
+
901
+ this._group = group;
902
+ this._zoom = zoom;
903
+
904
+ this._markers = [];
905
+ this._childClusters = [];
906
+ this._childCount = 0;
907
+ this._iconNeedsUpdate = true;
908
+
909
+ this._bounds = new L.LatLngBounds();
910
+
911
+ if (a) {
912
+ this._addChild(a);
913
+ }
914
+ if (b) {
915
+ this._addChild(b);
916
+ }
917
+ },
918
+
919
+ //Recursively retrieve all child markers of this cluster
920
+ getAllChildMarkers: function (storageArray) {
921
+ storageArray = storageArray || [];
922
+
923
+ for (var i = this._childClusters.length - 1; i >= 0; i--) {
924
+ this._childClusters[i].getAllChildMarkers(storageArray);
925
+ }
926
+
927
+ for (var j = this._markers.length - 1; j >= 0; j--) {
928
+ storageArray.push(this._markers[j]);
929
+ }
930
+
931
+ return storageArray;
932
+ },
933
+
934
+ //Returns the count of how many child markers we have
935
+ getChildCount: function () {
936
+ return this._childCount;
937
+ },
938
+
939
+ //Zoom to the extents of this cluster
940
+ zoomToBounds: function () {
941
+ this._group._map.fitBounds(this._bounds);
942
+ },
943
+
944
+
945
+ _updateIcon: function () {
946
+ this._iconNeedsUpdate = true;
947
+ if (this._icon) {
948
+ this.setIcon(this);
949
+ }
950
+ },
951
+
952
+ //Cludge for Icon, we pretend to be an icon for performance
953
+ createIcon: function () {
954
+ if (this._iconNeedsUpdate) {
955
+ this._iconObj = this._group.options.iconCreateFunction(this);
956
+ this._iconNeedsUpdate = false;
957
+ }
958
+ return this._iconObj.createIcon();
959
+ },
960
+ createShadow: function () {
961
+ return this._iconObj.createShadow();
962
+ },
963
+
964
+
965
+ _addChild: function (new1, isNotificationFromChild) {
966
+
967
+ this._iconNeedsUpdate = true;
968
+ this._expandBounds(new1);
969
+
970
+ if (new1 instanceof L.MarkerCluster) {
971
+ if (!isNotificationFromChild) {
972
+ this._childClusters.push(new1);
973
+ new1.__parent = this;
974
+ }
975
+ this._childCount += new1._childCount;
976
+ } else {
977
+ if (!isNotificationFromChild) {
978
+ this._markers.push(new1);
979
+ }
980
+ this._childCount++;
981
+ }
982
+
983
+ if (this.__parent) {
984
+ this.__parent._addChild(new1, true);
985
+ }
986
+ },
987
+
988
+ //Expand our bounds and tell our parent to
989
+ _expandBounds: function (marker) {
990
+ var addedCount,
991
+ addedLatLng = marker._wLatLng || marker._latlng;
992
+
993
+ if (marker instanceof L.MarkerCluster) {
994
+ this._bounds.extend(marker._bounds);
995
+ addedCount = marker._childCount;
996
+ } else {
997
+ this._bounds.extend(addedLatLng);
998
+ addedCount = 1;
999
+ }
1000
+
1001
+ if (!this._cLatLng) {
1002
+ // when clustering, take position of the first point as the cluster center
1003
+ this._cLatLng = marker._cLatLng || addedLatLng;
1004
+ }
1005
+
1006
+ // when showing clusters, take weighted average of all points as cluster center
1007
+ var totalCount = this._childCount + addedCount;
1008
+
1009
+ //Calculate weighted latlng for display
1010
+ if (!this._wLatLng) {
1011
+ this._latlng = this._wLatLng = new L.LatLng(addedLatLng.lat, addedLatLng.lng);
1012
+ } else {
1013
+ this._wLatLng.lat = (addedLatLng.lat * addedCount + this._wLatLng.lat * this._childCount) / totalCount;
1014
+ this._wLatLng.lng = (addedLatLng.lng * addedCount + this._wLatLng.lng * this._childCount) / totalCount;
1015
+ }
1016
+ },
1017
+
1018
+ //Set our markers position as given and add it to the map
1019
+ _addToMap: function (startPos) {
1020
+ if (startPos) {
1021
+ this._backupLatlng = this._latlng;
1022
+ this.setLatLng(startPos);
1023
+ }
1024
+ L.FeatureGroup.prototype.addLayer.call(this._group, this);
1025
+ },
1026
+
1027
+ _recursivelyAnimateChildrenIn: function (bounds, center, maxZoom) {
1028
+ this._recursively(bounds, 0, maxZoom - 1,
1029
+ function (c) {
1030
+ var markers = c._markers,
1031
+ i, m;
1032
+ for (i = markers.length - 1; i >= 0; i--) {
1033
+ m = markers[i];
1034
+
1035
+ //Only do it if the icon is still on the map
1036
+ if (m._icon) {
1037
+ m._setPos(center);
1038
+ m.setOpacity(0);
1039
+ }
1040
+ }
1041
+ },
1042
+ function (c) {
1043
+ var childClusters = c._childClusters,
1044
+ j, cm;
1045
+ for (j = childClusters.length - 1; j >= 0; j--) {
1046
+ cm = childClusters[j];
1047
+ if (cm._icon) {
1048
+ cm._setPos(center);
1049
+ cm.setOpacity(0);
1050
+ }
1051
+ }
1052
+ }
1053
+ );
1054
+ },
1055
+
1056
+ _recursivelyAnimateChildrenInAndAddSelfToMap: function (bounds, previousZoomLevel, newZoomLevel) {
1057
+ this._recursively(bounds, newZoomLevel, 0,
1058
+ function (c) {
1059
+ c._recursivelyAnimateChildrenIn(bounds, c._group._map.latLngToLayerPoint(c.getLatLng()).round(), previousZoomLevel);
1060
+
1061
+ //TODO: depthToAnimateIn affects _isSingleParent, if there is a multizoom we may/may not be.
1062
+ //As a hack we only do a animation free zoom on a single level zoom, if someone does multiple levels then we always animate
1063
+ if (c._isSingleParent() && previousZoomLevel - 1 === newZoomLevel) {
1064
+ c.setOpacity(1);
1065
+ c._recursivelyRemoveChildrenFromMap(bounds, previousZoomLevel); //Immediately remove our children as we are replacing them. TODO previousBounds not bounds
1066
+ } else {
1067
+ c.setOpacity(0);
1068
+ }
1069
+
1070
+ c._addToMap();
1071
+ }
1072
+ );
1073
+ },
1074
+
1075
+ _recursivelyBecomeVisible: function (bounds, zoomLevel) {
1076
+ this._recursively(bounds, 0, zoomLevel, null, function (c) {
1077
+ c.setOpacity(1);
1078
+ });
1079
+ },
1080
+
1081
+ _recursivelyAddChildrenToMap: function (startPos, zoomLevel, bounds) {
1082
+ this._recursively(bounds, -1, zoomLevel,
1083
+ function (c) {
1084
+ if (zoomLevel === c._zoom) {
1085
+ return;
1086
+ }
1087
+
1088
+ //Add our child markers at startPos (so they can be animated out)
1089
+ for (var i = c._markers.length - 1; i >= 0; i--) {
1090
+ var nm = c._markers[i];
1091
+
1092
+ if (!bounds.contains(nm._latlng)) {
1093
+ continue;
1094
+ }
1095
+
1096
+ if (startPos) {
1097
+ nm._backupLatlng = nm.getLatLng();
1098
+
1099
+ nm.setLatLng(startPos);
1100
+ nm.setOpacity(0);
1101
+ }
1102
+
1103
+ L.FeatureGroup.prototype.addLayer.call(c._group, nm);
1104
+ }
1105
+ },
1106
+ function (c) {
1107
+ c._addToMap(startPos);
1108
+ }
1109
+ );
1110
+ },
1111
+
1112
+ _recursivelyRestoreChildPositions: function (zoomLevel) {
1113
+ //Fix positions of child markers
1114
+ for (var i = this._markers.length - 1; i >= 0; i--) {
1115
+ var nm = this._markers[i];
1116
+ if (nm._backupLatlng) {
1117
+ nm.setLatLng(nm._backupLatlng);
1118
+ delete nm._backupLatlng;
1119
+ }
1120
+ }
1121
+
1122
+ if (zoomLevel - 1 === this._zoom) {
1123
+ //Reposition child clusters
1124
+ for (var j = this._childClusters.length - 1; j >= 0; j--) {
1125
+ this._childClusters[j]._restorePosition();
1126
+ }
1127
+ } else {
1128
+ for (var k = this._childClusters.length - 1; k >= 0; k--) {
1129
+ this._childClusters[k]._recursivelyRestoreChildPositions(zoomLevel);
1130
+ }
1131
+ }
1132
+ },
1133
+
1134
+ _restorePosition: function () {
1135
+ if (this._backupLatlng) {
1136
+ this.setLatLng(this._backupLatlng);
1137
+ delete this._backupLatlng;
1138
+ }
1139
+ },
1140
+
1141
+ //exceptBounds: If set, don't remove any markers/clusters in it
1142
+ _recursivelyRemoveChildrenFromMap: function (previousBounds, zoomLevel, exceptBounds) {
1143
+ var m, i;
1144
+ this._recursively(previousBounds, -1, zoomLevel - 1,
1145
+ function (c) {
1146
+ //Remove markers at every level
1147
+ for (i = c._markers.length - 1; i >= 0; i--) {
1148
+ m = c._markers[i];
1149
+ if (!exceptBounds || !exceptBounds.contains(m._latlng)) {
1150
+ L.FeatureGroup.prototype.removeLayer.call(c._group, m);
1151
+ m.setOpacity(1);
1152
+ }
1153
+ }
1154
+ },
1155
+ function (c) {
1156
+ //Remove child clusters at just the bottom level
1157
+ for (i = c._childClusters.length - 1; i >= 0; i--) {
1158
+ m = c._childClusters[i];
1159
+ if (!exceptBounds || !exceptBounds.contains(m._latlng)) {
1160
+ L.FeatureGroup.prototype.removeLayer.call(c._group, m);
1161
+ m.setOpacity(1);
1162
+ }
1163
+ }
1164
+ }
1165
+ );
1166
+ },
1167
+
1168
+ //Run the given functions recursively to this and child clusters
1169
+ // boundsToApplyTo: a L.LatLngBounds representing the bounds of what clusters to recurse in to
1170
+ // zoomLevelToStart: zoom level to start running functions (inclusive)
1171
+ // zoomLevelToStop: zoom level to stop running functions (inclusive)
1172
+ // runAtEveryLevel: function that takes an L.MarkerCluster as an argument that should be applied on every level
1173
+ // runAtBottomLevel: function that takes an L.MarkerCluster as an argument that should be applied at only the bottom level
1174
+ _recursively: function (boundsToApplyTo, zoomLevelToStart, zoomLevelToStop, runAtEveryLevel, runAtBottomLevel) {
1175
+ var childClusters = this._childClusters,
1176
+ zoom = this._zoom,
1177
+ i, c;
1178
+
1179
+ if (zoomLevelToStart > zoom) { //Still going down to required depth, just recurse to child clusters
1180
+ for (i = childClusters.length - 1; i >= 0; i--) {
1181
+ c = childClusters[i];
1182
+ if (boundsToApplyTo.intersects(c._bounds)) {
1183
+ c._recursively(boundsToApplyTo, zoomLevelToStart, zoomLevelToStop, runAtEveryLevel, runAtBottomLevel);
1184
+ }
1185
+ }
1186
+ } else { //In required depth
1187
+
1188
+ if (runAtEveryLevel) {
1189
+ runAtEveryLevel(this);
1190
+ }
1191
+ if (runAtBottomLevel && this._zoom === zoomLevelToStop) {
1192
+ runAtBottomLevel(this);
1193
+ }
1194
+
1195
+ //TODO: This loop is almost the same as above
1196
+ if (zoomLevelToStop > zoom) {
1197
+ for (i = childClusters.length - 1; i >= 0; i--) {
1198
+ c = childClusters[i];
1199
+ if (boundsToApplyTo.intersects(c._bounds)) {
1200
+ c._recursively(boundsToApplyTo, zoomLevelToStart, zoomLevelToStop, runAtEveryLevel, runAtBottomLevel);
1201
+ }
1202
+ }
1203
+ }
1204
+ }
1205
+ },
1206
+
1207
+ _recalculateBounds: function () {
1208
+ var markers = this._markers,
1209
+ childClusters = this._childClusters,
1210
+ i;
1211
+
1212
+ this._bounds = new L.LatLngBounds();
1213
+ delete this._wLatLng;
1214
+
1215
+ for (i = markers.length - 1; i >= 0; i--) {
1216
+ this._expandBounds(markers[i]);
1217
+ }
1218
+ for (i = childClusters.length - 1; i >= 0; i--) {
1219
+ this._expandBounds(childClusters[i]);
1220
+ }
1221
+ },
1222
+
1223
+
1224
+ //Returns true if we are the parent of only one cluster and that cluster is the same as us
1225
+ _isSingleParent: function () {
1226
+ //Don't need to check this._markers as the rest won't work if there are any
1227
+ return this._childClusters.length > 0 && this._childClusters[0]._childCount === this._childCount;
1228
+ }
1229
+ });
1230
+
1231
+
1232
+
1233
+ L.DistanceGrid = function (cellSize) {
1234
+ this._cellSize = cellSize;
1235
+ this._sqCellSize = cellSize * cellSize;
1236
+ this._grid = {};
1237
+ this._objectPoint = { };
1238
+ };
1239
+
1240
+ L.DistanceGrid.prototype = {
1241
+
1242
+ addObject: function (obj, point) {
1243
+ var x = this._getCoord(point.x),
1244
+ y = this._getCoord(point.y),
1245
+ grid = this._grid,
1246
+ row = grid[y] = grid[y] || {},
1247
+ cell = row[x] = row[x] || [],
1248
+ stamp = L.Util.stamp(obj);
1249
+
1250
+ this._objectPoint[stamp] = point;
1251
+
1252
+ cell.push(obj);
1253
+ },
1254
+
1255
+ updateObject: function (obj, point) {
1256
+ this.removeObject(obj);
1257
+ this.addObject(obj, point);
1258
+ },
1259
+
1260
+ //Returns true if the object was found
1261
+ removeObject: function (obj, point) {
1262
+ var x = this._getCoord(point.x),
1263
+ y = this._getCoord(point.y),
1264
+ grid = this._grid,
1265
+ row = grid[y] = grid[y] || {},
1266
+ cell = row[x] = row[x] || [],
1267
+ i, len;
1268
+
1269
+ delete this._objectPoint[L.Util.stamp(obj)];
1270
+
1271
+ for (i = 0, len = cell.length; i < len; i++) {
1272
+ if (cell[i] === obj) {
1273
+
1274
+ cell.splice(i, 1);
1275
+
1276
+ if (len === 1) {
1277
+ delete row[x];
1278
+ }
1279
+
1280
+ return true;
1281
+ }
1282
+ }
1283
+
1284
+ },
1285
+
1286
+ eachObject: function (fn, context) {
1287
+ var i, j, k, len, row, cell, removed,
1288
+ grid = this._grid;
1289
+
1290
+ for (i in grid) {
1291
+ if (grid.hasOwnProperty(i)) {
1292
+ row = grid[i];
1293
+
1294
+ for (j in row) {
1295
+ if (row.hasOwnProperty(j)) {
1296
+ cell = row[j];
1297
+
1298
+ for (k = 0, len = cell.length; k < len; k++) {
1299
+ removed = fn.call(context, cell[k]);
1300
+ if (removed) {
1301
+ k--;
1302
+ len--;
1303
+ }
1304
+ }
1305
+ }
1306
+ }
1307
+ }
1308
+ }
1309
+ },
1310
+
1311
+ getNearObject: function (point) {
1312
+ var x = this._getCoord(point.x),
1313
+ y = this._getCoord(point.y),
1314
+ i, j, k, row, cell, len, obj, dist,
1315
+ objectPoint = this._objectPoint,
1316
+ closestDistSq = this._sqCellSize,
1317
+ closest = null;
1318
+
1319
+ for (i = y - 1; i <= y + 1; i++) {
1320
+ row = this._grid[i];
1321
+ if (row) {
1322
+
1323
+ for (j = x - 1; j <= x + 1; j++) {
1324
+ cell = row[j];
1325
+ if (cell) {
1326
+
1327
+ for (k = 0, len = cell.length; k < len; k++) {
1328
+ obj = cell[k];
1329
+ dist = this._sqDist(objectPoint[L.Util.stamp(obj)], point);
1330
+ if (dist < closestDistSq) {
1331
+ closestDistSq = dist;
1332
+ closest = obj;
1333
+ }
1334
+ }
1335
+ }
1336
+ }
1337
+ }
1338
+ }
1339
+ return closest;
1340
+ },
1341
+
1342
+ _getCoord: function (x) {
1343
+ return Math.floor(x / this._cellSize);
1344
+ },
1345
+
1346
+ _sqDist: function (p, p2) {
1347
+ var dx = p2.x - p.x,
1348
+ dy = p2.y - p.y;
1349
+ return dx * dx + dy * dy;
1350
+ }
1351
+ };
1352
+
1353
+
1354
+ /* Copyright (c) 2012 the authors listed at the following URL, and/or
1355
+ the authors of referenced articles or incorporated external code:
1356
+ http://en.literateprograms.org/Quickhull_(Javascript)?action=history&offset=20120410175256
1357
+
1358
+ Permission is hereby granted, free of charge, to any person obtaining
1359
+ a copy of this software and associated documentation files (the
1360
+ "Software"), to deal in the Software without restriction, including
1361
+ without limitation the rights to use, copy, modify, merge, publish,
1362
+ distribute, sublicense, and/or sell copies of the Software, and to
1363
+ permit persons to whom the Software is furnished to do so, subject to
1364
+ the following conditions:
1365
+
1366
+ The above copyright notice and this permission notice shall be
1367
+ included in all copies or substantial portions of the Software.
1368
+
1369
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
1370
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
1371
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
1372
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
1373
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
1374
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
1375
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
1376
+
1377
+ Retrieved from: http://en.literateprograms.org/Quickhull_(Javascript)?oldid=18434
1378
+ */
1379
+
1380
+ (function () {
1381
+ L.QuickHull = {
1382
+ getDistant: function (cpt, bl) {
1383
+ var vY = bl[1].lat - bl[0].lat,
1384
+ vX = bl[0].lng - bl[1].lng;
1385
+ return (vX * (cpt.lat - bl[0].lat) + vY * (cpt.lng - bl[0].lng));
1386
+ },
1387
+
1388
+
1389
+ findMostDistantPointFromBaseLine: function (baseLine, latLngs) {
1390
+ var maxD = 0,
1391
+ maxPt = null,
1392
+ newPoints = [],
1393
+ i, pt, d;
1394
+
1395
+ for (i = latLngs.length - 1; i >= 0; i--) {
1396
+ pt = latLngs[i];
1397
+ d = this.getDistant(pt, baseLine);
1398
+
1399
+ if (d > 0) {
1400
+ newPoints.push(pt);
1401
+ } else {
1402
+ continue;
1403
+ }
1404
+
1405
+ if (d > maxD) {
1406
+ maxD = d;
1407
+ maxPt = pt;
1408
+ }
1409
+
1410
+ }
1411
+ return { 'maxPoint': maxPt, 'newPoints': newPoints };
1412
+ },
1413
+
1414
+ buildConvexHull: function (baseLine, latLngs) {
1415
+ var convexHullBaseLines = [],
1416
+ t = this.findMostDistantPointFromBaseLine(baseLine, latLngs);
1417
+
1418
+ if (t.maxPoint) { // if there is still a point "outside" the base line
1419
+ convexHullBaseLines =
1420
+ convexHullBaseLines.concat(
1421
+ this.buildConvexHull([baseLine[0], t.maxPoint], t.newPoints)
1422
+ );
1423
+ convexHullBaseLines =
1424
+ convexHullBaseLines.concat(
1425
+ this.buildConvexHull([t.maxPoint, baseLine[1]], t.newPoints)
1426
+ );
1427
+ return convexHullBaseLines;
1428
+ } else { // if there is no more point "outside" the base line, the current base line is part of the convex hull
1429
+ return [baseLine];
1430
+ }
1431
+ },
1432
+
1433
+ getConvexHull: function (latLngs) {
1434
+ //find first baseline
1435
+ var maxLat = false, minLat = false,
1436
+ maxPt = null, minPt = null,
1437
+ i;
1438
+
1439
+ for (i = latLngs.length - 1; i >= 0; i--) {
1440
+ var pt = latLngs[i];
1441
+ if (maxLat === false || pt.lat > maxLat) {
1442
+ maxPt = pt;
1443
+ maxLat = pt.lat;
1444
+ }
1445
+ if (minLat === false || pt.lat < minLat) {
1446
+ minPt = pt;
1447
+ minLat = pt.lat;
1448
+ }
1449
+ }
1450
+ var ch = [].concat(this.buildConvexHull([minPt, maxPt], latLngs),
1451
+ this.buildConvexHull([maxPt, minPt], latLngs));
1452
+ return ch;
1453
+ }
1454
+ };
1455
+ }());
1456
+
1457
+ L.MarkerCluster.include({
1458
+ getConvexHull: function () {
1459
+ var childMarkers = this.getAllChildMarkers(),
1460
+ points = [],
1461
+ hullLatLng = [],
1462
+ hull, p, i;
1463
+
1464
+ for (i = childMarkers.length - 1; i >= 0; i--) {
1465
+ p = childMarkers[i].getLatLng();
1466
+ points.push(p);
1467
+ }
1468
+
1469
+ hull = L.QuickHull.getConvexHull(points);
1470
+
1471
+ for (i = hull.length - 1; i >= 0; i--) {
1472
+ hullLatLng.push(hull[i][0]);
1473
+ }
1474
+
1475
+ return hullLatLng;
1476
+ }
1477
+ });
1478
+
1479
+ //This code is 100% based on https://github.com/jawj/OverlappingMarkerSpiderfier-Leaflet
1480
+ //Huge thanks to jawj for implementing it first to make my job easy :-)
1481
+
1482
+ L.MarkerCluster.include({
1483
+
1484
+ _2PI: Math.PI * 2,
1485
+ _circleFootSeparation: 25, //related to circumference of circle
1486
+ _circleStartAngle: Math.PI / 6,
1487
+
1488
+ _spiralFootSeparation: 28, //related to size of spiral (experiment!)
1489
+ _spiralLengthStart: 11,
1490
+ _spiralLengthFactor: 5,
1491
+
1492
+ _circleSpiralSwitchover: 9, //show spiral instead of circle from this marker count upwards.
1493
+ // 0 -> always spiral; Infinity -> always circle
1494
+
1495
+ spiderfy: function () {
1496
+ if (this._group._spiderfied === this || this._group._inZoomAnimation) {
1497
+ return;
1498
+ }
1499
+
1500
+ var childMarkers = this.getAllChildMarkers(),
1501
+ group = this._group,
1502
+ map = group._map,
1503
+ center = map.latLngToLayerPoint(this._latlng),
1504
+ positions;
1505
+
1506
+ this._group._unspiderfy();
1507
+ this._group._spiderfied = this;
1508
+
1509
+ //TODO Maybe: childMarkers order by distance to center
1510
+
1511
+ if (childMarkers.length >= this._circleSpiralSwitchover) {
1512
+ positions = this._generatePointsSpiral(childMarkers.length, center);
1513
+ } else {
1514
+ center.y += 10; //Otherwise circles look wrong
1515
+ positions = this._generatePointsCircle(childMarkers.length, center);
1516
+ }
1517
+
1518
+ this._animationSpiderfy(childMarkers, positions);
1519
+ },
1520
+
1521
+ unspiderfy: function (zoomDetails) {
1522
+ /// <param Name="zoomDetails">Argument from zoomanim if being called in a zoom animation or null otherwise</param>
1523
+ if (this._group._inZoomAnimation) {
1524
+ return;
1525
+ }
1526
+ this._animationUnspiderfy(zoomDetails);
1527
+
1528
+ this._group._spiderfied = null;
1529
+ },
1530
+
1531
+ _generatePointsCircle: function (count, centerPt) {
1532
+ var circumference = this._group.options.spiderfyDistanceMultiplier * this._circleFootSeparation * (2 + count),
1533
+ legLength = circumference / this._2PI, //radius from circumference
1534
+ angleStep = this._2PI / count,
1535
+ res = [],
1536
+ i, angle;
1537
+
1538
+ res.length = count;
1539
+
1540
+ for (i = count - 1; i >= 0; i--) {
1541
+ angle = this._circleStartAngle + i * angleStep;
1542
+ res[i] = new L.Point(centerPt.x + legLength * Math.cos(angle), centerPt.y + legLength * Math.sin(angle))._round();
1543
+ }
1544
+
1545
+ return res;
1546
+ },
1547
+
1548
+ _generatePointsSpiral: function (count, centerPt) {
1549
+ var legLength = this._group.options.spiderfyDistanceMultiplier * this._spiralLengthStart,
1550
+ separation = this._group.options.spiderfyDistanceMultiplier * this._spiralFootSeparation,
1551
+ lengthFactor = this._group.options.spiderfyDistanceMultiplier * this._spiralLengthFactor,
1552
+ angle = 0,
1553
+ res = [],
1554
+ i;
1555
+
1556
+ res.length = count;
1557
+
1558
+ for (i = count - 1; i >= 0; i--) {
1559
+ angle += separation / legLength + i * 0.0005;
1560
+ res[i] = new L.Point(centerPt.x + legLength * Math.cos(angle), centerPt.y + legLength * Math.sin(angle))._round();
1561
+ legLength += this._2PI * lengthFactor / angle;
1562
+ }
1563
+ return res;
1564
+ }
1565
+ });
1566
+
1567
+ L.MarkerCluster.include(!L.DomUtil.TRANSITION ? {
1568
+ //Non Animated versions of everything
1569
+ _animationSpiderfy: function (childMarkers, positions) {
1570
+ var group = this._group,
1571
+ map = group._map,
1572
+ i, m, leg, newPos;
1573
+
1574
+ for (i = childMarkers.length - 1; i >= 0; i--) {
1575
+ newPos = map.layerPointToLatLng(positions[i]);
1576
+ m = childMarkers[i];
1577
+
1578
+ m._preSpiderfyLatlng = m._latlng;
1579
+ m.setLatLng(newPos);
1580
+ m.setZIndexOffset(1000000); //Make these appear on top of EVERYTHING
1581
+
1582
+ L.FeatureGroup.prototype.addLayer.call(group, m);
1583
+
1584
+
1585
+ leg = new L.Polyline([this._latlng, newPos], { weight: 1.5, color: '#222' });
1586
+ map.addLayer(leg);
1587
+ m._spiderLeg = leg;
1588
+ }
1589
+ this.setOpacity(0.3);
1590
+ group.fire('spiderfied');
1591
+ },
1592
+
1593
+ _animationUnspiderfy: function () {
1594
+ var group = this._group,
1595
+ map = group._map,
1596
+ childMarkers = this.getAllChildMarkers(),
1597
+ m, i;
1598
+
1599
+ this.setOpacity(1);
1600
+ for (i = childMarkers.length - 1; i >= 0; i--) {
1601
+ m = childMarkers[i];
1602
+
1603
+ L.FeatureGroup.prototype.removeLayer.call(group, m);
1604
+
1605
+ m.setLatLng(m._preSpiderfyLatlng);
1606
+ delete m._preSpiderfyLatlng;
1607
+ m.setZIndexOffset(0);
1608
+
1609
+ map.removeLayer(m._spiderLeg);
1610
+ delete m._spiderLeg;
1611
+ }
1612
+ }
1613
+ } : {
1614
+ //Animated versions here
1615
+ SVG_ANIMATION: (function () {
1616
+ return document.createElementNS('http://www.w3.org/2000/svg', 'animate').toString().indexOf('SVGAnimate') > -1;
1617
+ }()),
1618
+
1619
+ _animationSpiderfy: function (childMarkers, positions) {
1620
+ var me = this,
1621
+ group = this._group,
1622
+ map = group._map,
1623
+ thisLayerPos = map.latLngToLayerPoint(this._latlng),
1624
+ i, m, leg, newPos;
1625
+
1626
+ //Add markers to map hidden at our center point
1627
+ for (i = childMarkers.length - 1; i >= 0; i--) {
1628
+ m = childMarkers[i];
1629
+
1630
+ m.setZIndexOffset(1000000); //Make these appear on top of EVERYTHING
1631
+ m.setOpacity(0);
1632
+
1633
+ L.FeatureGroup.prototype.addLayer.call(group, m);
1634
+
1635
+ m._setPos(thisLayerPos);
1636
+ }
1637
+
1638
+ group._forceLayout();
1639
+ group._animationStart();
1640
+
1641
+ var initialLegOpacity = L.Path.SVG ? 0 : 0.3,
1642
+ xmlns = L.Path.SVG_NS;
1643
+
1644
+
1645
+ for (i = childMarkers.length - 1; i >= 0; i--) {
1646
+ newPos = map.layerPointToLatLng(positions[i]);
1647
+ m = childMarkers[i];
1648
+
1649
+ //Move marker to new position
1650
+ m._preSpiderfyLatlng = m._latlng;
1651
+ m.setLatLng(newPos);
1652
+ m.setOpacity(1);
1653
+
1654
+
1655
+ //Add Legs.
1656
+ leg = new L.Polyline([me._latlng, newPos], { weight: 1.5, color: '#222', opacity: initialLegOpacity });
1657
+ map.addLayer(leg);
1658
+ m._spiderLeg = leg;
1659
+
1660
+ //Following animations don't work for canvas
1661
+ if (!L.Path.SVG || !this.SVG_ANIMATION) {
1662
+ continue;
1663
+ }
1664
+
1665
+ //How this works:
1666
+ //http://stackoverflow.com/questions/5924238/how-do-you-animate-an-svg-path-in-ios
1667
+ //http://dev.opera.com/articles/view/advanced-svg-animation-techniques/
1668
+
1669
+ //Animate length
1670
+ var length = leg._path.getTotalLength();
1671
+ leg._path.setAttribute("stroke-dasharray", length + "," + length);
1672
+
1673
+ var anim = document.createElementNS(xmlns, "animate");
1674
+ anim.setAttribute("attributeName", "stroke-dashoffset");
1675
+ anim.setAttribute("begin", "indefinite");
1676
+ anim.setAttribute("from", length);
1677
+ anim.setAttribute("to", 0);
1678
+ anim.setAttribute("dur", 0.25);
1679
+ leg._path.appendChild(anim);
1680
+ anim.beginElement();
1681
+
1682
+ //Animate opacity
1683
+ anim = document.createElementNS(xmlns, "animate");
1684
+ anim.setAttribute("attributeName", "stroke-opacity");
1685
+ anim.setAttribute("attributeName", "stroke-opacity");
1686
+ anim.setAttribute("begin", "indefinite");
1687
+ anim.setAttribute("from", 0);
1688
+ anim.setAttribute("to", 0.5);
1689
+ anim.setAttribute("dur", 0.25);
1690
+ leg._path.appendChild(anim);
1691
+ anim.beginElement();
1692
+ }
1693
+ me.setOpacity(0.3);
1694
+
1695
+ //Set the opacity of the spiderLegs back to their correct value
1696
+ // The animations above override this until they complete.
1697
+ // If the initial opacity of the spiderlegs isn't 0 then they appear before the animation starts.
1698
+ if (L.Path.SVG) {
1699
+ this._group._forceLayout();
1700
+
1701
+ for (i = childMarkers.length - 1; i >= 0; i--) {
1702
+ m = childMarkers[i]._spiderLeg;
1703
+
1704
+ m.options.opacity = 0.5;
1705
+ m._path.setAttribute('stroke-opacity', 0.5);
1706
+ }
1707
+ }
1708
+
1709
+ setTimeout(function () {
1710
+ group._animationEnd();
1711
+ group.fire('spiderfied');
1712
+ }, 250);
1713
+ },
1714
+
1715
+ _animationUnspiderfy: function (zoomDetails) {
1716
+ var group = this._group,
1717
+ map = group._map,
1718
+ thisLayerPos = zoomDetails ? map._latLngToNewLayerPoint(this._latlng, zoomDetails.zoom, zoomDetails.center) : map.latLngToLayerPoint(this._latlng),
1719
+ childMarkers = this.getAllChildMarkers(),
1720
+ svg = L.Path.SVG && this.SVG_ANIMATION,
1721
+ m, i, a;
1722
+
1723
+ group._animationStart();
1724
+
1725
+ //Make us visible and bring the child markers back in
1726
+ this.setOpacity(1);
1727
+ for (i = childMarkers.length - 1; i >= 0; i--) {
1728
+ m = childMarkers[i];
1729
+
1730
+ //Marker was added to us after we were spidified
1731
+ if (!m._preSpiderfyLatlng) {
1732
+ continue;
1733
+ }
1734
+
1735
+ //Fix up the location to the real one
1736
+ m.setLatLng(m._preSpiderfyLatlng);
1737
+ delete m._preSpiderfyLatlng;
1738
+ //Hack override the location to be our center
1739
+ m._setPos(thisLayerPos);
1740
+
1741
+ m.setOpacity(0);
1742
+
1743
+ //Animate the spider legs back in
1744
+ if (svg) {
1745
+ a = m._spiderLeg._path.childNodes[0];
1746
+ a.setAttribute('to', a.getAttribute('from'));
1747
+ a.setAttribute('from', 0);
1748
+ a.beginElement();
1749
+
1750
+ a = m._spiderLeg._path.childNodes[1];
1751
+ a.setAttribute('from', 0.5);
1752
+ a.setAttribute('to', 0);
1753
+ a.setAttribute('stroke-opacity', 0);
1754
+ a.beginElement();
1755
+
1756
+ m._spiderLeg._path.setAttribute('stroke-opacity', 0);
1757
+ }
1758
+ }
1759
+
1760
+ setTimeout(function () {
1761
+ //If we have only <= one child left then that marker will be shown on the map so don't remove it!
1762
+ var stillThereChildCount = 0;
1763
+ for (i = childMarkers.length - 1; i >= 0; i--) {
1764
+ m = childMarkers[i];
1765
+ if (m._spiderLeg) {
1766
+ stillThereChildCount++;
1767
+ }
1768
+ }
1769
+
1770
+
1771
+ for (i = childMarkers.length - 1; i >= 0; i--) {
1772
+ m = childMarkers[i];
1773
+
1774
+ if (!m._spiderLeg) { //Has already been unspiderfied
1775
+ continue;
1776
+ }
1777
+
1778
+
1779
+ m.setOpacity(1);
1780
+ m.setZIndexOffset(0);
1781
+
1782
+ if (stillThereChildCount > 1) {
1783
+ L.FeatureGroup.prototype.removeLayer.call(group, m);
1784
+ }
1785
+
1786
+ map.removeLayer(m._spiderLeg);
1787
+ delete m._spiderLeg;
1788
+ }
1789
+ group._animationEnd();
1790
+ }, 250);
1791
+ }
1792
+ });
1793
+
1794
+
1795
+ L.MarkerClusterGroup.include({
1796
+ //The MarkerCluster currently spiderfied (if any)
1797
+ _spiderfied: null,
1798
+
1799
+ _spiderfierOnAdd: function () {
1800
+ this._map.on('click', this._unspiderfyWrapper, this);
1801
+
1802
+ if (this._map.options.zoomAnimation) {
1803
+ this._map.on('zoomstart', this._unspiderfyZoomStart, this);
1804
+ } else {
1805
+ //Browsers without zoomAnimation don't fire zoomstart
1806
+ this._map.on('zoomend', this._unspiderfyWrapper, this);
1807
+ }
1808
+
1809
+ if (L.Path.SVG && !L.Browser.touch) {
1810
+ this._map._initPathRoot();
1811
+ //Needs to happen in the pageload, not after, or animations don't work in webkit
1812
+ // http://stackoverflow.com/questions/8455200/svg-animate-with-dynamically-added-elements
1813
+ //Disable on touch browsers as the animation messes up on a touch zoom and isn't very noticable
1814
+ }
1815
+ },
1816
+
1817
+ _spiderfierOnRemove: function () {
1818
+ this._map.off('click', this._unspiderfyWrapper, this);
1819
+ this._map.off('zoomstart', this._unspiderfyZoomStart, this);
1820
+ this._map.off('zoomanim', this._unspiderfyZoomAnim, this);
1821
+
1822
+ this._unspiderfy(); //Ensure that markers are back where they should be
1823
+ },
1824
+
1825
+
1826
+ //On zoom start we add a zoomanim handler so that we are guaranteed to be last (after markers are animated)
1827
+ //This means we can define the animation they do rather than Markers doing an animation to their actual location
1828
+ _unspiderfyZoomStart: function () {
1829
+ if (!this._map) { //May have been removed from the map by a zoomEnd handler
1830
+ return;
1831
+ }
1832
+
1833
+ this._map.on('zoomanim', this._unspiderfyZoomAnim, this);
1834
+ },
1835
+ _unspiderfyZoomAnim: function (zoomDetails) {
1836
+ //Wait until the first zoomanim after the user has finished touch-zooming before running the animation
1837
+ if (L.DomUtil.hasClass(this._map._mapPane, 'leaflet-touching')) {
1838
+ return;
1839
+ }
1840
+
1841
+ this._map.off('zoomanim', this._unspiderfyZoomAnim, this);
1842
+ this._unspiderfy(zoomDetails);
1843
+ },
1844
+
1845
+
1846
+ _unspiderfyWrapper: function () {
1847
+ /// <summary>_unspiderfy but passes no arguments</summary>
1848
+ this._unspiderfy();
1849
+ },
1850
+
1851
+ _unspiderfy: function (zoomDetails) {
1852
+ if (this._spiderfied) {
1853
+ this._spiderfied.unspiderfy(zoomDetails);
1854
+ }
1855
+ },
1856
+
1857
+ //If the given layer is currently being spiderfied then we unspiderfy it so it isn't on the map anymore etc
1858
+ _unspiderfyLayer: function (layer) {
1859
+ if (layer._spiderLeg) {
1860
+ L.FeatureGroup.prototype.removeLayer.call(this, layer);
1861
+
1862
+ layer.setOpacity(1);
1863
+ //Position will be fixed up immediately in _animationUnspiderfy
1864
+ layer.setZIndexOffset(0);
1865
+
1866
+ this._map.removeLayer(layer._spiderLeg);
1867
+ delete layer._spiderLeg;
1868
+ }
1869
+ }
1870
+ });
1871
+
1872
+
1873
+
1874
+ }(this));