leaflet.markercluster-src.js 54 KB

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