- Timestamp:
- Jan 31, 2018, 2:52:42 PM (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
binary-improvements/webserver/leaflet/markercluster/leaflet.markercluster-src.js
r173 r315 2 2 Leaflet.markercluster, Provides Beautiful Animated Marker Clustering functionality for Leaflet, a JS library for interactive maps. 3 3 https://github.com/Leaflet/Leaflet.markercluster 4 (c) 2012-201 3, Dave Leaver, smartrak4 (c) 2012-2017, Dave Leaver 5 5 */ 6 6 (function (window, document, undefined) {/* … … 13 13 maxClusterRadius: 80, //A cluster will cover at most this many pixels from its center 14 14 iconCreateFunction: null, 15 clusterPane: L.Marker.prototype.options.pane, 15 16 16 17 spiderfyOnMaxZoom: true, … … 25 26 removeOutsideVisibleBounds: true, 26 27 28 // Set to false to disable all animations (zoom and spiderfy). 29 // If false, option animateAddingMarkers below has no effect. 30 // If L.DomUtil.TRANSITION is falsy, this option has no effect. 31 animate: true, 32 27 33 //Whether to animate adding markers after adding the MarkerClusterGroup to the map 28 34 // If you are adding individual markers set to true, if adding bulk markers leave false for massive performance gains. … … 31 37 //Increase to increase the distance away that spiderfied markers appear from the center 32 38 spiderfyDistanceMultiplier: 1, 39 40 // Make it possible to specify a polyline options on a spider leg 41 spiderLegPolylineOptions: { weight: 1.5, color: '#222', opacity: 0.5 }, 33 42 34 43 // When bulk adding layers, adds markers in chunks. Means addLayers may not add all the layers in the call, others will be loaded during setTimeouts … … 49 58 50 59 this._featureGroup = L.featureGroup(); 51 this._featureGroup. on(L.FeatureGroup.EVENTS, this._propagateEvent,this);60 this._featureGroup.addEventParent(this); 52 61 53 62 this._nonPointGroup = L.featureGroup(); 54 this._nonPointGroup. on(L.FeatureGroup.EVENTS, this._propagateEvent,this);63 this._nonPointGroup.addEventParent(this); 55 64 56 65 this._inZoomAnimation = 0; … … 61 70 62 71 this._queue = []; 72 73 this._childMarkerEventHandlers = { 74 'dragstart': this._childMarkerDragStart, 75 'move': this._childMarkerMoved, 76 'dragend': this._childMarkerDragEnd, 77 }; 78 79 // Hook the appropriate animation methods. 80 var animate = L.DomUtil.TRANSITION && this.options.animate; 81 L.extend(this, animate ? this._withAnimation : this._noAnimation); 82 // Remember which MarkerCluster class to instantiate (animated or not). 83 this._markerCluster = animate ? L.MarkerCluster : L.MarkerClusterNonAnimated; 63 84 }, 64 85 … … 66 87 67 88 if (layer instanceof L.LayerGroup) { 68 var array = []; 69 for (var i in layer._layers) { 70 array.push(layer._layers[i]); 71 } 72 return this.addLayers(array); 89 return this.addLayers([layer]); 73 90 } 74 91 … … 76 93 if (!layer.getLatLng) { 77 94 this._nonPointGroup.addLayer(layer); 95 this.fire('layeradd', { layer: layer }); 78 96 return this; 79 97 } … … 81 99 if (!this._map) { 82 100 this._needsClustering.push(layer); 101 this.fire('layeradd', { layer: layer }); 83 102 return this; 84 103 } … … 96 115 97 116 this._addLayer(layer, this._maxZoom); 117 this.fire('layeradd', { layer: layer }); 118 119 // Refresh bounds and weighted positions. 120 this._topClusterLevel._recalculateBounds(); 121 122 this._refreshClustersIcons(); 98 123 99 124 //Work out what is visible 100 125 var visibleLayer = layer, 101 currentZoom = this._map.getZoom();126 currentZoom = this._zoom; 102 127 if (layer.__parent) { 103 128 while (visibleLayer.__parent._zoom >= currentZoom) { … … 118 143 removeLayer: function (layer) { 119 144 120 if (layer instanceof L.LayerGroup) 121 { 122 var array = []; 123 for (var i in layer._layers) { 124 array.push(layer._layers[i]); 125 } 126 return this.removeLayers(array); 145 if (layer instanceof L.LayerGroup) { 146 return this.removeLayers([layer]); 127 147 } 128 148 … … 130 150 if (!layer.getLatLng) { 131 151 this._nonPointGroup.removeLayer(layer); 152 this.fire('layerremove', { layer: layer }); 132 153 return this; 133 154 } … … 135 156 if (!this._map) { 136 157 if (!this._arraySplice(this._needsClustering, layer) && this.hasLayer(layer)) { 137 this._needsRemoving.push(layer); 138 } 158 this._needsRemoving.push({ layer: layer, latlng: layer._latlng }); 159 } 160 this.fire('layerremove', { layer: layer }); 139 161 return this; 140 162 } … … 151 173 //Remove the marker from clusters 152 174 this._removeLayer(layer, true); 175 this.fire('layerremove', { layer: layer }); 176 177 // Refresh bounds and weighted positions. 178 this._topClusterLevel._recalculateBounds(); 179 180 this._refreshClustersIcons(); 181 182 layer.off(this._childMarkerEventHandlers, this); 153 183 154 184 if (this._featureGroup.hasLayer(layer)) { 155 185 this._featureGroup.removeLayer(layer); 156 if (layer. setOpacity) {157 layer. setOpacity(1);186 if (layer.clusterShow) { 187 layer.clusterShow(); 158 188 } 159 189 } … … 163 193 164 194 //Takes an array of markers and adds them in bulk 165 addLayers: function (layersArray) { 195 addLayers: function (layersArray, skipLayerAddEvent) { 196 if (!L.Util.isArray(layersArray)) { 197 return this.addLayer(layersArray); 198 } 199 166 200 var fg = this._featureGroup, 167 npg = this._nonPointGroup, 168 chunked = this.options.chunkedLoading, 169 chunkInterval = this.options.chunkInterval, 170 chunkProgress = this.options.chunkProgress, 171 newMarkers, i, l, m; 201 npg = this._nonPointGroup, 202 chunked = this.options.chunkedLoading, 203 chunkInterval = this.options.chunkInterval, 204 chunkProgress = this.options.chunkProgress, 205 l = layersArray.length, 206 offset = 0, 207 originalArray = true, 208 m; 172 209 173 210 if (this._map) { 174 var offset = 0, 175 started = (new Date()).getTime(); 211 var started = (new Date()).getTime(); 176 212 var process = L.bind(function () { 177 213 var start = (new Date()).getTime(); 178 for (; offset < l ayersArray.length; offset++) {214 for (; offset < l; offset++) { 179 215 if (chunked && offset % 200 === 0) { 180 216 // every couple hundred markers, instrument the time elapsed since processing started: … … 187 223 m = layersArray[offset]; 188 224 225 // Group of layers, append children to layersArray and skip. 226 // Side effects: 227 // - Total increases, so chunkProgress ratio jumps backward. 228 // - Groups are not included in this group, only their non-group child layers (hasLayer). 229 // Changing array length while looping does not affect performance in current browsers: 230 // http://jsperf.com/for-loop-changing-length/6 231 if (m instanceof L.LayerGroup) { 232 if (originalArray) { 233 layersArray = layersArray.slice(); 234 originalArray = false; 235 } 236 this._extractNonGroupLayers(m, layersArray); 237 l = layersArray.length; 238 continue; 239 } 240 189 241 //Not point data, can't be clustered 190 242 if (!m.getLatLng) { 191 243 npg.addLayer(m); 244 if (!skipLayerAddEvent) { 245 this.fire('layeradd', { layer: m }); 246 } 192 247 continue; 193 248 } … … 198 253 199 254 this._addLayer(m, this._maxZoom); 255 if (!skipLayerAddEvent) { 256 this.fire('layeradd', { layer: m }); 257 } 200 258 201 259 //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 … … 203 261 if (m.__parent.getChildCount() === 2) { 204 262 var markers = m.__parent.getAllChildMarkers(), 205 263 otherMarker = markers[0] === m ? markers[1] : markers[0]; 206 264 fg.removeLayer(otherMarker); 207 265 } … … 211 269 if (chunkProgress) { 212 270 // report progress and time elapsed: 213 chunkProgress(offset, l ayersArray.length, (new Date()).getTime() - started);214 } 215 216 if (offset === layersArray.length) {217 //Update the icons of all those visible clusters that were affected218 this._featureGroup.eachLayer(function (c) { 219 if (c instanceof L.MarkerCluster && c._iconNeedsUpdate) {220 c._updateIcon();221 } 222 });271 chunkProgress(offset, l, (new Date()).getTime() - started); 272 } 273 274 // Completed processing all markers. 275 if (offset === l) { 276 277 // Refresh bounds and weighted positions. 278 this._topClusterLevel._recalculateBounds(); 279 280 this._refreshClustersIcons(); 223 281 224 282 this._topClusterLevel._recursivelyAddChildrenToMap(null, this._zoom, this._currentShownBounds); … … 230 288 process(); 231 289 } else { 232 newMarkers = []; 233 for (i = 0, l = layersArray.length; i < l; i++) { 234 m = layersArray[i]; 290 var needsClustering = this._needsClustering; 291 292 for (; offset < l; offset++) { 293 m = layersArray[offset]; 294 295 // Group of layers, append children to layersArray and skip. 296 if (m instanceof L.LayerGroup) { 297 if (originalArray) { 298 layersArray = layersArray.slice(); 299 originalArray = false; 300 } 301 this._extractNonGroupLayers(m, layersArray); 302 l = layersArray.length; 303 continue; 304 } 235 305 236 306 //Not point data, can't be clustered … … 244 314 } 245 315 246 newMarkers.push(m); 247 } 248 this._needsClustering = this._needsClustering.concat(newMarkers); 316 needsClustering.push(m); 317 } 249 318 } 250 319 return this; … … 253 322 //Takes an array of markers and removes them in bulk 254 323 removeLayers: function (layersArray) { 255 var i, l, m, 256 fg = this._featureGroup, 257 npg = this._nonPointGroup; 324 var i, m, 325 l = layersArray.length, 326 fg = this._featureGroup, 327 npg = this._nonPointGroup, 328 originalArray = true; 258 329 259 330 if (!this._map) { 260 for (i = 0 , l = layersArray.length; i < l; i++) {331 for (i = 0; i < l; i++) { 261 332 m = layersArray[i]; 333 334 // Group of layers, append children to layersArray and skip. 335 if (m instanceof L.LayerGroup) { 336 if (originalArray) { 337 layersArray = layersArray.slice(); 338 originalArray = false; 339 } 340 this._extractNonGroupLayers(m, layersArray); 341 l = layersArray.length; 342 continue; 343 } 344 262 345 this._arraySplice(this._needsClustering, m); 263 346 npg.removeLayer(m); 347 if (this.hasLayer(m)) { 348 this._needsRemoving.push({ layer: m, latlng: m._latlng }); 349 } 350 this.fire('layerremove', { layer: m }); 264 351 } 265 352 return this; 266 353 } 267 354 268 for (i = 0, l = layersArray.length; i < l; i++) { 355 if (this._unspiderfy) { 356 this._unspiderfy(); 357 358 // Work on a copy of the array, so that next loop is not affected. 359 var layersArray2 = layersArray.slice(), 360 l2 = l; 361 for (i = 0; i < l2; i++) { 362 m = layersArray2[i]; 363 364 // Group of layers, append children to layersArray and skip. 365 if (m instanceof L.LayerGroup) { 366 this._extractNonGroupLayers(m, layersArray2); 367 l2 = layersArray2.length; 368 continue; 369 } 370 371 this._unspiderfyLayer(m); 372 } 373 } 374 375 for (i = 0; i < l; i++) { 269 376 m = layersArray[i]; 377 378 // Group of layers, append children to layersArray and skip. 379 if (m instanceof L.LayerGroup) { 380 if (originalArray) { 381 layersArray = layersArray.slice(); 382 originalArray = false; 383 } 384 this._extractNonGroupLayers(m, layersArray); 385 l = layersArray.length; 386 continue; 387 } 270 388 271 389 if (!m.__parent) { 272 390 npg.removeLayer(m); 391 this.fire('layerremove', { layer: m }); 273 392 continue; 274 393 } 275 394 276 395 this._removeLayer(m, true, true); 396 this.fire('layerremove', { layer: m }); 277 397 278 398 if (fg.hasLayer(m)) { 279 399 fg.removeLayer(m); 280 if (m.setOpacity) { 281 m.setOpacity(1); 282 } 283 } 284 } 400 if (m.clusterShow) { 401 m.clusterShow(); 402 } 403 } 404 } 405 406 // Refresh bounds and weighted positions. 407 this._topClusterLevel._recalculateBounds(); 408 409 this._refreshClustersIcons(); 285 410 286 411 //Fix up the clusters and markers on the map 287 412 this._topClusterLevel._recursivelyAddChildrenToMap(null, this._zoom, this._currentShownBounds); 288 289 fg.eachLayer(function (c) {290 if (c instanceof L.MarkerCluster) {291 c._updateIcon();292 }293 });294 413 295 414 return this; … … 316 435 317 436 this.eachLayer(function (marker) { 437 marker.off(this._childMarkerEventHandlers, this); 318 438 delete marker.__parent; 319 } );439 }, this); 320 440 321 441 if (this._map) { … … 347 467 eachLayer: function (method, context) { 348 468 var markers = this._needsClustering.slice(), 349 i; 469 needsRemoving = this._needsRemoving, 470 thisNeedsRemoving, i, j; 350 471 351 472 if (this._topClusterLevel) { … … 354 475 355 476 for (i = markers.length - 1; i >= 0; i--) { 356 method.call(context, markers[i]); 477 thisNeedsRemoving = true; 478 479 for (j = needsRemoving.length - 1; j >= 0; j--) { 480 if (needsRemoving[j].layer === markers[i]) { 481 thisNeedsRemoving = false; 482 break; 483 } 484 } 485 486 if (thisNeedsRemoving) { 487 method.call(context, markers[i]); 488 } 357 489 } 358 490 … … 372 504 getLayer: function (id) { 373 505 var result = null; 506 507 id = parseInt(id, 10); 374 508 375 509 this.eachLayer(function (l) { … … 398 532 anArray = this._needsRemoving; 399 533 for (i = anArray.length - 1; i >= 0; i--) { 400 if (anArray[i] === layer) {534 if (anArray[i].layer === layer) { 401 535 return false; 402 536 } … … 408 542 //Zoom down to show the given layer (spiderfying if necessary) then calls the callback 409 543 zoomToShowLayer: function (layer, callback) { 544 545 if (typeof callback !== 'function') { 546 callback = function () {}; 547 } 410 548 411 549 var showMarker = function () { … … 417 555 callback(); 418 556 } else if (layer.__parent._icon) { 419 var afterSpiderfy = function () { 420 this.off('spiderfied', afterSpiderfy, this); 421 callback(); 422 }; 423 424 this.on('spiderfied', afterSpiderfy, this); 557 this.once('spiderfied', callback, this); 425 558 layer.__parent.spiderfy(); 426 559 } … … 431 564 //Layer is visible ond on screen, immediate return 432 565 callback(); 433 } else if (layer.__parent._zoom < this._map.getZoom()) {566 } else if (layer.__parent._zoom < Math.round(this._map._zoom)) { 434 567 //Layer should be visible at this zoom level. It must not be on screen so just pan over to it 435 568 this._map.on('moveend', showMarker, this); 436 569 this._map.panTo(layer.getLatLng()); 437 570 } else { 438 var moveStart = function () {439 this._map.off('movestart', moveStart, this);440 moveStart = null;441 };442 443 this._map.on('movestart', moveStart, this);444 571 this._map.on('moveend', showMarker, this); 445 572 this.on('animationend', showMarker, this); 446 573 layer.__parent.zoomToBounds(); 447 448 if (moveStart) {449 //Never started moving, must already be there, probably need clustering however450 showMarker.call(this);451 }452 574 } 453 575 }, … … 462 584 } 463 585 464 this._featureGroup. onAdd(map);465 this._nonPointGroup. onAdd(map);586 this._featureGroup.addTo(map); 587 this._nonPointGroup.addTo(map); 466 588 467 589 if (!this._gridClusters) { … … 469 591 } 470 592 593 this._maxLat = map.options.crs.projection.MAX_LATITUDE; 594 595 //Restore all the positions as they are in the MCG before removing them 471 596 for (i = 0, l = this._needsRemoving.length; i < l; i++) { 472 597 layer = this._needsRemoving[i]; 473 this._removeLayer(layer, true); 598 layer.newlatlng = layer.layer._latlng; 599 layer.layer._latlng = layer.latlng; 600 } 601 //Remove them, then restore their new positions 602 for (i = 0, l = this._needsRemoving.length; i < l; i++) { 603 layer = this._needsRemoving[i]; 604 this._removeLayer(layer.layer, true); 605 layer.layer._latlng = layer.newlatlng; 474 606 } 475 607 this._needsRemoving = []; 476 608 477 609 //Remember the current zoom level and bounds 478 this._zoom = this._map.getZoom();610 this._zoom = Math.round(this._map._zoom); 479 611 this._currentShownBounds = this._getExpandedVisibleBounds(); 480 612 … … 491 623 l = this._needsClustering; 492 624 this._needsClustering = []; 493 this.addLayers(l );625 this.addLayers(l, true); 494 626 }, 495 627 … … 508 640 } 509 641 510 642 delete this._maxLat; 511 643 512 644 //Clean up all the layers we added to the map 513 645 this._hideCoverage(); 514 this._featureGroup. onRemove(map);515 this._nonPointGroup. onRemove(map);646 this._featureGroup.remove(); 647 this._nonPointGroup.remove(); 516 648 517 649 this._featureGroup.clearLayers(); … … 538 670 }, 539 671 672 /** 673 * Removes a marker from all _gridUnclustered zoom levels, starting at the supplied zoom. 674 * @param marker to be removed from _gridUnclustered. 675 * @param z integer bottom start zoom level (included) 676 * @private 677 */ 678 _removeFromGridUnclustered: function (marker, z) { 679 var map = this._map, 680 gridUnclustered = this._gridUnclustered, 681 minZoom = Math.floor(this._map.getMinZoom()); 682 683 for (; z >= minZoom; z--) { 684 if (!gridUnclustered[z].removeObject(marker, map.project(marker.getLatLng(), z))) { 685 break; 686 } 687 } 688 }, 689 690 _childMarkerDragStart: function (e) { 691 e.target.__dragStart = e.target._latlng; 692 }, 693 694 _childMarkerMoved: function (e) { 695 if (!this._ignoreMove && !e.target.__dragStart) { 696 var isPopupOpen = e.target._popup && e.target._popup.isOpen(); 697 698 this._moveChild(e.target, e.oldLatLng, e.latlng); 699 700 if (isPopupOpen) { 701 e.target.openPopup(); 702 } 703 } 704 }, 705 706 _moveChild: function (layer, from, to) { 707 layer._latlng = from; 708 this.removeLayer(layer); 709 710 layer._latlng = to; 711 this.addLayer(layer); 712 }, 713 714 _childMarkerDragEnd: function (e) { 715 if (e.target.__dragStart) { 716 this._moveChild(e.target, e.target.__dragStart, e.target._latlng); 717 } 718 delete e.target.__dragStart; 719 }, 720 721 540 722 //Internal function for removing a marker from everything. 541 723 //dontUpdateMap: set to true if you will handle updating the map manually (for bulk functions) … … 544 726 gridUnclustered = this._gridUnclustered, 545 727 fg = this._featureGroup, 546 map = this._map; 728 map = this._map, 729 minZoom = Math.floor(this._map.getMinZoom()); 547 730 548 731 //Remove the marker from distance clusters it might be in 549 732 if (removeFromDistanceGrid) { 550 for (var z = this._maxZoom; z >= 0; z--) { 551 if (!gridUnclustered[z].removeObject(marker, map.project(marker.getLatLng(), z))) { 552 break; 553 } 554 } 733 this._removeFromGridUnclustered(marker, this._maxZoom); 555 734 } 556 735 … … 565 744 while (cluster) { 566 745 cluster._childCount--; 567 568 if (cluster._zoom < 0) { 746 cluster._boundsNeedUpdate = true; 747 748 if (cluster._zoom < minZoom) { 569 749 //Top level, do nothing 570 750 break; … … 590 770 } 591 771 } else { 592 cluster._recalculateBounds(); 593 if (!dontUpdateMap || !cluster._icon) { 594 cluster._updateIcon(); 595 } 772 cluster._iconNeedsUpdate = true; 596 773 } 597 774 … … 612 789 }, 613 790 614 _propagateEvent: function (e) { 615 if (e.layer instanceof L.MarkerCluster) { 791 //Override L.Evented.fire 792 fire: function (type, data, propagate) { 793 if (data && data.layer instanceof L.MarkerCluster) { 616 794 //Prevent multiple clustermouseover/off events if the icon is made up of stacked divs (Doesn't work in ie <= 8, no relatedTarget) 617 if ( e.originalEvent && this._isOrIsParent(e.layer._icon, e.originalEvent.relatedTarget)) {795 if (data.originalEvent && this._isOrIsParent(data.layer._icon, data.originalEvent.relatedTarget)) { 618 796 return; 619 797 } 620 e.type = 'cluster' + e.type; 621 } 622 623 this.fire(e.type, e); 798 type = 'cluster' + type; 799 } 800 801 L.FeatureGroup.prototype.fire.call(this, type, data, propagate); 802 }, 803 804 //Override L.Evented.listens 805 listens: function (type, propagate) { 806 return L.FeatureGroup.prototype.listens.call(this, type, propagate) || L.FeatureGroup.prototype.listens.call(this, 'cluster' + type, propagate); 624 807 }, 625 808 … … 660 843 661 844 _zoomOrSpiderfy: function (e) { 662 var map = this._map; 663 if (map.getMaxZoom() === map.getZoom()) { 664 if (this.options.spiderfyOnMaxZoom) { 665 e.layer.spiderfy(); 666 } 845 var cluster = e.layer, 846 bottomCluster = cluster; 847 848 while (bottomCluster._childClusters.length === 1) { 849 bottomCluster = bottomCluster._childClusters[0]; 850 } 851 852 if (bottomCluster._zoom === this._maxZoom && 853 bottomCluster._childCount === cluster._childCount && 854 this.options.spiderfyOnMaxZoom) { 855 856 // All child markers are contained in a single cluster from this._maxZoom to this cluster. 857 cluster.spiderfy(); 667 858 } else if (this.options.zoomToBoundsOnClick) { 668 e.layer.zoomToBounds();859 cluster.zoomToBounds(); 669 860 } 670 861 671 862 // Focus the map again for keyboard users. 672 863 if (e.originalEvent && e.originalEvent.keyCode === 13) { 673 map._container.focus();864 this._map._container.focus(); 674 865 } 675 866 }, … … 718 909 this._mergeSplitClusters(); 719 910 720 this._zoom = this._map._zoom;911 this._zoom = Math.round(this._map._zoom); 721 912 this._currentShownBounds = this._getExpandedVisibleBounds(); 722 913 }, … … 729 920 var newBounds = this._getExpandedVisibleBounds(); 730 921 731 this._topClusterLevel._recursivelyRemoveChildrenFromMap(this._currentShownBounds, this._zoom, newBounds);732 this._topClusterLevel._recursivelyAddChildrenToMap(null, this._map._zoom, newBounds);922 this._topClusterLevel._recursivelyRemoveChildrenFromMap(this._currentShownBounds, Math.floor(this._map.getMinZoom()), this._zoom, newBounds); 923 this._topClusterLevel._recursivelyAddChildrenToMap(null, Math.round(this._map._zoom), newBounds); 733 924 734 925 this._currentShownBounds = newBounds; … … 737 928 738 929 _generateInitialClusters: function () { 739 var maxZoom = this._map.getMaxZoom(), 930 var maxZoom = Math.ceil(this._map.getMaxZoom()), 931 minZoom = Math.floor(this._map.getMinZoom()), 740 932 radius = this.options.maxClusterRadius, 741 933 radiusFn = radius; … … 748 940 } 749 941 750 if (this.options.disableClusteringAtZoom ) {942 if (this.options.disableClusteringAtZoom !== null) { 751 943 maxZoom = this.options.disableClusteringAtZoom - 1; 752 944 } … … 756 948 757 949 //Set up DistanceGrids for each zoom 758 for (var zoom = maxZoom; zoom >= 0; zoom--) {950 for (var zoom = maxZoom; zoom >= minZoom; zoom--) { 759 951 this._gridClusters[zoom] = new L.DistanceGrid(radiusFn(zoom)); 760 952 this._gridUnclustered[zoom] = new L.DistanceGrid(radiusFn(zoom)); 761 953 } 762 954 763 this._topClusterLevel = new L.MarkerCluster(this, -1); 955 // Instantiate the appropriate L.MarkerCluster class (animated or not). 956 this._topClusterLevel = new this._markerCluster(this, minZoom - 1); 764 957 }, 765 958 … … 768 961 var gridClusters = this._gridClusters, 769 962 gridUnclustered = this._gridUnclustered, 963 minZoom = Math.floor(this._map.getMinZoom()), 770 964 markerPoint, z; 771 965 772 966 if (this.options.singleMarkerMode) { 773 layer.options.icon = this.options.iconCreateFunction({ 774 getChildCount: function () { 775 return 1; 776 }, 777 getAllChildMarkers: function () { 778 return [layer]; 779 } 780 }); 781 } 967 this._overrideMarkerIcon(layer); 968 } 969 970 layer.on(this._childMarkerEventHandlers, this); 782 971 783 972 //Find the lowest zoom level to slot this one in 784 for (; zoom >= 0; zoom--) {973 for (; zoom >= minZoom; zoom--) { 785 974 markerPoint = this._map.project(layer.getLatLng(), zoom); // calculate pixel position 786 975 … … 803 992 //Create new cluster with these 2 in it 804 993 805 var newCluster = new L.MarkerCluster(this, zoom, closest, layer);994 var newCluster = new this._markerCluster(this, zoom, closest, layer); 806 995 gridClusters[zoom].addObject(newCluster, this._map.project(newCluster._cLatLng, zoom)); 807 996 closest.__parent = newCluster; … … 811 1000 var lastParent = newCluster; 812 1001 for (z = zoom - 1; z > parent._zoom; z--) { 813 lastParent = new L.MarkerCluster(this, z, lastParent);1002 lastParent = new this._markerCluster(this, z, lastParent); 814 1003 gridClusters[z].addObject(lastParent, this._map.project(closest.getLatLng(), z)); 815 1004 } … … 817 1006 818 1007 //Remove closest from this zoom level and any above that it is in, replace with newCluster 819 for (z = zoom; z >= 0; z--) { 820 if (!gridUnclustered[z].removeObject(closest, this._map.project(closest.getLatLng(), z))) { 821 break; 822 } 823 } 1008 this._removeFromGridUnclustered(closest, zoom); 824 1009 825 1010 return; … … 834 1019 layer.__parent = this._topClusterLevel; 835 1020 return; 1021 }, 1022 1023 /** 1024 * Refreshes the icon of all "dirty" visible clusters. 1025 * Non-visible "dirty" clusters will be updated when they are added to the map. 1026 * @private 1027 */ 1028 _refreshClustersIcons: function () { 1029 this._featureGroup.eachLayer(function (c) { 1030 if (c instanceof L.MarkerCluster && c._iconNeedsUpdate) { 1031 c._updateIcon(); 1032 } 1033 }); 836 1034 }, 837 1035 … … 854 1052 //Merge and split any existing clusters that are too big or small 855 1053 _mergeSplitClusters: function () { 856 857 //Incase we are starting to split before the animation finished 1054 var mapZoom = Math.round(this._map._zoom); 1055 1056 //In case we are starting to split before the animation finished 858 1057 this._processQueue(); 859 1058 860 if (this._zoom < this._map._zoom && this._currentShownBounds.intersects(this._getExpandedVisibleBounds())) { //Zoom in, split1059 if (this._zoom < mapZoom && this._currentShownBounds.intersects(this._getExpandedVisibleBounds())) { //Zoom in, split 861 1060 this._animationStart(); 862 1061 //Remove clusters now off screen 863 this._topClusterLevel._recursivelyRemoveChildrenFromMap(this._currentShownBounds, this._zoom, this._getExpandedVisibleBounds());864 865 this._animationZoomIn(this._zoom, this._map._zoom);866 867 } else if (this._zoom > this._map._zoom) { //Zoom out, merge1062 this._topClusterLevel._recursivelyRemoveChildrenFromMap(this._currentShownBounds, Math.floor(this._map.getMinZoom()), this._zoom, this._getExpandedVisibleBounds()); 1063 1064 this._animationZoomIn(this._zoom, mapZoom); 1065 1066 } else if (this._zoom > mapZoom) { //Zoom out, merge 868 1067 this._animationStart(); 869 1068 870 this._animationZoomOut(this._zoom, this._map._zoom);1069 this._animationZoomOut(this._zoom, mapZoom); 871 1070 } else { 872 1071 this._moveEnd(); … … 877 1076 _getExpandedVisibleBounds: function () { 878 1077 if (!this.options.removeOutsideVisibleBounds) { 879 return this.getBounds(); 880 } 881 882 var map = this._map, 883 bounds = map.getBounds(), 884 sw = bounds._southWest, 885 ne = bounds._northEast, 886 latDiff = L.Browser.mobile ? 0 : Math.abs(sw.lat - ne.lat), 887 lngDiff = L.Browser.mobile ? 0 : Math.abs(sw.lng - ne.lng); 888 889 return new L.LatLngBounds( 890 new L.LatLng(sw.lat - latDiff, sw.lng - lngDiff, true), 891 new L.LatLng(ne.lat + latDiff, ne.lng + lngDiff, true)); 1078 return this._mapBoundsInfinite; 1079 } else if (L.Browser.mobile) { 1080 return this._checkBoundsMaxLat(this._map.getBounds()); 1081 } 1082 1083 return this._checkBoundsMaxLat(this._map.getBounds().pad(1)); // Padding expands the bounds by its own dimensions but scaled with the given factor. 1084 }, 1085 1086 /** 1087 * Expands the latitude to Infinity (or -Infinity) if the input bounds reach the map projection maximum defined latitude 1088 * (in the case of Web/Spherical Mercator, it is 85.0511287798 / see https://en.wikipedia.org/wiki/Web_Mercator#Formulas). 1089 * Otherwise, the removeOutsideVisibleBounds option will remove markers beyond that limit, whereas the same markers without 1090 * this option (or outside MCG) will have their position floored (ceiled) by the projection and rendered at that limit, 1091 * making the user think that MCG "eats" them and never displays them again. 1092 * @param bounds L.LatLngBounds 1093 * @returns {L.LatLngBounds} 1094 * @private 1095 */ 1096 _checkBoundsMaxLat: function (bounds) { 1097 var maxLat = this._maxLat; 1098 1099 if (maxLat !== undefined) { 1100 if (bounds.getNorth() >= maxLat) { 1101 bounds._northEast.lat = Infinity; 1102 } 1103 if (bounds.getSouth() <= -maxLat) { 1104 bounds._southWest.lat = -Infinity; 1105 } 1106 } 1107 1108 return bounds; 892 1109 }, 893 1110 … … 905 1122 newCluster._updateIcon(); 906 1123 } 1124 }, 1125 1126 /** 1127 * Extracts individual (i.e. non-group) layers from a Layer Group. 1128 * @param group to extract layers from. 1129 * @param output {Array} in which to store the extracted layers. 1130 * @returns {*|Array} 1131 * @private 1132 */ 1133 _extractNonGroupLayers: function (group, output) { 1134 var layers = group.getLayers(), 1135 i = 0, 1136 layer; 1137 1138 output = output || []; 1139 1140 for (; i < layers.length; i++) { 1141 layer = layers[i]; 1142 1143 if (layer instanceof L.LayerGroup) { 1144 this._extractNonGroupLayers(layer, output); 1145 continue; 1146 } 1147 1148 output.push(layer); 1149 } 1150 1151 return output; 1152 }, 1153 1154 /** 1155 * Implements the singleMarkerMode option. 1156 * @param layer Marker to re-style using the Clusters iconCreateFunction. 1157 * @returns {L.Icon} The newly created icon. 1158 * @private 1159 */ 1160 _overrideMarkerIcon: function (layer) { 1161 var icon = layer.options.icon = this.options.iconCreateFunction({ 1162 getChildCount: function () { 1163 return 1; 1164 }, 1165 getAllChildMarkers: function () { 1166 return [layer]; 1167 } 1168 }); 1169 1170 return icon; 907 1171 } 908 1172 }); 909 1173 910 L.MarkerClusterGroup.include(!L.DomUtil.TRANSITION ? { 911 912 //Non Animated versions of everything 913 _animationStart: function () { 914 //Do nothing... 915 }, 916 _animationZoomIn: function (previousZoomLevel, newZoomLevel) { 917 this._topClusterLevel._recursivelyRemoveChildrenFromMap(this._currentShownBounds, previousZoomLevel); 918 this._topClusterLevel._recursivelyAddChildrenToMap(null, newZoomLevel, this._getExpandedVisibleBounds()); 919 920 //We didn't actually animate, but we use this event to mean "clustering animations have finished" 921 this.fire('animationend'); 922 }, 923 _animationZoomOut: function (previousZoomLevel, newZoomLevel) { 924 this._topClusterLevel._recursivelyRemoveChildrenFromMap(this._currentShownBounds, previousZoomLevel); 925 this._topClusterLevel._recursivelyAddChildrenToMap(null, newZoomLevel, this._getExpandedVisibleBounds()); 926 927 //We didn't actually animate, but we use this event to mean "clustering animations have finished" 928 this.fire('animationend'); 929 }, 930 _animationAddLayer: function (layer, newCluster) { 931 this._animationAddLayerNonAnimated(layer, newCluster); 932 } 933 } : { 934 935 //Animated versions here 936 _animationStart: function () { 937 this._map._mapPane.className += ' leaflet-cluster-anim'; 938 this._inZoomAnimation++; 939 }, 940 _animationEnd: function () { 941 if (this._map) { 942 this._map._mapPane.className = this._map._mapPane.className.replace(' leaflet-cluster-anim', ''); 943 } 944 this._inZoomAnimation--; 945 this.fire('animationend'); 946 }, 947 _animationZoomIn: function (previousZoomLevel, newZoomLevel) { 1174 // Constant bounds used in case option "removeOutsideVisibleBounds" is set to false. 1175 L.MarkerClusterGroup.include({ 1176 _mapBoundsInfinite: new L.LatLngBounds(new L.LatLng(-Infinity, -Infinity), new L.LatLng(Infinity, Infinity)) 1177 }); 1178 1179 L.MarkerClusterGroup.include({ 1180 _noAnimation: { 1181 //Non Animated versions of everything 1182 _animationStart: function () { 1183 //Do nothing... 1184 }, 1185 _animationZoomIn: function (previousZoomLevel, newZoomLevel) { 1186 this._topClusterLevel._recursivelyRemoveChildrenFromMap(this._currentShownBounds, Math.floor(this._map.getMinZoom()), previousZoomLevel); 1187 this._topClusterLevel._recursivelyAddChildrenToMap(null, newZoomLevel, this._getExpandedVisibleBounds()); 1188 1189 //We didn't actually animate, but we use this event to mean "clustering animations have finished" 1190 this.fire('animationend'); 1191 }, 1192 _animationZoomOut: function (previousZoomLevel, newZoomLevel) { 1193 this._topClusterLevel._recursivelyRemoveChildrenFromMap(this._currentShownBounds, Math.floor(this._map.getMinZoom()), previousZoomLevel); 1194 this._topClusterLevel._recursivelyAddChildrenToMap(null, newZoomLevel, this._getExpandedVisibleBounds()); 1195 1196 //We didn't actually animate, but we use this event to mean "clustering animations have finished" 1197 this.fire('animationend'); 1198 }, 1199 _animationAddLayer: function (layer, newCluster) { 1200 this._animationAddLayerNonAnimated(layer, newCluster); 1201 } 1202 }, 1203 1204 _withAnimation: { 1205 //Animated versions here 1206 _animationStart: function () { 1207 this._map._mapPane.className += ' leaflet-cluster-anim'; 1208 this._inZoomAnimation++; 1209 }, 1210 1211 _animationZoomIn: function (previousZoomLevel, newZoomLevel) { 1212 var bounds = this._getExpandedVisibleBounds(), 1213 fg = this._featureGroup, 1214 minZoom = Math.floor(this._map.getMinZoom()), 1215 i; 1216 1217 this._ignoreMove = true; 1218 1219 //Add all children of current clusters to map and remove those clusters from map 1220 this._topClusterLevel._recursively(bounds, previousZoomLevel, minZoom, function (c) { 1221 var startPos = c._latlng, 1222 markers = c._markers, 1223 m; 1224 1225 if (!bounds.contains(startPos)) { 1226 startPos = null; 1227 } 1228 1229 if (c._isSingleParent() && previousZoomLevel + 1 === newZoomLevel) { //Immediately add the new child and remove us 1230 fg.removeLayer(c); 1231 c._recursivelyAddChildrenToMap(null, newZoomLevel, bounds); 1232 } else { 1233 //Fade out old cluster 1234 c.clusterHide(); 1235 c._recursivelyAddChildrenToMap(startPos, newZoomLevel, bounds); 1236 } 1237 1238 //Remove all markers that aren't visible any more 1239 //TODO: Do we actually need to do this on the higher levels too? 1240 for (i = markers.length - 1; i >= 0; i--) { 1241 m = markers[i]; 1242 if (!bounds.contains(m._latlng)) { 1243 fg.removeLayer(m); 1244 } 1245 } 1246 1247 }); 1248 1249 this._forceLayout(); 1250 1251 //Update opacities 1252 this._topClusterLevel._recursivelyBecomeVisible(bounds, newZoomLevel); 1253 //TODO Maybe? Update markers in _recursivelyBecomeVisible 1254 fg.eachLayer(function (n) { 1255 if (!(n instanceof L.MarkerCluster) && n._icon) { 1256 n.clusterShow(); 1257 } 1258 }); 1259 1260 //update the positions of the just added clusters/markers 1261 this._topClusterLevel._recursively(bounds, previousZoomLevel, newZoomLevel, function (c) { 1262 c._recursivelyRestoreChildPositions(newZoomLevel); 1263 }); 1264 1265 this._ignoreMove = false; 1266 1267 //Remove the old clusters and close the zoom animation 1268 this._enqueue(function () { 1269 //update the positions of the just added clusters/markers 1270 this._topClusterLevel._recursively(bounds, previousZoomLevel, minZoom, function (c) { 1271 fg.removeLayer(c); 1272 c.clusterShow(); 1273 }); 1274 1275 this._animationEnd(); 1276 }); 1277 }, 1278 1279 _animationZoomOut: function (previousZoomLevel, newZoomLevel) { 1280 this._animationZoomOutSingle(this._topClusterLevel, previousZoomLevel - 1, newZoomLevel); 1281 1282 //Need to add markers for those that weren't on the map before but are now 1283 this._topClusterLevel._recursivelyAddChildrenToMap(null, newZoomLevel, this._getExpandedVisibleBounds()); 1284 //Remove markers that were on the map before but won't be now 1285 this._topClusterLevel._recursivelyRemoveChildrenFromMap(this._currentShownBounds, Math.floor(this._map.getMinZoom()), previousZoomLevel, this._getExpandedVisibleBounds()); 1286 }, 1287 1288 _animationAddLayer: function (layer, newCluster) { 1289 var me = this, 1290 fg = this._featureGroup; 1291 1292 fg.addLayer(layer); 1293 if (newCluster !== layer) { 1294 if (newCluster._childCount > 2) { //Was already a cluster 1295 1296 newCluster._updateIcon(); 1297 this._forceLayout(); 1298 this._animationStart(); 1299 1300 layer._setPos(this._map.latLngToLayerPoint(newCluster.getLatLng())); 1301 layer.clusterHide(); 1302 1303 this._enqueue(function () { 1304 fg.removeLayer(layer); 1305 layer.clusterShow(); 1306 1307 me._animationEnd(); 1308 }); 1309 1310 } else { //Just became a cluster 1311 this._forceLayout(); 1312 1313 me._animationStart(); 1314 me._animationZoomOutSingle(newCluster, this._map.getMaxZoom(), this._zoom); 1315 } 1316 } 1317 } 1318 }, 1319 1320 // Private methods for animated versions. 1321 _animationZoomOutSingle: function (cluster, previousZoomLevel, newZoomLevel) { 948 1322 var bounds = this._getExpandedVisibleBounds(), 949 fg = this._featureGroup, 950 i; 951 952 //Add all children of current clusters to map and remove those clusters from map 953 this._topClusterLevel._recursively(bounds, previousZoomLevel, 0, function (c) { 954 var startPos = c._latlng, 955 markers = c._markers, 956 m; 957 958 if (!bounds.contains(startPos)) { 959 startPos = null; 960 } 961 962 if (c._isSingleParent() && previousZoomLevel + 1 === newZoomLevel) { //Immediately add the new child and remove us 963 fg.removeLayer(c); 964 c._recursivelyAddChildrenToMap(null, newZoomLevel, bounds); 965 } else { 966 //Fade out old cluster 967 c.setOpacity(0); 968 c._recursivelyAddChildrenToMap(startPos, newZoomLevel, bounds); 969 } 970 971 //Remove all markers that aren't visible any more 972 //TODO: Do we actually need to do this on the higher levels too? 973 for (i = markers.length - 1; i >= 0; i--) { 974 m = markers[i]; 975 if (!bounds.contains(m._latlng)) { 976 fg.removeLayer(m); 977 } 978 } 979 980 }); 981 982 this._forceLayout(); 983 984 //Update opacities 985 this._topClusterLevel._recursivelyBecomeVisible(bounds, newZoomLevel); 986 //TODO Maybe? Update markers in _recursivelyBecomeVisible 987 fg.eachLayer(function (n) { 988 if (!(n instanceof L.MarkerCluster) && n._icon) { 989 n.setOpacity(1); 990 } 991 }); 992 993 //update the positions of the just added clusters/markers 994 this._topClusterLevel._recursively(bounds, previousZoomLevel, newZoomLevel, function (c) { 995 c._recursivelyRestoreChildPositions(newZoomLevel); 996 }); 997 998 //Remove the old clusters and close the zoom animation 999 this._enqueue(function () { 1000 //update the positions of the just added clusters/markers 1001 this._topClusterLevel._recursively(bounds, previousZoomLevel, 0, function (c) { 1002 fg.removeLayer(c); 1003 c.setOpacity(1); 1004 }); 1005 1006 this._animationEnd(); 1007 }); 1008 }, 1009 1010 _animationZoomOut: function (previousZoomLevel, newZoomLevel) { 1011 this._animationZoomOutSingle(this._topClusterLevel, previousZoomLevel - 1, newZoomLevel); 1012 1013 //Need to add markers for those that weren't on the map before but are now 1014 this._topClusterLevel._recursivelyAddChildrenToMap(null, newZoomLevel, this._getExpandedVisibleBounds()); 1015 //Remove markers that were on the map before but won't be now 1016 this._topClusterLevel._recursivelyRemoveChildrenFromMap(this._currentShownBounds, previousZoomLevel, this._getExpandedVisibleBounds()); 1017 }, 1018 _animationZoomOutSingle: function (cluster, previousZoomLevel, newZoomLevel) { 1019 var bounds = this._getExpandedVisibleBounds(); 1323 minZoom = Math.floor(this._map.getMinZoom()); 1020 1324 1021 1325 //Animate all of the markers in the clusters to move to their cluster center point 1022 cluster._recursivelyAnimateChildrenInAndAddSelfToMap(bounds, previousZoomLevel + 1, newZoomLevel);1326 cluster._recursivelyAnimateChildrenInAndAddSelfToMap(bounds, minZoom, previousZoomLevel + 1, newZoomLevel); 1023 1327 1024 1328 var me = this; … … 1036 1340 var m = cluster._markers[0]; 1037 1341 //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 1342 this._ignoreMove = true; 1038 1343 m.setLatLng(m.getLatLng()); 1039 if (m.setOpacity) { 1040 m.setOpacity(1); 1344 this._ignoreMove = false; 1345 if (m.clusterShow) { 1346 m.clusterShow(); 1041 1347 } 1042 1348 } else { 1043 cluster._recursively(bounds, newZoomLevel, 0, function (c) {1044 c._recursivelyRemoveChildrenFromMap(bounds, previousZoomLevel + 1);1349 cluster._recursively(bounds, newZoomLevel, minZoom, function (c) { 1350 c._recursivelyRemoveChildrenFromMap(bounds, minZoom, previousZoomLevel + 1); 1045 1351 }); 1046 1352 } … … 1048 1354 }); 1049 1355 }, 1050 _animationAddLayer: function (layer, newCluster) { 1051 var me = this, 1052 fg = this._featureGroup; 1053 1054 fg.addLayer(layer); 1055 if (newCluster !== layer) { 1056 if (newCluster._childCount > 2) { //Was already a cluster 1057 1058 newCluster._updateIcon(); 1059 this._forceLayout(); 1060 this._animationStart(); 1061 1062 layer._setPos(this._map.latLngToLayerPoint(newCluster.getLatLng())); 1063 layer.setOpacity(0); 1064 1065 this._enqueue(function () { 1066 fg.removeLayer(layer); 1067 layer.setOpacity(1); 1068 1069 me._animationEnd(); 1070 }); 1071 1072 } else { //Just became a cluster 1073 this._forceLayout(); 1074 1075 me._animationStart(); 1076 me._animationZoomOutSingle(newCluster, this._map.getMaxZoom(), this._map.getZoom()); 1077 } 1078 } 1356 1357 _animationEnd: function () { 1358 if (this._map) { 1359 this._map._mapPane.className = this._map._mapPane.className.replace(' leaflet-cluster-anim', ''); 1360 } 1361 this._inZoomAnimation--; 1362 this.fire('animationend'); 1079 1363 }, 1080 1364 … … 1097 1381 initialize: function (group, zoom, a, b) { 1098 1382 1099 L.Marker.prototype.initialize.call(this, a ? (a._cLatLng || a.getLatLng()) : new L.LatLng(0, 0), { icon: this });1100 1383 L.Marker.prototype.initialize.call(this, a ? (a._cLatLng || a.getLatLng()) : new L.LatLng(0, 0), 1384 { icon: this, pane: group.options.clusterPane }); 1101 1385 1102 1386 this._group = group; … … 1107 1391 this._childCount = 0; 1108 1392 this._iconNeedsUpdate = true; 1393 this._boundsNeedUpdate = true; 1109 1394 1110 1395 this._bounds = new L.LatLngBounds(); … … 1139 1424 1140 1425 //Zoom to the minimum of showing all of the child markers, or the extents of this cluster 1141 zoomToBounds: function ( ) {1426 zoomToBounds: function (fitBoundsOptions) { 1142 1427 var childClusters = this._childClusters.slice(), 1143 1428 map = this._group._map, … … 1162 1447 this._group._map.setView(this._latlng, mapZoom + 1); 1163 1448 } else { 1164 this._group._map.fitBounds(this._bounds );1449 this._group._map.fitBounds(this._bounds, fitBoundsOptions); 1165 1450 } 1166 1451 }, … … 1195 1480 1196 1481 this._iconNeedsUpdate = true; 1197 this._expandBounds(new1); 1482 1483 this._boundsNeedUpdate = true; 1484 this._setClusterCenter(new1); 1198 1485 1199 1486 if (new1 instanceof L.MarkerCluster) { … … 1215 1502 }, 1216 1503 1217 //Expand our bounds and tell our parent to 1218 _expandBounds: function (marker) { 1219 var addedCount, 1220 addedLatLng = marker._wLatLng || marker._latlng; 1221 1222 if (marker instanceof L.MarkerCluster) { 1223 this._bounds.extend(marker._bounds); 1224 addedCount = marker._childCount; 1225 } else { 1226 this._bounds.extend(addedLatLng); 1227 addedCount = 1; 1228 } 1229 1504 /** 1505 * Makes sure the cluster center is set. If not, uses the child center if it is a cluster, or the marker position. 1506 * @param child L.MarkerCluster|L.Marker that will be used as cluster center if not defined yet. 1507 * @private 1508 */ 1509 _setClusterCenter: function (child) { 1230 1510 if (!this._cLatLng) { 1231 1511 // when clustering, take position of the first point as the cluster center 1232 this._cLatLng = marker._cLatLng || addedLatLng; 1233 } 1234 1235 // when showing clusters, take weighted average of all points as cluster center 1236 var totalCount = this._childCount + addedCount; 1237 1238 //Calculate weighted latlng for display 1239 if (!this._wLatLng) { 1240 this._latlng = this._wLatLng = new L.LatLng(addedLatLng.lat, addedLatLng.lng); 1241 } else { 1242 this._wLatLng.lat = (addedLatLng.lat * addedCount + this._wLatLng.lat * this._childCount) / totalCount; 1243 this._wLatLng.lng = (addedLatLng.lng * addedCount + this._wLatLng.lng * this._childCount) / totalCount; 1244 } 1512 this._cLatLng = child._cLatLng || child._latlng; 1513 } 1514 }, 1515 1516 /** 1517 * Assigns impossible bounding values so that the next extend entirely determines the new bounds. 1518 * This method avoids having to trash the previous L.LatLngBounds object and to create a new one, which is much slower for this class. 1519 * As long as the bounds are not extended, most other methods would probably fail, as they would with bounds initialized but not extended. 1520 * @private 1521 */ 1522 _resetBounds: function () { 1523 var bounds = this._bounds; 1524 1525 if (bounds._southWest) { 1526 bounds._southWest.lat = Infinity; 1527 bounds._southWest.lng = Infinity; 1528 } 1529 if (bounds._northEast) { 1530 bounds._northEast.lat = -Infinity; 1531 bounds._northEast.lng = -Infinity; 1532 } 1533 }, 1534 1535 _recalculateBounds: function () { 1536 var markers = this._markers, 1537 childClusters = this._childClusters, 1538 latSum = 0, 1539 lngSum = 0, 1540 totalCount = this._childCount, 1541 i, child, childLatLng, childCount; 1542 1543 // Case where all markers are removed from the map and we are left with just an empty _topClusterLevel. 1544 if (totalCount === 0) { 1545 return; 1546 } 1547 1548 // Reset rather than creating a new object, for performance. 1549 this._resetBounds(); 1550 1551 // Child markers. 1552 for (i = 0; i < markers.length; i++) { 1553 childLatLng = markers[i]._latlng; 1554 1555 this._bounds.extend(childLatLng); 1556 1557 latSum += childLatLng.lat; 1558 lngSum += childLatLng.lng; 1559 } 1560 1561 // Child clusters. 1562 for (i = 0; i < childClusters.length; i++) { 1563 child = childClusters[i]; 1564 1565 // Re-compute child bounds and weighted position first if necessary. 1566 if (child._boundsNeedUpdate) { 1567 child._recalculateBounds(); 1568 } 1569 1570 this._bounds.extend(child._bounds); 1571 1572 childLatLng = child._wLatLng; 1573 childCount = child._childCount; 1574 1575 latSum += childLatLng.lat * childCount; 1576 lngSum += childLatLng.lng * childCount; 1577 } 1578 1579 this._latlng = this._wLatLng = new L.LatLng(latSum / totalCount, lngSum / totalCount); 1580 1581 // Reset dirty flag. 1582 this._boundsNeedUpdate = false; 1245 1583 }, 1246 1584 … … 1255 1593 1256 1594 _recursivelyAnimateChildrenIn: function (bounds, center, maxZoom) { 1257 this._recursively(bounds, 0, maxZoom - 1,1595 this._recursively(bounds, this._group._map.getMinZoom(), maxZoom - 1, 1258 1596 function (c) { 1259 1597 var markers = c._markers, … … 1265 1603 if (m._icon) { 1266 1604 m._setPos(center); 1267 m. setOpacity(0);1605 m.clusterHide(); 1268 1606 } 1269 1607 } … … 1276 1614 if (cm._icon) { 1277 1615 cm._setPos(center); 1278 cm. setOpacity(0);1616 cm.clusterHide(); 1279 1617 } 1280 1618 } … … 1283 1621 }, 1284 1622 1285 _recursivelyAnimateChildrenInAndAddSelfToMap: function (bounds, previousZoomLevel, newZoomLevel) {1286 this._recursively(bounds, newZoomLevel, 0,1623 _recursivelyAnimateChildrenInAndAddSelfToMap: function (bounds, mapMinZoom, previousZoomLevel, newZoomLevel) { 1624 this._recursively(bounds, newZoomLevel, mapMinZoom, 1287 1625 function (c) { 1288 1626 c._recursivelyAnimateChildrenIn(bounds, c._group._map.latLngToLayerPoint(c.getLatLng()).round(), previousZoomLevel); … … 1291 1629 //As a hack we only do a animation free zoom on a single level zoom, if someone does multiple levels then we always animate 1292 1630 if (c._isSingleParent() && previousZoomLevel - 1 === newZoomLevel) { 1293 c. setOpacity(1);1294 c._recursivelyRemoveChildrenFromMap(bounds, previousZoomLevel); //Immediately remove our children as we are replacing them. TODO previousBounds not bounds1631 c.clusterShow(); 1632 c._recursivelyRemoveChildrenFromMap(bounds, mapMinZoom, previousZoomLevel); //Immediately remove our children as we are replacing them. TODO previousBounds not bounds 1295 1633 } else { 1296 c. setOpacity(0);1634 c.clusterHide(); 1297 1635 } 1298 1636 … … 1303 1641 1304 1642 _recursivelyBecomeVisible: function (bounds, zoomLevel) { 1305 this._recursively(bounds, 0, zoomLevel, null, function (c) {1306 c. setOpacity(1);1643 this._recursively(bounds, this._group._map.getMinZoom(), zoomLevel, null, function (c) { 1644 c.clusterShow(); 1307 1645 }); 1308 1646 }, 1309 1647 1310 1648 _recursivelyAddChildrenToMap: function (startPos, zoomLevel, bounds) { 1311 this._recursively(bounds, -1, zoomLevel,1649 this._recursively(bounds, this._group._map.getMinZoom() - 1, zoomLevel, 1312 1650 function (c) { 1313 1651 if (zoomLevel === c._zoom) { … … 1327 1665 1328 1666 nm.setLatLng(startPos); 1329 if (nm. setOpacity) {1330 nm. setOpacity(0);1667 if (nm.clusterHide) { 1668 nm.clusterHide(); 1331 1669 } 1332 1670 } … … 1371 1709 1372 1710 //exceptBounds: If set, don't remove any markers/clusters in it 1373 _recursivelyRemoveChildrenFromMap: function (previousBounds, zoomLevel, exceptBounds) {1711 _recursivelyRemoveChildrenFromMap: function (previousBounds, mapMinZoom, zoomLevel, exceptBounds) { 1374 1712 var m, i; 1375 this._recursively(previousBounds, -1, zoomLevel - 1,1713 this._recursively(previousBounds, mapMinZoom - 1, zoomLevel - 1, 1376 1714 function (c) { 1377 1715 //Remove markers at every level … … 1380 1718 if (!exceptBounds || !exceptBounds.contains(m._latlng)) { 1381 1719 c._group._featureGroup.removeLayer(m); 1382 if (m. setOpacity) {1383 m. setOpacity(1);1720 if (m.clusterShow) { 1721 m.clusterShow(); 1384 1722 } 1385 1723 } … … 1392 1730 if (!exceptBounds || !exceptBounds.contains(m._latlng)) { 1393 1731 c._group._featureGroup.removeLayer(m); 1394 if (m. setOpacity) {1395 m. setOpacity(1);1732 if (m.clusterShow) { 1733 m.clusterShow(); 1396 1734 } 1397 1735 } … … 1410 1748 var childClusters = this._childClusters, 1411 1749 zoom = this._zoom, 1412 i, c; 1413 1414 if (zoomLevelToStart > zoom) { //Still going down to required depth, just recurse to child clusters 1750 i, c; 1751 1752 if (zoomLevelToStart <= zoom) { 1753 if (runAtEveryLevel) { 1754 runAtEveryLevel(this); 1755 } 1756 if (runAtBottomLevel && zoom === zoomLevelToStop) { 1757 runAtBottomLevel(this); 1758 } 1759 } 1760 1761 if (zoom < zoomLevelToStart || zoom < zoomLevelToStop) { 1415 1762 for (i = childClusters.length - 1; i >= 0; i--) { 1416 1763 c = childClusters[i]; … … 1419 1766 } 1420 1767 } 1421 } else { //In required depth 1422 1423 if (runAtEveryLevel) { 1424 runAtEveryLevel(this); 1425 } 1426 if (runAtBottomLevel && this._zoom === zoomLevelToStop) { 1427 runAtBottomLevel(this); 1428 } 1429 1430 //TODO: This loop is almost the same as above 1431 if (zoomLevelToStop > zoom) { 1432 for (i = childClusters.length - 1; i >= 0; i--) { 1433 c = childClusters[i]; 1434 if (boundsToApplyTo.intersects(c._bounds)) { 1435 c._recursively(boundsToApplyTo, zoomLevelToStart, zoomLevelToStop, runAtEveryLevel, runAtBottomLevel); 1436 } 1437 } 1438 } 1439 } 1440 }, 1441 1442 _recalculateBounds: function () { 1443 var markers = this._markers, 1444 childClusters = this._childClusters, 1445 i; 1446 1447 this._bounds = new L.LatLngBounds(); 1448 delete this._wLatLng; 1449 1450 for (i = markers.length - 1; i >= 0; i--) { 1451 this._expandBounds(markers[i]); 1452 } 1453 for (i = childClusters.length - 1; i >= 0; i--) { 1454 this._expandBounds(childClusters[i]); 1455 } 1456 }, 1457 1768 } 1769 }, 1458 1770 1459 1771 //Returns true if we are the parent of only one cluster and that cluster is the same as us … … 1463 1775 } 1464 1776 }); 1777 1778 1779 1780 /* 1781 * Extends L.Marker to include two extra methods: clusterHide and clusterShow. 1782 * 1783 * They work as setOpacity(0) and setOpacity(1) respectively, but 1784 * they will remember the marker's opacity when hiding and showing it again. 1785 * 1786 */ 1787 1788 1789 L.Marker.include({ 1790 1791 clusterHide: function () { 1792 this.options.opacityWhenUnclustered = this.options.opacity || 1; 1793 return this.setOpacity(0); 1794 }, 1795 1796 clusterShow: function () { 1797 var ret = this.setOpacity(this.options.opacity || this.options.opacityWhenUnclustered); 1798 delete this.options.opacityWhenUnclustered; 1799 return ret; 1800 } 1801 1802 }); 1803 1804 1465 1805 1466 1806 … … 1559 1899 obj = cell[k]; 1560 1900 dist = this._sqDist(objectPoint[L.Util.stamp(obj)], point); 1561 if (dist < closestDistSq) { 1901 if (dist < closestDistSq || 1902 dist <= closestDistSq && closest === null) { 1562 1903 closestDistSq = dist; 1563 1904 closest = obj; … … 1572 1913 1573 1914 _getCoord: function (x) { 1574 return Math.floor(x / this._cellSize); 1915 var coord = Math.floor(x / this._cellSize); 1916 return isFinite(coord) ? coord : x; 1575 1917 }, 1576 1918 … … 1693 2035 // find first baseline 1694 2036 var maxLat = false, minLat = false, 2037 maxLng = false, minLng = false, 2038 maxLatPt = null, minLatPt = null, 2039 maxLngPt = null, minLngPt = null, 1695 2040 maxPt = null, minPt = null, 1696 2041 i; … … 1699 2044 var pt = latLngs[i]; 1700 2045 if (maxLat === false || pt.lat > maxLat) { 1701 max Pt = pt;2046 maxLatPt = pt; 1702 2047 maxLat = pt.lat; 1703 2048 } 1704 2049 if (minLat === false || pt.lat < minLat) { 1705 min Pt = pt;2050 minLatPt = pt; 1706 2051 minLat = pt.lat; 1707 2052 } 1708 } 2053 if (maxLng === false || pt.lng > maxLng) { 2054 maxLngPt = pt; 2055 maxLng = pt.lng; 2056 } 2057 if (minLng === false || pt.lng < minLng) { 2058 minLngPt = pt; 2059 minLng = pt.lng; 2060 } 2061 } 2062 2063 if (minLat !== maxLat) { 2064 minPt = minLatPt; 2065 maxPt = maxLatPt; 2066 } else { 2067 minPt = minLngPt; 2068 maxPt = maxLngPt; 2069 } 2070 1709 2071 var ch = [].concat(this.buildConvexHull([minPt, maxPt], latLngs), 1710 2072 this.buildConvexHull([maxPt, minPt], latLngs)); … … 1765 2127 positions = this._generatePointsSpiral(childMarkers.length, center); 1766 2128 } else { 1767 center.y += 10; // Otherwise circles look wrong2129 center.y += 10; // Otherwise circles look wrong => hack for standard blue icon, renders differently for other icons. 1768 2130 positions = this._generatePointsCircle(childMarkers.length, center); 1769 2131 } … … 1800 2162 1801 2163 _generatePointsSpiral: function (count, centerPt) { 1802 var legLength = this._group.options.spiderfyDistanceMultiplier * this._spiralLengthStart, 1803 separation = this._group.options.spiderfyDistanceMultiplier * this._spiralFootSeparation, 1804 lengthFactor = this._group.options.spiderfyDistanceMultiplier * this._spiralLengthFactor, 2164 var spiderfyDistanceMultiplier = this._group.options.spiderfyDistanceMultiplier, 2165 legLength = spiderfyDistanceMultiplier * this._spiralLengthStart, 2166 separation = spiderfyDistanceMultiplier * this._spiralFootSeparation, 2167 lengthFactor = spiderfyDistanceMultiplier * this._spiralLengthFactor * this._2PI, 1805 2168 angle = 0, 1806 2169 res = [], … … 1809 2172 res.length = count; 1810 2173 2174 // Higher index, closer position to cluster center. 1811 2175 for (i = count - 1; i >= 0; i--) { 1812 2176 angle += separation / legLength + i * 0.0005; 1813 2177 res[i] = new L.Point(centerPt.x + legLength * Math.cos(angle), centerPt.y + legLength * Math.sin(angle))._round(); 1814 legLength += this._2PI *lengthFactor / angle;2178 legLength += lengthFactor / angle; 1815 2179 } 1816 2180 return res; … … 1824 2188 m, i; 1825 2189 2190 group._ignoreMove = true; 2191 1826 2192 this.setOpacity(1); 1827 2193 for (i = childMarkers.length - 1; i >= 0; i--) { … … 1844 2210 } 1845 2211 2212 group.fire('unspiderfied', { 2213 cluster: this, 2214 markers: childMarkers 2215 }); 2216 group._ignoreMove = false; 1846 2217 group._spiderfied = null; 1847 2218 } 1848 2219 }); 1849 2220 1850 L.MarkerCluster.include(!L.DomUtil.TRANSITION ? { 1851 //Non Animated versions of everything 2221 //Non Animated versions of everything 2222 L.MarkerClusterNonAnimated = L.MarkerCluster.extend({ 1852 2223 _animationSpiderfy: function (childMarkers, positions) { 1853 2224 var group = this._group, 1854 2225 map = group._map, 1855 2226 fg = group._featureGroup, 2227 legOptions = this._group.options.spiderLegPolylineOptions, 1856 2228 i, m, leg, newPos; 1857 2229 1858 for (i = childMarkers.length - 1; i >= 0; i--) { 2230 group._ignoreMove = true; 2231 2232 // Traverse in ascending order to make sure that inner circleMarkers are on top of further legs. Normal markers are re-ordered by newPosition. 2233 // The reverse order trick no longer improves performance on modern browsers. 2234 for (i = 0; i < childMarkers.length; i++) { 1859 2235 newPos = map.layerPointToLatLng(positions[i]); 1860 2236 m = childMarkers[i]; 1861 2237 2238 // Add the leg before the marker, so that in case the latter is a circleMarker, the leg is behind it. 2239 leg = new L.Polyline([this._latlng, newPos], legOptions); 2240 map.addLayer(leg); 2241 m._spiderLeg = leg; 2242 2243 // Now add the marker. 1862 2244 m._preSpiderfyLatlng = m._latlng; 1863 2245 m.setLatLng(newPos); … … 1867 2249 1868 2250 fg.addLayer(m); 1869 1870 1871 leg = new L.Polyline([this._latlng, newPos], { weight: 1.5, color: '#222' });1872 map.addLayer(leg);1873 m._spiderLeg = leg;1874 2251 } 1875 2252 this.setOpacity(0.3); 1876 group.fire('spiderfied'); 2253 2254 group._ignoreMove = false; 2255 group.fire('spiderfied', { 2256 cluster: this, 2257 markers: childMarkers 2258 }); 1877 2259 }, 1878 2260 … … 1880 2262 this._noanimationUnspiderfy(); 1881 2263 } 1882 } : { 1883 //Animated versions here 1884 SVG_ANIMATION: (function () { 1885 return document.createElementNS('http://www.w3.org/2000/svg', 'animate').toString().indexOf('SVGAnimate') > -1; 1886 }()), 2264 }); 2265 2266 //Animated versions here 2267 L.MarkerCluster.include({ 1887 2268 1888 2269 _animationSpiderfy: function (childMarkers, positions) { … … 1891 2272 map = group._map, 1892 2273 fg = group._featureGroup, 1893 thisLayerPos = map.latLngToLayerPoint(this._latlng), 1894 i, m, leg, newPos; 1895 1896 //Add markers to map hidden at our center point 1897 for (i = childMarkers.length - 1; i >= 0; i--) { 2274 thisLayerLatLng = this._latlng, 2275 thisLayerPos = map.latLngToLayerPoint(thisLayerLatLng), 2276 svg = L.Path.SVG, 2277 legOptions = L.extend({}, this._group.options.spiderLegPolylineOptions), // Copy the options so that we can modify them for animation. 2278 finalLegOpacity = legOptions.opacity, 2279 i, m, leg, legPath, legLength, newPos; 2280 2281 if (finalLegOpacity === undefined) { 2282 finalLegOpacity = L.MarkerClusterGroup.prototype.options.spiderLegPolylineOptions.opacity; 2283 } 2284 2285 if (svg) { 2286 // If the initial opacity of the spider leg is not 0 then it appears before the animation starts. 2287 legOptions.opacity = 0; 2288 2289 // Add the class for CSS transitions. 2290 legOptions.className = (legOptions.className || '') + ' leaflet-cluster-spider-leg'; 2291 } else { 2292 // Make sure we have a defined opacity. 2293 legOptions.opacity = finalLegOpacity; 2294 } 2295 2296 group._ignoreMove = true; 2297 2298 // Add markers and spider legs to map, hidden at our center point. 2299 // Traverse in ascending order to make sure that inner circleMarkers are on top of further legs. Normal markers are re-ordered by newPosition. 2300 // The reverse order trick no longer improves performance on modern browsers. 2301 for (i = 0; i < childMarkers.length; i++) { 1898 2302 m = childMarkers[i]; 1899 2303 1900 //If it is a marker, add it now and we'll animate it out 1901 if (m.setOpacity) { 1902 m.setZIndexOffset(1000000); //Make these appear on top of EVERYTHING 1903 m.setOpacity(0); 2304 newPos = map.layerPointToLatLng(positions[i]); 2305 2306 // Add the leg before the marker, so that in case the latter is a circleMarker, the leg is behind it. 2307 leg = new L.Polyline([thisLayerLatLng, newPos], legOptions); 2308 map.addLayer(leg); 2309 m._spiderLeg = leg; 2310 2311 // Explanations: https://jakearchibald.com/2013/animated-line-drawing-svg/ 2312 // In our case the transition property is declared in the CSS file. 2313 if (svg) { 2314 legPath = leg._path; 2315 legLength = legPath.getTotalLength() + 0.1; // Need a small extra length to avoid remaining dot in Firefox. 2316 legPath.style.strokeDasharray = legLength; // Just 1 length is enough, it will be duplicated. 2317 legPath.style.strokeDashoffset = legLength; 2318 } 2319 2320 // If it is a marker, add it now and we'll animate it out 2321 if (m.setZIndexOffset) { 2322 m.setZIndexOffset(1000000); // Make normal markers appear on top of EVERYTHING 2323 } 2324 if (m.clusterHide) { 2325 m.clusterHide(); 2326 } 1904 2327 1905 fg.addLayer(m); 1906 2328 // Vectors just get immediately added 2329 fg.addLayer(m); 2330 2331 if (m._setPos) { 1907 2332 m._setPos(thisLayerPos); 1908 } else {1909 //Vectors just get immediately added1910 fg.addLayer(m);1911 2333 } 1912 2334 } … … 1915 2337 group._animationStart(); 1916 2338 1917 var initialLegOpacity = L.Path.SVG ? 0 : 0.3, 1918 xmlns = L.Path.SVG_NS; 1919 1920 2339 // Reveal markers and spider legs. 1921 2340 for (i = childMarkers.length - 1; i >= 0; i--) { 1922 2341 newPos = map.layerPointToLatLng(positions[i]); … … 1927 2346 m.setLatLng(newPos); 1928 2347 1929 if (m.setOpacity) { 1930 m.setOpacity(1); 1931 } 1932 1933 1934 //Add Legs. 1935 leg = new L.Polyline([me._latlng, newPos], { weight: 1.5, color: '#222', opacity: initialLegOpacity }); 1936 map.addLayer(leg); 1937 m._spiderLeg = leg; 1938 1939 //Following animations don't work for canvas 1940 if (!L.Path.SVG || !this.SVG_ANIMATION) { 1941 continue; 1942 } 1943 1944 //How this works: 1945 //http://stackoverflow.com/questions/5924238/how-do-you-animate-an-svg-path-in-ios 1946 //http://dev.opera.com/articles/view/advanced-svg-animation-techniques/ 1947 1948 //Animate length 1949 var length = leg._path.getTotalLength(); 1950 leg._path.setAttribute("stroke-dasharray", length + "," + length); 1951 1952 var anim = document.createElementNS(xmlns, "animate"); 1953 anim.setAttribute("attributeName", "stroke-dashoffset"); 1954 anim.setAttribute("begin", "indefinite"); 1955 anim.setAttribute("from", length); 1956 anim.setAttribute("to", 0); 1957 anim.setAttribute("dur", 0.25); 1958 leg._path.appendChild(anim); 1959 anim.beginElement(); 1960 1961 //Animate opacity 1962 anim = document.createElementNS(xmlns, "animate"); 1963 anim.setAttribute("attributeName", "stroke-opacity"); 1964 anim.setAttribute("attributeName", "stroke-opacity"); 1965 anim.setAttribute("begin", "indefinite"); 1966 anim.setAttribute("from", 0); 1967 anim.setAttribute("to", 0.5); 1968 anim.setAttribute("dur", 0.25); 1969 leg._path.appendChild(anim); 1970 anim.beginElement(); 1971 } 1972 me.setOpacity(0.3); 1973 1974 //Set the opacity of the spiderLegs back to their correct value 1975 // The animations above override this until they complete. 1976 // If the initial opacity of the spiderlegs isn't 0 then they appear before the animation starts. 1977 if (L.Path.SVG) { 1978 this._group._forceLayout(); 1979 1980 for (i = childMarkers.length - 1; i >= 0; i--) { 1981 m = childMarkers[i]._spiderLeg; 1982 1983 m.options.opacity = 0.5; 1984 m._path.setAttribute('stroke-opacity', 0.5); 1985 } 1986 } 2348 if (m.clusterShow) { 2349 m.clusterShow(); 2350 } 2351 2352 // Animate leg (animation is actually delegated to CSS transition). 2353 if (svg) { 2354 leg = m._spiderLeg; 2355 legPath = leg._path; 2356 legPath.style.strokeDashoffset = 0; 2357 //legPath.style.strokeOpacity = finalLegOpacity; 2358 leg.setStyle({opacity: finalLegOpacity}); 2359 } 2360 } 2361 this.setOpacity(0.3); 2362 2363 group._ignoreMove = false; 1987 2364 1988 2365 setTimeout(function () { 1989 2366 group._animationEnd(); 1990 group.fire('spiderfied'); 2367 group.fire('spiderfied', { 2368 cluster: me, 2369 markers: childMarkers 2370 }); 1991 2371 }, 200); 1992 2372 }, 1993 2373 1994 2374 _animationUnspiderfy: function (zoomDetails) { 1995 var group = this._group, 2375 var me = this, 2376 group = this._group, 1996 2377 map = group._map, 1997 2378 fg = group._featureGroup, 1998 2379 thisLayerPos = zoomDetails ? map._latLngToNewLayerPoint(this._latlng, zoomDetails.zoom, zoomDetails.center) : map.latLngToLayerPoint(this._latlng), 1999 2380 childMarkers = this.getAllChildMarkers(), 2000 svg = L.Path.SVG && this.SVG_ANIMATION, 2001 m, i, a; 2002 2381 svg = L.Path.SVG, 2382 m, i, leg, legPath, legLength, nonAnimatable; 2383 2384 group._ignoreMove = true; 2003 2385 group._animationStart(); 2004 2386 … … 2008 2390 m = childMarkers[i]; 2009 2391 2010 //Marker was added to us after we were spid ified2392 //Marker was added to us after we were spiderfied 2011 2393 if (!m._preSpiderfyLatlng) { 2012 2394 continue; 2013 2395 } 2396 2397 //Close any popup on the marker first, otherwise setting the location of the marker will make the map scroll 2398 m.closePopup(); 2014 2399 2015 2400 //Fix up the location to the real one 2016 2401 m.setLatLng(m._preSpiderfyLatlng); 2017 2402 delete m._preSpiderfyLatlng; 2403 2018 2404 //Hack override the location to be our center 2019 if (m.setOpacity) { 2405 nonAnimatable = true; 2406 if (m._setPos) { 2020 2407 m._setPos(thisLayerPos); 2021 m.setOpacity(0); 2022 } else { 2408 nonAnimatable = false; 2409 } 2410 if (m.clusterHide) { 2411 m.clusterHide(); 2412 nonAnimatable = false; 2413 } 2414 if (nonAnimatable) { 2023 2415 fg.removeLayer(m); 2024 2416 } 2025 2417 2026 // Animate the spider legs back in2418 // Animate the spider leg back in (animation is actually delegated to CSS transition). 2027 2419 if (svg) { 2028 a = m._spiderLeg._path.childNodes[0]; 2029 a.setAttribute('to', a.getAttribute('from')); 2030 a.setAttribute('from', 0); 2031 a.beginElement(); 2032 2033 a = m._spiderLeg._path.childNodes[1]; 2034 a.setAttribute('from', 0.5); 2035 a.setAttribute('to', 0); 2036 a.setAttribute('stroke-opacity', 0); 2037 a.beginElement(); 2038 2039 m._spiderLeg._path.setAttribute('stroke-opacity', 0); 2040 } 2041 } 2420 leg = m._spiderLeg; 2421 legPath = leg._path; 2422 legLength = legPath.getTotalLength() + 0.1; 2423 legPath.style.strokeDashoffset = legLength; 2424 leg.setStyle({opacity: 0}); 2425 } 2426 } 2427 2428 group._ignoreMove = false; 2042 2429 2043 2430 setTimeout(function () { … … 2059 2446 } 2060 2447 2061 2062 if (m.setOpacity) { 2063 m.setOpacity(1); 2448 if (m.clusterShow) { 2449 m.clusterShow(); 2450 } 2451 if (m.setZIndexOffset) { 2064 2452 m.setZIndexOffset(0); 2065 2453 } … … 2073 2461 } 2074 2462 group._animationEnd(); 2463 group.fire('unspiderfied', { 2464 cluster: me, 2465 markers: childMarkers 2466 }); 2075 2467 }, 200); 2076 2468 } … … 2082 2474 _spiderfied: null, 2083 2475 2476 unspiderfy: function () { 2477 this._unspiderfy.apply(this, arguments); 2478 }, 2479 2084 2480 _spiderfierOnAdd: function () { 2085 2481 this._map.on('click', this._unspiderfyWrapper, this); … … 2091 2487 this._map.on('zoomend', this._noanimationUnspiderfy, this); 2092 2488 2093 if ( L.Path.SVG &&!L.Browser.touch) {2094 this._map. _initPathRoot();2489 if (!L.Browser.touch) { 2490 this._map.getRenderer(this); 2095 2491 //Needs to happen in the pageload, not after, or animations don't work in webkit 2096 2492 // http://stackoverflow.com/questions/8455200/svg-animate-with-dynamically-added-elements … … 2103 2499 this._map.off('zoomstart', this._unspiderfyZoomStart, this); 2104 2500 this._map.off('zoomanim', this._unspiderfyZoomAnim, this); 2105 2106 this._unspiderfy(); //Ensure that markers are back where they should be 2107 }, 2108 2501 this._map.off('zoomend', this._noanimationUnspiderfy, this); 2502 2503 //Ensure that markers are back where they should be 2504 // Use no animation to avoid a sticky leaflet-cluster-anim class on mapPane 2505 this._noanimationUnspiderfy(); 2506 }, 2109 2507 2110 2508 //On zoom start we add a zoomanim handler so that we are guaranteed to be last (after markers are animated) … … 2117 2515 this._map.on('zoomanim', this._unspiderfyZoomAnim, this); 2118 2516 }, 2517 2119 2518 _unspiderfyZoomAnim: function (zoomDetails) { 2120 2519 //Wait until the first zoomanim after the user has finished touch-zooming before running the animation … … 2127 2526 }, 2128 2527 2129 2130 2528 _unspiderfyWrapper: function () { 2131 2529 /// <summary>_unspiderfy but passes no arguments</summary> … … 2150 2548 this._featureGroup.removeLayer(layer); 2151 2549 2152 layer.setOpacity(1); 2153 //Position will be fixed up immediately in _animationUnspiderfy 2154 layer.setZIndexOffset(0); 2550 if (layer.clusterShow) { 2551 layer.clusterShow(); 2552 } 2553 //Position will be fixed up immediately in _animationUnspiderfy 2554 if (layer.setZIndexOffset) { 2555 layer.setZIndexOffset(0); 2556 } 2155 2557 2156 2558 this._map.removeLayer(layer._spiderLeg); … … 2161 2563 2162 2564 2565 /** 2566 * Adds 1 public method to MCG and 1 to L.Marker to facilitate changing 2567 * markers' icon options and refreshing their icon and their parent clusters 2568 * accordingly (case where their iconCreateFunction uses data of childMarkers 2569 * to make up the cluster icon). 2570 */ 2571 2572 2573 L.MarkerClusterGroup.include({ 2574 /** 2575 * Updates the icon of all clusters which are parents of the given marker(s). 2576 * In singleMarkerMode, also updates the given marker(s) icon. 2577 * @param layers L.MarkerClusterGroup|L.LayerGroup|Array(L.Marker)|Map(L.Marker)| 2578 * L.MarkerCluster|L.Marker (optional) list of markers (or single marker) whose parent 2579 * clusters need to be updated. If not provided, retrieves all child markers of this. 2580 * @returns {L.MarkerClusterGroup} 2581 */ 2582 refreshClusters: function (layers) { 2583 if (!layers) { 2584 layers = this._topClusterLevel.getAllChildMarkers(); 2585 } else if (layers instanceof L.MarkerClusterGroup) { 2586 layers = layers._topClusterLevel.getAllChildMarkers(); 2587 } else if (layers instanceof L.LayerGroup) { 2588 layers = layers._layers; 2589 } else if (layers instanceof L.MarkerCluster) { 2590 layers = layers.getAllChildMarkers(); 2591 } else if (layers instanceof L.Marker) { 2592 layers = [layers]; 2593 } // else: must be an Array(L.Marker)|Map(L.Marker) 2594 this._flagParentsIconsNeedUpdate(layers); 2595 this._refreshClustersIcons(); 2596 2597 // In case of singleMarkerMode, also re-draw the markers. 2598 if (this.options.singleMarkerMode) { 2599 this._refreshSingleMarkerModeMarkers(layers); 2600 } 2601 2602 return this; 2603 }, 2604 2605 /** 2606 * Simply flags all parent clusters of the given markers as having a "dirty" icon. 2607 * @param layers Array(L.Marker)|Map(L.Marker) list of markers. 2608 * @private 2609 */ 2610 _flagParentsIconsNeedUpdate: function (layers) { 2611 var id, parent; 2612 2613 // Assumes layers is an Array or an Object whose prototype is non-enumerable. 2614 for (id in layers) { 2615 // Flag parent clusters' icon as "dirty", all the way up. 2616 // Dumb process that flags multiple times upper parents, but still 2617 // much more efficient than trying to be smart and make short lists, 2618 // at least in the case of a hierarchy following a power law: 2619 // http://jsperf.com/flag-nodes-in-power-hierarchy/2 2620 parent = layers[id].__parent; 2621 while (parent) { 2622 parent._iconNeedsUpdate = true; 2623 parent = parent.__parent; 2624 } 2625 } 2626 }, 2627 2628 /** 2629 * Re-draws the icon of the supplied markers. 2630 * To be used in singleMarkerMode only. 2631 * @param layers Array(L.Marker)|Map(L.Marker) list of markers. 2632 * @private 2633 */ 2634 _refreshSingleMarkerModeMarkers: function (layers) { 2635 var id, layer; 2636 2637 for (id in layers) { 2638 layer = layers[id]; 2639 2640 // Make sure we do not override markers that do not belong to THIS group. 2641 if (this.hasLayer(layer)) { 2642 // Need to re-create the icon first, then re-draw the marker. 2643 layer.setIcon(this._overrideMarkerIcon(layer)); 2644 } 2645 } 2646 } 2647 }); 2648 2649 L.Marker.include({ 2650 /** 2651 * Updates the given options in the marker's icon and refreshes the marker. 2652 * @param options map object of icon options. 2653 * @param directlyRefreshClusters boolean (optional) true to trigger 2654 * MCG.refreshClustersOf() right away with this single marker. 2655 * @returns {L.Marker} 2656 */ 2657 refreshIconOptions: function (options, directlyRefreshClusters) { 2658 var icon = this.options.icon; 2659 2660 L.setOptions(icon, options); 2661 2662 this.setIcon(icon); 2663 2664 // Shortcut to refresh the associated MCG clusters right away. 2665 // To be used when refreshing a single marker. 2666 // Otherwise, better use MCG.refreshClusters() once at the end with 2667 // the list of modified markers. 2668 if (directlyRefreshClusters && this.__parent) { 2669 this.__parent._group.refreshClusters(this); 2670 } 2671 2672 return this; 2673 } 2674 }); 2675 2676 2163 2677 }(window, document));
Note:
See TracChangeset
for help on using the changeset viewer.