当前位置:   article > 正文

Leaflet聚合——PruneCluster 、markerClusterGroup使用总结

markerclustergroup
  1. var map = L.map("map", {
  2. center: [31.59, 120.29],
  3. zoom: 12,
  4. zoomControl: true
  5. });

 


1.PruneCluster

1.1创建

  1. var pruneClusterView = new PruneClusterForLeaflet();
  2. for(var key in data){
  3. var point = data[key];
  4. pruneClusterView.RegisterMarker(new PruneCluster.Marker(point.lat, point.lng, {
  5. title: 1,
  6. singleMarkerMode:true
  7. }));
  8. }
  9. map.addLayer(pruneClusterView);
  10. pruneClusterView .ProcessView();

1.2 删除

  1. // Remove all the markers
  2. pruneClusterView.RemoveMarkers();
  3. // Remove a list of markers
  4. pruneClusterView.RemoveMarkers([markerA,markerB,...]);

2.markerClusterGroup

参考leaflet官方地址

备注:markerCluster在数量多的时候初次创建会很卡,pruneClusterView不会很卡。

2.1创建

  1. var markerCluster =new L.markerClusterGroup({
  2. singleMarkerMode: true//true:单个marker显示聚合数字1,false:显示单个marker
  3. });
  4. var markerList =[];
  5. for(var key in markers){
  6. var point = markers[key];
  7. var marker = L.marker(L.latLng(point.lat, point.lon));
  8. markerList.push(marker);
  9. }
  10. markerCluster.addLayers(markerList);
  11. map.addLayer(markerCluster);

2.2删除

  1. markerCluster.clearLayers();
  2. //addLayer, removeLayer and clearLayers

2.3其他

  1. markerCluster.refreshClusters();
  2. markerCluster.refreshClusters([myMarker0, myMarker33]);
  3. markerCluster.refreshClusters({id_0: myMarker0, id_any: myMarker33});
  4. markerCluster.refreshClusters(myLayerGroup);
  5. markerCluster.refreshClusters(myMarker);
  6. // Use as many times as required to update markers,
  7. // then call refreshClusters once finished.
  8. for (i in markersSubArray) {
  9. markersSubArray[i].refreshIconOptions(newOptionsMappingArray[i]);
  10. }
  11. markers.refreshClusters(markersSubArray);
  12. // If updating only one marker, pass true to
  13. // refresh this marker's parent clusters right away.
  14. myMarker.refreshIconOptions(optionsMap, true);

2.4 需引入markerClusterGroup插件代码(CSS、js文件)

  1. .marker-cluster-small {
  2. background-color: rgba(181, 226, 140, 0.6);
  3. }
  4. .marker-cluster-small div {
  5. background-color: rgba(110, 204, 57, 0.6);
  6. }
  7. .marker-cluster-medium {
  8. background-color: rgba(241, 211, 87, 0.6);
  9. }
  10. .marker-cluster-medium div {
  11. background-color: rgba(240, 194, 12, 0.6);
  12. }
  13. .marker-cluster-large {
  14. background-color: rgba(253, 156, 115, 0.6);
  15. }
  16. .marker-cluster-large div {
  17. background-color: rgba(241, 128, 23, 0.6);
  18. }
  19. /* IE 6-8 fallback colors */
  20. .leaflet-oldie .marker-cluster-small {
  21. background-color: rgb(181, 226, 140);
  22. }
  23. .leaflet-oldie .marker-cluster-small div {
  24. background-color: rgb(110, 204, 57);
  25. }
  26. .leaflet-oldie .marker-cluster-medium {
  27. background-color: rgb(241, 211, 87);
  28. }
  29. .leaflet-oldie .marker-cluster-medium div {
  30. background-color: rgb(240, 194, 12);
  31. }
  32. .leaflet-oldie .marker-cluster-large {
  33. background-color: rgb(253, 156, 115);
  34. }
  35. .leaflet-oldie .marker-cluster-large div {
  36. background-color: rgb(241, 128, 23);
  37. }
  38. .marker-cluster {
  39. background-clip: padding-box;
  40. border-radius: 20px;
  41. }
  42. .marker-cluster div {
  43. width: 30px;
  44. height: 30px;
  45. margin-left: 5px;
  46. margin-top: 5px;
  47. text-align: center;
  48. border-radius: 15px;
  49. font: 12px "Helvetica Neue", Arial, Helvetica, sans-serif;
  50. }
  51. .marker-cluster span {
  52. line-height: 30px;
  53. }
  54. .leaflet-cluster-anim .leaflet-marker-icon, .leaflet-cluster-anim .leaflet-marker-shadow {
  55. -webkit-transition: -webkit-transform 0.3s ease-out, opacity 0.3s ease-in;
  56. -moz-transition: -moz-transform 0.3s ease-out, opacity 0.3s ease-in;
  57. -o-transition: -o-transform 0.3s ease-out, opacity 0.3s ease-in;
  58. transition: transform 0.3s ease-out, opacity 0.3s ease-in;
  59. }
  60. .leaflet-cluster-spider-leg {
  61. /* stroke-dashoffset (duration and function) should match with leaflet-marker-icon transform in order to track it exactly */
  62. -webkit-transition: -webkit-stroke-dashoffset 0.3s ease-out, -webkit-stroke-opacity 0.3s ease-in;
  63. -moz-transition: -moz-stroke-dashoffset 0.3s ease-out, -moz-stroke-opacity 0.3s ease-in;
  64. -o-transition: -o-stroke-dashoffset 0.3s ease-out, -o-stroke-opacity 0.3s ease-in;
  65. transition: stroke-dashoffset 0.3s ease-out, stroke-opacity 0.3s ease-in;
  66. }
  1. /***** MarkerClusterGroup plugin *******/
  2. L.MarkerClusterGroup = L.FeatureGroup.extend({
  3. options: {
  4. maxClusterRadius: 80, //A cluster will cover at most this many pixels from its center
  5. iconCreateFunction: null,
  6. spiderfyOnMaxZoom: true,
  7. showCoverageOnHover: true,
  8. zoomToBoundsOnClick: true,
  9. singleMarkerMode: false,
  10. disableClusteringAtZoom: null,
  11. // Setting this to false prevents the removal of any clusters outside of the viewpoint, which
  12. // is the default behaviour for performance reasons.
  13. removeOutsideVisibleBounds: true,
  14. // Set to false to disable all animations (zoom and spiderfy).
  15. // If false, option animateAddingMarkers below has no effect.
  16. // If L.DomUtil.TRANSITION is falsy, this option has no effect.
  17. animate: true,
  18. //Whether to animate adding markers after adding the MarkerClusterGroup to the map
  19. // If you are adding individual markers set to true, if adding bulk markers leave false for massive performance gains.
  20. animateAddingMarkers: false,
  21. //Increase to increase the distance away that spiderfied markers appear from the center
  22. spiderfyDistanceMultiplier: 1,
  23. // Make it possible to specify a polyline options on a spider leg
  24. spiderLegPolylineOptions: { weight: 1.5, color: '#222', opacity: 0.5 },
  25. // 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
  26. chunkedLoading: false,
  27. chunkInterval: 200, // process markers for a maximum of ~ n milliseconds (then trigger the chunkProgress callback)
  28. chunkDelay: 50, // at the end of each interval, give n milliseconds back to system/browser
  29. chunkProgress: null, // progress callback: function(processed, total, elapsed) (e.g. for a progress indicator)
  30. //Options to pass to the L.Polygon constructor
  31. polygonOptions: {}
  32. },
  33. initialize: function (options) {
  34. L.Util.setOptions(this, options);
  35. if (!this.options.iconCreateFunction) {
  36. this.options.iconCreateFunction = this._defaultIconCreateFunction;
  37. }
  38. if (!this.options.clusterPane) {
  39. this.options.clusterPane = L.Marker.prototype.options.pane;
  40. }
  41. this._featureGroup = L.featureGroup();
  42. this._featureGroup.addEventParent(this);
  43. this._nonPointGroup = L.featureGroup();
  44. this._nonPointGroup.addEventParent(this);
  45. this._inZoomAnimation = 0;
  46. this._needsClustering = [];
  47. this._needsRemoving = []; //Markers removed while we aren't on the map need to be kept track of
  48. //The bounds of the currently shown area (from _getExpandedVisibleBounds) Updated on zoom/move
  49. this._currentShownBounds = null;
  50. this._queue = [];
  51. this._childMarkerEventHandlers = {
  52. 'dragstart': this._childMarkerDragStart,
  53. 'move': this._childMarkerMoved,
  54. 'dragend': this._childMarkerDragEnd,
  55. };
  56. // Hook the appropriate animation methods.
  57. var animate = L.DomUtil.TRANSITION && this.options.animate;
  58. L.extend(this, animate ? this._withAnimation : this._noAnimation);
  59. // Remember which MarkerCluster class to instantiate (animated or not).
  60. this._markerCluster = animate ? L.MarkerCluster : L.MarkerClusterNonAnimated;
  61. },
  62. addLayer: function (layer) {
  63. if (layer instanceof L.LayerGroup) {
  64. return this.addLayers([layer]);
  65. }
  66. //Don't cluster non point data
  67. if (!layer.getLatLng) {
  68. this._nonPointGroup.addLayer(layer);
  69. this.fire('layeradd', { layer: layer });
  70. return this;
  71. }
  72. if (!this._map) {
  73. this._needsClustering.push(layer);
  74. this.fire('layeradd', { layer: layer });
  75. return this;
  76. }
  77. if (this.hasLayer(layer)) {
  78. return this;
  79. }
  80. //If we have already clustered we'll need to add this one to a cluster
  81. if (this._unspiderfy) {
  82. this._unspiderfy();
  83. }
  84. this._addLayer(layer, this._maxZoom);
  85. this.fire('layeradd', { layer: layer });
  86. // Refresh bounds and weighted positions.
  87. this._topClusterLevel._recalculateBounds();
  88. this._refreshClustersIcons();
  89. //Work out what is visible
  90. var visibleLayer = layer,
  91. currentZoom = this._zoom;
  92. if (layer.__parent) {
  93. while (visibleLayer.__parent._zoom >= currentZoom) {
  94. visibleLayer = visibleLayer.__parent;
  95. }
  96. }
  97. if (this._currentShownBounds.contains(visibleLayer.getLatLng())) {
  98. if (this.options.animateAddingMarkers) {
  99. this._animationAddLayer(layer, visibleLayer);
  100. } else {
  101. this._animationAddLayerNonAnimated(layer, visibleLayer);
  102. }
  103. }
  104. return this;
  105. },
  106. removeLayer: function (layer) {
  107. if (layer instanceof L.LayerGroup) {
  108. return this.removeLayers([layer]);
  109. }
  110. //Non point layers
  111. if (!layer.getLatLng) {
  112. this._nonPointGroup.removeLayer(layer);
  113. this.fire('layerremove', { layer: layer });
  114. return this;
  115. }
  116. if (!this._map) {
  117. if (!this._arraySplice(this._needsClustering, layer) && this.hasLayer(layer)) {
  118. this._needsRemoving.push({ layer: layer, latlng: layer._latlng });
  119. }
  120. this.fire('layerremove', { layer: layer });
  121. return this;
  122. }
  123. if (!layer.__parent) {
  124. return this;
  125. }
  126. if (this._unspiderfy) {
  127. this._unspiderfy();
  128. this._unspiderfyLayer(layer);
  129. }
  130. //Remove the marker from clusters
  131. this._removeLayer(layer, true);
  132. this.fire('layerremove', { layer: layer });
  133. // Refresh bounds and weighted positions.
  134. this._topClusterLevel._recalculateBounds();
  135. this._refreshClustersIcons();
  136. layer.off(this._childMarkerEventHandlers, this);
  137. if (this._featureGroup.hasLayer(layer)) {
  138. this._featureGroup.removeLayer(layer);
  139. if (layer.clusterShow) {
  140. layer.clusterShow();
  141. }
  142. }
  143. return this;
  144. },
  145. //Takes an array of markers and adds them in bulk
  146. addLayers: function (layersArray, skipLayerAddEvent) {
  147. if (!L.Util.isArray(layersArray)) {
  148. return this.addLayer(layersArray);
  149. }
  150. var fg = this._featureGroup,
  151. npg = this._nonPointGroup,
  152. chunked = this.options.chunkedLoading,
  153. chunkInterval = this.options.chunkInterval,
  154. chunkProgress = this.options.chunkProgress,
  155. l = layersArray.length,
  156. offset = 0,
  157. originalArray = true,
  158. m;
  159. if (this._map) {
  160. var started = (new Date()).getTime();
  161. var process = L.bind(function () {
  162. var start = (new Date()).getTime();
  163. for (; offset < l; offset++) {
  164. if (chunked && offset % 200 === 0) {
  165. // every couple hundred markers, instrument the time elapsed since processing started:
  166. var elapsed = (new Date()).getTime() - start;
  167. if (elapsed > chunkInterval) {
  168. break; // been working too hard, time to take a break :-)
  169. }
  170. }
  171. m = layersArray[offset];
  172. // Group of layers, append children to layersArray and skip.
  173. // Side effects:
  174. // - Total increases, so chunkProgress ratio jumps backward.
  175. // - Groups are not included in this group, only their non-group child layers (hasLayer).
  176. // Changing array length while looping does not affect performance in current browsers:
  177. // http://jsperf.com/for-loop-changing-length/6
  178. if (m instanceof L.LayerGroup) {
  179. if (originalArray) {
  180. layersArray = layersArray.slice();
  181. originalArray = false;
  182. }
  183. this._extractNonGroupLayers(m, layersArray);
  184. l = layersArray.length;
  185. continue;
  186. }
  187. //Not point data, can't be clustered
  188. if (!m.getLatLng) {
  189. npg.addLayer(m);
  190. if (!skipLayerAddEvent) {
  191. this.fire('layeradd', { layer: m });
  192. }
  193. continue;
  194. }
  195. if (this.hasLayer(m)) {
  196. continue;
  197. }
  198. this._addLayer(m, this._maxZoom);
  199. if (!skipLayerAddEvent) {
  200. this.fire('layeradd', { layer: m });
  201. }
  202. //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. if (m.__parent) {
  204. if (m.__parent.getChildCount() === 2) {
  205. var markers = m.__parent.getAllChildMarkers(),
  206. otherMarker = markers[0] === m ? markers[1] : markers[0];
  207. fg.removeLayer(otherMarker);
  208. }
  209. }
  210. }
  211. if (chunkProgress) {
  212. // report progress and time elapsed:
  213. chunkProgress(offset, l, (new Date()).getTime() - started);
  214. }
  215. // Completed processing all markers.
  216. if (offset === l) {
  217. // Refresh bounds and weighted positions.
  218. this._topClusterLevel._recalculateBounds();
  219. this._refreshClustersIcons();
  220. this._topClusterLevel._recursivelyAddChildrenToMap(null, this._zoom, this._currentShownBounds);
  221. } else {
  222. setTimeout(process, this.options.chunkDelay);
  223. }
  224. }, this);
  225. process();
  226. } else {
  227. var needsClustering = this._needsClustering;
  228. for (; offset < l; offset++) {
  229. m = layersArray[offset];
  230. // Group of layers, append children to layersArray and skip.
  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. //Not point data, can't be clustered
  241. if (!m.getLatLng) {
  242. npg.addLayer(m);
  243. continue;
  244. }
  245. if (this.hasLayer(m)) {
  246. continue;
  247. }
  248. needsClustering.push(m);
  249. }
  250. }
  251. return this;
  252. },
  253. //Takes an array of markers and removes them in bulk
  254. removeLayers: function (layersArray) {
  255. var i, m,
  256. l = layersArray.length,
  257. fg = this._featureGroup,
  258. npg = this._nonPointGroup,
  259. originalArray = true;
  260. if (!this._map) {
  261. for (i = 0; i < l; i++) {
  262. m = layersArray[i];
  263. // Group of layers, append children to layersArray and skip.
  264. if (m instanceof L.LayerGroup) {
  265. if (originalArray) {
  266. layersArray = layersArray.slice();
  267. originalArray = false;
  268. }
  269. this._extractNonGroupLayers(m, layersArray);
  270. l = layersArray.length;
  271. continue;
  272. }
  273. this._arraySplice(this._needsClustering, m);
  274. npg.removeLayer(m);
  275. if (this.hasLayer(m)) {
  276. this._needsRemoving.push({ layer: m, latlng: m._latlng });
  277. }
  278. this.fire('layerremove', { layer: m });
  279. }
  280. return this;
  281. }
  282. if (this._unspiderfy) {
  283. this._unspiderfy();
  284. // Work on a copy of the array, so that next loop is not affected.
  285. var layersArray2 = layersArray.slice(),
  286. l2 = l;
  287. for (i = 0; i < l2; i++) {
  288. m = layersArray2[i];
  289. // Group of layers, append children to layersArray and skip.
  290. if (m instanceof L.LayerGroup) {
  291. this._extractNonGroupLayers(m, layersArray2);
  292. l2 = layersArray2.length;
  293. continue;
  294. }
  295. this._unspiderfyLayer(m);
  296. }
  297. }
  298. for (i = 0; i < l; i++) {
  299. m = layersArray[i];
  300. // Group of layers, append children to layersArray and skip.
  301. if (m instanceof L.LayerGroup) {
  302. if (originalArray) {
  303. layersArray = layersArray.slice();
  304. originalArray = false;
  305. }
  306. this._extractNonGroupLayers(m, layersArray);
  307. l = layersArray.length;
  308. continue;
  309. }
  310. if (!m.__parent) {
  311. npg.removeLayer(m);
  312. this.fire('layerremove', { layer: m });
  313. continue;
  314. }
  315. this._removeLayer(m, true, true);
  316. this.fire('layerremove', { layer: m });
  317. if (fg.hasLayer(m)) {
  318. fg.removeLayer(m);
  319. if (m.clusterShow) {
  320. m.clusterShow();
  321. }
  322. }
  323. }
  324. // Refresh bounds and weighted positions.
  325. this._topClusterLevel._recalculateBounds();
  326. this._refreshClustersIcons();
  327. //Fix up the clusters and markers on the map
  328. this._topClusterLevel._recursivelyAddChildrenToMap(null, this._zoom, this._currentShownBounds);
  329. return this;
  330. },
  331. //Removes all layers from the MarkerClusterGroup
  332. clearLayers: function () {
  333. //Need our own special implementation as the LayerGroup one doesn't work for us
  334. //If we aren't on the map (yet), blow away the markers we know of
  335. if (!this._map) {
  336. this._needsClustering = [];
  337. delete this._gridClusters;
  338. delete this._gridUnclustered;
  339. }
  340. if (this._noanimationUnspiderfy) {
  341. this._noanimationUnspiderfy();
  342. }
  343. //Remove all the visible layers
  344. this._featureGroup.clearLayers();
  345. this._nonPointGroup.clearLayers();
  346. this.eachLayer(function (marker) {
  347. marker.off(this._childMarkerEventHandlers, this);
  348. delete marker.__parent;
  349. }, this);
  350. if (this._map) {
  351. //Reset _topClusterLevel and the DistanceGrids
  352. this._generateInitialClusters();
  353. }
  354. return this;
  355. },
  356. //Override FeatureGroup.getBounds as it doesn't work
  357. getBounds: function () {
  358. var bounds = new L.LatLngBounds();
  359. if (this._topClusterLevel) {
  360. bounds.extend(this._topClusterLevel._bounds);
  361. }
  362. for (var i = this._needsClustering.length - 1; i >= 0; i--) {
  363. bounds.extend(this._needsClustering[i].getLatLng());
  364. }
  365. bounds.extend(this._nonPointGroup.getBounds());
  366. return bounds;
  367. },
  368. //Overrides LayerGroup.eachLayer
  369. eachLayer: function (method, context) {
  370. var markers = this._needsClustering.slice(),
  371. needsRemoving = this._needsRemoving,
  372. thisNeedsRemoving, i, j;
  373. if (this._topClusterLevel) {
  374. this._topClusterLevel.getAllChildMarkers(markers);
  375. }
  376. for (i = markers.length - 1; i >= 0; i--) {
  377. thisNeedsRemoving = true;
  378. for (j = needsRemoving.length - 1; j >= 0; j--) {
  379. if (needsRemoving[j].layer === markers[i]) {
  380. thisNeedsRemoving = false;
  381. break;
  382. }
  383. }
  384. if (thisNeedsRemoving) {
  385. method.call(context, markers[i]);
  386. }
  387. }
  388. this._nonPointGroup.eachLayer(method, context);
  389. },
  390. //Overrides LayerGroup.getLayers
  391. getLayers: function () {
  392. var layers = [];
  393. this.eachLayer(function (l) {
  394. layers.push(l);
  395. });
  396. return layers;
  397. },
  398. //Overrides LayerGroup.getLayer, WARNING: Really bad performance
  399. getLayer: function (id) {
  400. var result = null;
  401. id = parseInt(id, 10);
  402. this.eachLayer(function (l) {
  403. if (L.stamp(l) === id) {
  404. result = l;
  405. }
  406. });
  407. return result;
  408. },
  409. //Returns true if the given layer is in this MarkerClusterGroup
  410. hasLayer: function (layer) {
  411. if (!layer) {
  412. return false;
  413. }
  414. var i, anArray = this._needsClustering;
  415. for (i = anArray.length - 1; i >= 0; i--) {
  416. if (anArray[i] === layer) {
  417. return true;
  418. }
  419. }
  420. anArray = this._needsRemoving;
  421. for (i = anArray.length - 1; i >= 0; i--) {
  422. if (anArray[i].layer === layer) {
  423. return false;
  424. }
  425. }
  426. return !!(layer.__parent && layer.__parent._group === this) || this._nonPointGroup.hasLayer(layer);
  427. },
  428. //Zoom down to show the given layer (spiderfying if necessary) then calls the callback
  429. zoomToShowLayer: function (layer, callback) {
  430. if (typeof callback !== 'function') {
  431. callback = function () {};
  432. }
  433. var showMarker = function () {
  434. if ((layer._icon || layer.__parent._icon) && !this._inZoomAnimation) {
  435. this._map.off('moveend', showMarker, this);
  436. this.off('animationend', showMarker, this);
  437. if (layer._icon) {
  438. callback();
  439. } else if (layer.__parent._icon) {
  440. this.once('spiderfied', callback, this);
  441. layer.__parent.spiderfy();
  442. }
  443. }
  444. };
  445. if (layer._icon && this._map.getBounds().contains(layer.getLatLng())) {
  446. //Layer is visible ond on screen, immediate return
  447. callback();
  448. } else if (layer.__parent._zoom < Math.round(this._map._zoom)) {
  449. //Layer should be visible at this zoom level. It must not be on screen so just pan over to it
  450. this._map.on('moveend', showMarker, this);
  451. this._map.panTo(layer.getLatLng());
  452. } else {
  453. this._map.on('moveend', showMarker, this);
  454. this.on('animationend', showMarker, this);
  455. layer.__parent.zoomToBounds();
  456. }
  457. },
  458. //Overrides FeatureGroup.onAdd
  459. onAdd: function (map) {
  460. this._map = map;
  461. var i, l, layer;
  462. if (!isFinite(this._map.getMaxZoom())) {
  463. throw "Map has no maxZoom specified";
  464. }
  465. this._featureGroup.addTo(map);
  466. this._nonPointGroup.addTo(map);
  467. if (!this._gridClusters) {
  468. this._generateInitialClusters();
  469. }
  470. this._maxLat = map.options.crs.projection.MAX_LATITUDE;
  471. //Restore all the positions as they are in the MCG before removing them
  472. for (i = 0, l = this._needsRemoving.length; i < l; i++) {
  473. layer = this._needsRemoving[i];
  474. layer.newlatlng = layer.layer._latlng;
  475. layer.layer._latlng = layer.latlng;
  476. }
  477. //Remove them, then restore their new positions
  478. for (i = 0, l = this._needsRemoving.length; i < l; i++) {
  479. layer = this._needsRemoving[i];
  480. this._removeLayer(layer.layer, true);
  481. layer.layer._latlng = layer.newlatlng;
  482. }
  483. this._needsRemoving = [];
  484. //Remember the current zoom level and bounds
  485. this._zoom = Math.round(this._map._zoom);
  486. this._currentShownBounds = this._getExpandedVisibleBounds();
  487. this._map.on('zoomend', this._zoomEnd, this);
  488. this._map.on('moveend', this._moveEnd, this);
  489. if (this._spiderfierOnAdd) { //TODO FIXME: Not sure how to have spiderfier add something on here nicely
  490. this._spiderfierOnAdd();
  491. }
  492. this._bindEvents();
  493. //Actually add our markers to the map:
  494. l = this._needsClustering;
  495. this._needsClustering = [];
  496. this.addLayers(l, true);
  497. },
  498. //Overrides FeatureGroup.onRemove
  499. onRemove: function (map) {
  500. map.off('zoomend', this._zoomEnd, this);
  501. map.off('moveend', this._moveEnd, this);
  502. this._unbindEvents();
  503. //In case we are in a cluster animation
  504. this._map._mapPane.className = this._map._mapPane.className.replace(' leaflet-cluster-anim', '');
  505. if (this._spiderfierOnRemove) { //TODO FIXME: Not sure how to have spiderfier add something on here nicely
  506. this._spiderfierOnRemove();
  507. }
  508. delete this._maxLat;
  509. //Clean up all the layers we added to the map
  510. this._hideCoverage();
  511. this._featureGroup.remove();
  512. this._nonPointGroup.remove();
  513. this._featureGroup.clearLayers();
  514. this._map = null;
  515. },
  516. getVisibleParent: function (marker) {
  517. var vMarker = marker;
  518. while (vMarker && !vMarker._icon) {
  519. vMarker = vMarker.__parent;
  520. }
  521. return vMarker || null;
  522. },
  523. //Remove the given object from the given array
  524. _arraySplice: function (anArray, obj) {
  525. for (var i = anArray.length - 1; i >= 0; i--) {
  526. if (anArray[i] === obj) {
  527. anArray.splice(i, 1);
  528. return true;
  529. }
  530. }
  531. },
  532. /**
  533. * Removes a marker from all _gridUnclustered zoom levels, starting at the supplied zoom.
  534. * @param marker to be removed from _gridUnclustered.
  535. * @param z integer bottom start zoom level (included)
  536. * @private
  537. */
  538. _removeFromGridUnclustered: function (marker, z) {
  539. var map = this._map,
  540. gridUnclustered = this._gridUnclustered,
  541. minZoom = Math.floor(this._map.getMinZoom());
  542. for (; z >= minZoom; z--) {
  543. if (!gridUnclustered[z].removeObject(marker, map.project(marker.getLatLng(), z))) {
  544. break;
  545. }
  546. }
  547. },
  548. _childMarkerDragStart: function (e) {
  549. e.target.__dragStart = e.target._latlng;
  550. },
  551. _childMarkerMoved: function (e) {
  552. if (!this._ignoreMove && !e.target.__dragStart) {
  553. var isPopupOpen = e.target._popup && e.target._popup.isOpen();
  554. this._moveChild(e.target, e.oldLatLng, e.latlng);
  555. if (isPopupOpen) {
  556. e.target.openPopup();
  557. }
  558. }
  559. },
  560. _moveChild: function (layer, from, to) {
  561. layer._latlng = from;
  562. this.removeLayer(layer);
  563. layer._latlng = to;
  564. this.addLayer(layer);
  565. },
  566. _childMarkerDragEnd: function (e) {
  567. if (e.target.__dragStart) {
  568. this._moveChild(e.target, e.target.__dragStart, e.target._latlng);
  569. }
  570. delete e.target.__dragStart;
  571. },
  572. //Internal function for removing a marker from everything.
  573. //dontUpdateMap: set to true if you will handle updating the map manually (for bulk functions)
  574. _removeLayer: function (marker, removeFromDistanceGrid, dontUpdateMap) {
  575. var gridClusters = this._gridClusters,
  576. gridUnclustered = this._gridUnclustered,
  577. fg = this._featureGroup,
  578. map = this._map,
  579. minZoom = Math.floor(this._map.getMinZoom());
  580. //Remove the marker from distance clusters it might be in
  581. if (removeFromDistanceGrid) {
  582. this._removeFromGridUnclustered(marker, this._maxZoom);
  583. }
  584. //Work our way up the clusters removing them as we go if required
  585. var cluster = marker.__parent,
  586. markers = cluster._markers,
  587. otherMarker;
  588. //Remove the marker from the immediate parents marker list
  589. this._arraySplice(markers, marker);
  590. while (cluster) {
  591. cluster._childCount--;
  592. cluster._boundsNeedUpdate = true;
  593. if (cluster._zoom < minZoom) {
  594. //Top level, do nothing
  595. break;
  596. } else if (removeFromDistanceGrid && cluster._childCount <= 1) { //Cluster no longer required
  597. //We need to push the other marker up to the parent
  598. otherMarker = cluster._markers[0] === marker ? cluster._markers[1] : cluster._markers[0];
  599. //Update distance grid
  600. gridClusters[cluster._zoom].removeObject(cluster, map.project(cluster._cLatLng, cluster._zoom));
  601. gridUnclustered[cluster._zoom].addObject(otherMarker, map.project(otherMarker.getLatLng(), cluster._zoom));
  602. //Move otherMarker up to parent
  603. this._arraySplice(cluster.__parent._childClusters, cluster);
  604. cluster.__parent._markers.push(otherMarker);
  605. otherMarker.__parent = cluster.__parent;
  606. if (cluster._icon) {
  607. //Cluster is currently on the map, need to put the marker on the map instead
  608. fg.removeLayer(cluster);
  609. if (!dontUpdateMap) {
  610. fg.addLayer(otherMarker);
  611. }
  612. }
  613. } else {
  614. cluster._iconNeedsUpdate = true;
  615. }
  616. cluster = cluster.__parent;
  617. }
  618. delete marker.__parent;
  619. },
  620. _isOrIsParent: function (el, oel) {
  621. while (oel) {
  622. if (el === oel) {
  623. return true;
  624. }
  625. oel = oel.parentNode;
  626. }
  627. return false;
  628. },
  629. //Override L.Evented.fire
  630. fire: function (type, data, propagate) {
  631. if (data && data.layer instanceof L.MarkerCluster) {
  632. //Prevent multiple clustermouseover/off events if the icon is made up of stacked divs (Doesn't work in ie <= 8, no relatedTarget)
  633. if (data.originalEvent && this._isOrIsParent(data.layer._icon, data.originalEvent.relatedTarget)) {
  634. return;
  635. }
  636. type = 'cluster' + type;
  637. }
  638. L.FeatureGroup.prototype.fire.call(this, type, data, propagate);
  639. },
  640. //Override L.Evented.listens
  641. listens: function (type, propagate) {
  642. return L.FeatureGroup.prototype.listens.call(this, type, propagate) || L.FeatureGroup.prototype.listens.call(this, 'cluster' + type, propagate);
  643. },
  644. //Default functionality
  645. _defaultIconCreateFunction: function (cluster) {
  646. var childCount = cluster.getChildCount();
  647. var c = ' marker-cluster-';
  648. if (childCount < 10) {
  649. c += 'small';
  650. } else if (childCount < 100) {
  651. c += 'medium';
  652. } else {
  653. c += 'large';
  654. }
  655. return new L.DivIcon({ html: '<div><span>' + childCount + '</span></div>', className: 'marker-cluster' + c, iconSize: new L.Point(40, 40) });
  656. },
  657. _bindEvents: function () {
  658. var map = this._map,
  659. spiderfyOnMaxZoom = this.options.spiderfyOnMaxZoom,
  660. showCoverageOnHover = this.options.showCoverageOnHover,
  661. zoomToBoundsOnClick = this.options.zoomToBoundsOnClick;
  662. //Zoom on cluster click or spiderfy if we are at the lowest level
  663. if (spiderfyOnMaxZoom || zoomToBoundsOnClick) {
  664. this.on('clusterclick', this._zoomOrSpiderfy, this);
  665. }
  666. //Show convex hull (boundary) polygon on mouse over
  667. if (showCoverageOnHover) {
  668. this.on('clustermouseover', this._showCoverage, this);
  669. this.on('clustermouseout', this._hideCoverage, this);
  670. map.on('zoomend', this._hideCoverage, this);
  671. }
  672. },
  673. _zoomOrSpiderfy: function (e) {
  674. var cluster = e.layer,
  675. bottomCluster = cluster;
  676. while (bottomCluster._childClusters.length === 1) {
  677. bottomCluster = bottomCluster._childClusters[0];
  678. }
  679. if (bottomCluster._zoom === this._maxZoom &&
  680. bottomCluster._childCount === cluster._childCount &&
  681. this.options.spiderfyOnMaxZoom) {
  682. // All child markers are contained in a single cluster from this._maxZoom to this cluster.
  683. cluster.spiderfy();
  684. } else if (this.options.zoomToBoundsOnClick) {
  685. cluster.zoomToBounds();
  686. }
  687. // Focus the map again for keyboard users.
  688. if (e.originalEvent && e.originalEvent.keyCode === 13) {
  689. this._map._container.focus();
  690. }
  691. },
  692. _showCoverage: function (e) {
  693. var map = this._map;
  694. if (this._inZoomAnimation) {
  695. return;
  696. }
  697. if (this._shownPolygon) {
  698. map.removeLayer(this._shownPolygon);
  699. }
  700. if (e.layer.getChildCount() > 2 && e.layer !== this._spiderfied) {
  701. this._shownPolygon = new L.Polygon(e.layer.getConvexHull(), this.options.polygonOptions);
  702. map.addLayer(this._shownPolygon);
  703. }
  704. },
  705. _hideCoverage: function () {
  706. if (this._shownPolygon) {
  707. this._map.removeLayer(this._shownPolygon);
  708. this._shownPolygon = null;
  709. }
  710. },
  711. _unbindEvents: function () {
  712. var spiderfyOnMaxZoom = this.options.spiderfyOnMaxZoom,
  713. showCoverageOnHover = this.options.showCoverageOnHover,
  714. zoomToBoundsOnClick = this.options.zoomToBoundsOnClick,
  715. map = this._map;
  716. if (spiderfyOnMaxZoom || zoomToBoundsOnClick) {
  717. this.off('clusterclick', this._zoomOrSpiderfy, this);
  718. }
  719. if (showCoverageOnHover) {
  720. this.off('clustermouseover', this._showCoverage, this);
  721. this.off('clustermouseout', this._hideCoverage, this);
  722. map.off('zoomend', this._hideCoverage, this);
  723. }
  724. },
  725. _zoomEnd: function () {
  726. if (!this._map) { //May have been removed from the map by a zoomEnd handler
  727. return;
  728. }
  729. this._mergeSplitClusters();
  730. this._zoom = Math.round(this._map._zoom);
  731. this._currentShownBounds = this._getExpandedVisibleBounds();
  732. },
  733. _moveEnd: function () {
  734. if (this._inZoomAnimation) {
  735. return;
  736. }
  737. var newBounds = this._getExpandedVisibleBounds();
  738. this._topClusterLevel._recursivelyRemoveChildrenFromMap(this._currentShownBounds, Math.floor(this._map.getMinZoom()), this._zoom, newBounds);
  739. this._topClusterLevel._recursivelyAddChildrenToMap(null, Math.round(this._map._zoom), newBounds);
  740. this._currentShownBounds = newBounds;
  741. return;
  742. },
  743. _generateInitialClusters: function () {
  744. var maxZoom = Math.ceil(this._map.getMaxZoom()),
  745. minZoom = Math.floor(this._map.getMinZoom()),
  746. radius = this.options.maxClusterRadius,
  747. radiusFn = radius;
  748. //If we just set maxClusterRadius to a single number, we need to create
  749. //a simple function to return that number. Otherwise, we just have to
  750. //use the function we've passed in.
  751. if (typeof radius !== "function") {
  752. radiusFn = function () { return radius; };
  753. }
  754. if (this.options.disableClusteringAtZoom !== null) {
  755. maxZoom = this.options.disableClusteringAtZoom - 1;
  756. }
  757. this._maxZoom = maxZoom;
  758. this._gridClusters = {};
  759. this._gridUnclustered = {};
  760. //Set up DistanceGrids for each zoom
  761. for (var zoom = maxZoom; zoom >= minZoom; zoom--) {
  762. this._gridClusters[zoom] = new L.DistanceGrid(radiusFn(zoom));
  763. this._gridUnclustered[zoom] = new L.DistanceGrid(radiusFn(zoom));
  764. }
  765. // Instantiate the appropriate L.MarkerCluster class (animated or not).
  766. this._topClusterLevel = new this._markerCluster(this, minZoom - 1);
  767. },
  768. //Zoom: Zoom to start adding at (Pass this._maxZoom to start at the bottom)
  769. _addLayer: function (layer, zoom) {
  770. var gridClusters = this._gridClusters,
  771. gridUnclustered = this._gridUnclustered,
  772. minZoom = Math.floor(this._map.getMinZoom()),
  773. markerPoint, z;
  774. if (this.options.singleMarkerMode) {
  775. this._overrideMarkerIcon(layer);
  776. }
  777. layer.on(this._childMarkerEventHandlers, this);
  778. //Find the lowest zoom level to slot this one in
  779. for (; zoom >= minZoom; zoom--) {
  780. markerPoint = this._map.project(layer.getLatLng(), zoom); // calculate pixel position
  781. //Try find a cluster close by
  782. var closest = gridClusters[zoom].getNearObject(markerPoint);
  783. if (closest) {
  784. closest._addChild(layer);
  785. layer.__parent = closest;
  786. return;
  787. }
  788. //Try find a marker close by to form a new cluster with
  789. closest = gridUnclustered[zoom].getNearObject(markerPoint);
  790. if (closest) {
  791. var parent = closest.__parent;
  792. if (parent) {
  793. this._removeLayer(closest, false);
  794. }
  795. //Create new cluster with these 2 in it
  796. var newCluster = new this._markerCluster(this, zoom, closest, layer);
  797. gridClusters[zoom].addObject(newCluster, this._map.project(newCluster._cLatLng, zoom));
  798. closest.__parent = newCluster;
  799. layer.__parent = newCluster;
  800. //First create any new intermediate parent clusters that don't exist
  801. var lastParent = newCluster;
  802. for (z = zoom - 1; z > parent._zoom; z--) {
  803. lastParent = new this._markerCluster(this, z, lastParent);
  804. gridClusters[z].addObject(lastParent, this._map.project(closest.getLatLng(), z));
  805. }
  806. parent._addChild(lastParent);
  807. //Remove closest from this zoom level and any above that it is in, replace with newCluster
  808. this._removeFromGridUnclustered(closest, zoom);
  809. return;
  810. }
  811. //Didn't manage to cluster in at this zoom, record us as a marker here and continue upwards
  812. gridUnclustered[zoom].addObject(layer, markerPoint);
  813. }
  814. //Didn't get in anything, add us to the top
  815. this._topClusterLevel._addChild(layer);
  816. layer.__parent = this._topClusterLevel;
  817. return;
  818. },
  819. /**
  820. * Refreshes the icon of all "dirty" visible clusters.
  821. * Non-visible "dirty" clusters will be updated when they are added to the map.
  822. * @private
  823. */
  824. _refreshClustersIcons: function () {
  825. this._featureGroup.eachLayer(function (c) {
  826. if (c instanceof L.MarkerCluster && c._iconNeedsUpdate) {
  827. c._updateIcon();
  828. }
  829. });
  830. },
  831. //Enqueue code to fire after the marker expand/contract has happened
  832. _enqueue: function (fn) {
  833. this._queue.push(fn);
  834. if (!this._queueTimeout) {
  835. this._queueTimeout = setTimeout(L.bind(this._processQueue, this), 300);
  836. }
  837. },
  838. _processQueue: function () {
  839. for (var i = 0; i < this._queue.length; i++) {
  840. this._queue[i].call(this);
  841. }
  842. this._queue.length = 0;
  843. clearTimeout(this._queueTimeout);
  844. this._queueTimeout = null;
  845. },
  846. //Merge and split any existing clusters that are too big or small
  847. _mergeSplitClusters: function () {
  848. var mapZoom = Math.round(this._map._zoom);
  849. //In case we are starting to split before the animation finished
  850. this._processQueue();
  851. if (this._zoom < mapZoom && this._currentShownBounds.intersects(this._getExpandedVisibleBounds())) { //Zoom in, split
  852. this._animationStart();
  853. //Remove clusters now off screen
  854. this._topClusterLevel._recursivelyRemoveChildrenFromMap(this._currentShownBounds, Math.floor(this._map.getMinZoom()), this._zoom, this._getExpandedVisibleBounds());
  855. this._animationZoomIn(this._zoom, mapZoom);
  856. } else if (this._zoom > mapZoom) { //Zoom out, merge
  857. this._animationStart();
  858. this._animationZoomOut(this._zoom, mapZoom);
  859. } else {
  860. this._moveEnd();
  861. }
  862. },
  863. //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)
  864. _getExpandedVisibleBounds: function () {
  865. if (!this.options.removeOutsideVisibleBounds) {
  866. return this._mapBoundsInfinite;
  867. } else if (L.Browser.mobile) {
  868. return this._checkBoundsMaxLat(this._map.getBounds());
  869. }
  870. return this._checkBoundsMaxLat(this._map.getBounds().pad(1)); // Padding expands the bounds by its own dimensions but scaled with the given factor.
  871. },
  872. /**
  873. * Expands the latitude to Infinity (or -Infinity) if the input bounds reach the map projection maximum defined latitude
  874. * (in the case of Web/Spherical Mercator, it is 85.0511287798 / see https://en.wikipedia.org/wiki/Web_Mercator#Formulas).
  875. * Otherwise, the removeOutsideVisibleBounds option will remove markers beyond that limit, whereas the same markers without
  876. * this option (or outside MCG) will have their position floored (ceiled) by the projection and rendered at that limit,
  877. * making the user think that MCG "eats" them and never displays them again.
  878. * @param bounds L.LatLngBounds
  879. * @returns {L.LatLngBounds}
  880. * @private
  881. */
  882. _checkBoundsMaxLat: function (bounds) {
  883. var maxLat = this._maxLat;
  884. if (maxLat !== undefined) {
  885. if (bounds.getNorth() >= maxLat) {
  886. bounds._northEast.lat = Infinity;
  887. }
  888. if (bounds.getSouth() <= -maxLat) {
  889. bounds._southWest.lat = -Infinity;
  890. }
  891. }
  892. return bounds;
  893. },
  894. //Shared animation code
  895. _animationAddLayerNonAnimated: function (layer, newCluster) {
  896. if (newCluster === layer) {
  897. this._featureGroup.addLayer(layer);
  898. } else if (newCluster._childCount === 2) {
  899. newCluster._addToMap();
  900. var markers = newCluster.getAllChildMarkers();
  901. this._featureGroup.removeLayer(markers[0]);
  902. this._featureGroup.removeLayer(markers[1]);
  903. } else {
  904. newCluster._updateIcon();
  905. }
  906. },
  907. /**
  908. * Extracts individual (i.e. non-group) layers from a Layer Group.
  909. * @param group to extract layers from.
  910. * @param output {Array} in which to store the extracted layers.
  911. * @returns {*|Array}
  912. * @private
  913. */
  914. _extractNonGroupLayers: function (group, output) {
  915. var layers = group.getLayers(),
  916. i = 0,
  917. layer;
  918. output = output || [];
  919. for (; i < layers.length; i++) {
  920. layer = layers[i];
  921. if (layer instanceof L.LayerGroup) {
  922. this._extractNonGroupLayers(layer, output);
  923. continue;
  924. }
  925. output.push(layer);
  926. }
  927. return output;
  928. },
  929. /**
  930. * Implements the singleMarkerMode option.
  931. * @param layer Marker to re-style using the Clusters iconCreateFunction.
  932. * @returns {L.Icon} The newly created icon.
  933. * @private
  934. */
  935. _overrideMarkerIcon: function (layer) {
  936. var icon = layer.options.icon = this.options.iconCreateFunction({
  937. getChildCount: function () {
  938. return 1;
  939. },
  940. getAllChildMarkers: function () {
  941. return [layer];
  942. }
  943. });
  944. return icon;
  945. }
  946. });
  947. // Constant bounds used in case option "removeOutsideVisibleBounds" is set to false.
  948. L.MarkerClusterGroup.include({
  949. _mapBoundsInfinite: new L.LatLngBounds(new L.LatLng(-Infinity, -Infinity), new L.LatLng(Infinity, Infinity))
  950. });
  951. L.MarkerClusterGroup.include({
  952. _noAnimation: {
  953. //Non Animated versions of everything
  954. _animationStart: function () {
  955. //Do nothing...
  956. },
  957. _animationZoomIn: function (previousZoomLevel, newZoomLevel) {
  958. this._topClusterLevel._recursivelyRemoveChildrenFromMap(this._currentShownBounds, Math.floor(this._map.getMinZoom()), previousZoomLevel);
  959. this._topClusterLevel._recursivelyAddChildrenToMap(null, newZoomLevel, this._getExpandedVisibleBounds());
  960. //We didn't actually animate, but we use this event to mean "clustering animations have finished"
  961. this.fire('animationend');
  962. },
  963. _animationZoomOut: function (previousZoomLevel, newZoomLevel) {
  964. this._topClusterLevel._recursivelyRemoveChildrenFromMap(this._currentShownBounds, Math.floor(this._map.getMinZoom()), previousZoomLevel);
  965. this._topClusterLevel._recursivelyAddChildrenToMap(null, newZoomLevel, this._getExpandedVisibleBounds());
  966. //We didn't actually animate, but we use this event to mean "clustering animations have finished"
  967. this.fire('animationend');
  968. },
  969. _animationAddLayer: function (layer, newCluster) {
  970. this._animationAddLayerNonAnimated(layer, newCluster);
  971. }
  972. },
  973. _withAnimation: {
  974. //Animated versions here
  975. _animationStart: function () {
  976. this._map._mapPane.className += ' leaflet-cluster-anim';
  977. this._inZoomAnimation++;
  978. },
  979. _animationZoomIn: function (previousZoomLevel, newZoomLevel) {
  980. var bounds = this._getExpandedVisibleBounds(),
  981. fg = this._featureGroup,
  982. minZoom = Math.floor(this._map.getMinZoom()),
  983. i;
  984. this._ignoreMove = true;
  985. //Add all children of current clusters to map and remove those clusters from map
  986. this._topClusterLevel._recursively(bounds, previousZoomLevel, minZoom, function (c) {
  987. var startPos = c._latlng,
  988. markers = c._markers,
  989. m;
  990. if (!bounds.contains(startPos)) {
  991. startPos = null;
  992. }
  993. if (c._isSingleParent() && previousZoomLevel + 1 === newZoomLevel) { //Immediately add the new child and remove us
  994. fg.removeLayer(c);
  995. c._recursivelyAddChildrenToMap(null, newZoomLevel, bounds);
  996. } else {
  997. //Fade out old cluster
  998. c.clusterHide();
  999. c._recursivelyAddChildrenToMap(startPos, newZoomLevel, bounds);
  1000. }
  1001. //Remove all markers that aren't visible any more
  1002. //TODO: Do we actually need to do this on the higher levels too?
  1003. for (i = markers.length - 1; i >= 0; i--) {
  1004. m = markers[i];
  1005. if (!bounds.contains(m._latlng)) {
  1006. fg.removeLayer(m);
  1007. }
  1008. }
  1009. });
  1010. this._forceLayout();
  1011. //Update opacities
  1012. this._topClusterLevel._recursivelyBecomeVisible(bounds, newZoomLevel);
  1013. //TODO Maybe? Update markers in _recursivelyBecomeVisible
  1014. fg.eachLayer(function (n) {
  1015. if (!(n instanceof L.MarkerCluster) && n._icon) {
  1016. n.clusterShow();
  1017. }
  1018. });
  1019. //update the positions of the just added clusters/markers
  1020. this._topClusterLevel._recursively(bounds, previousZoomLevel, newZoomLevel, function (c) {
  1021. c._recursivelyRestoreChildPositions(newZoomLevel);
  1022. });
  1023. this._ignoreMove = false;
  1024. //Remove the old clusters and close the zoom animation
  1025. this._enqueue(function () {
  1026. //update the positions of the just added clusters/markers
  1027. this._topClusterLevel._recursively(bounds, previousZoomLevel, minZoom, function (c) {
  1028. fg.removeLayer(c);
  1029. c.clusterShow();
  1030. });
  1031. this._animationEnd();
  1032. });
  1033. },
  1034. _animationZoomOut: function (previousZoomLevel, newZoomLevel) {
  1035. this._animationZoomOutSingle(this._topClusterLevel, previousZoomLevel - 1, newZoomLevel);
  1036. //Need to add markers for those that weren't on the map before but are now
  1037. this._topClusterLevel._recursivelyAddChildrenToMap(null, newZoomLevel, this._getExpandedVisibleBounds());
  1038. //Remove markers that were on the map before but won't be now
  1039. this._topClusterLevel._recursivelyRemoveChildrenFromMap(this._currentShownBounds, Math.floor(this._map.getMinZoom()), previousZoomLevel, this._getExpandedVisibleBounds());
  1040. },
  1041. _animationAddLayer: function (layer, newCluster) {
  1042. var me = this,
  1043. fg = this._featureGroup;
  1044. fg.addLayer(layer);
  1045. if (newCluster !== layer) {
  1046. if (newCluster._childCount > 2) { //Was already a cluster
  1047. newCluster._updateIcon();
  1048. this._forceLayout();
  1049. this._animationStart();
  1050. layer._setPos(this._map.latLngToLayerPoint(newCluster.getLatLng()));
  1051. layer.clusterHide();
  1052. this._enqueue(function () {
  1053. fg.removeLayer(layer);
  1054. layer.clusterShow();
  1055. me._animationEnd();
  1056. });
  1057. } else { //Just became a cluster
  1058. this._forceLayout();
  1059. me._animationStart();
  1060. me._animationZoomOutSingle(newCluster, this._map.getMaxZoom(), this._zoom);
  1061. }
  1062. }
  1063. }
  1064. },
  1065. // Private methods for animated versions.
  1066. _animationZoomOutSingle: function (cluster, previousZoomLevel, newZoomLevel) {
  1067. var bounds = this._getExpandedVisibleBounds(),
  1068. minZoom = Math.floor(this._map.getMinZoom());
  1069. //Animate all of the markers in the clusters to move to their cluster center point
  1070. cluster._recursivelyAnimateChildrenInAndAddSelfToMap(bounds, minZoom, previousZoomLevel + 1, newZoomLevel);
  1071. var me = this;
  1072. //Update the opacity (If we immediately set it they won't animate)
  1073. this._forceLayout();
  1074. cluster._recursivelyBecomeVisible(bounds, newZoomLevel);
  1075. //TODO: Maybe use the transition timing stuff to make this more reliable
  1076. //When the animations are done, tidy up
  1077. this._enqueue(function () {
  1078. //This cluster stopped being a cluster before the timeout fired
  1079. if (cluster._childCount === 1) {
  1080. var m = cluster._markers[0];
  1081. //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
  1082. this._ignoreMove = true;
  1083. m.setLatLng(m.getLatLng());
  1084. this._ignoreMove = false;
  1085. if (m.clusterShow) {
  1086. m.clusterShow();
  1087. }
  1088. } else {
  1089. cluster._recursively(bounds, newZoomLevel, minZoom, function (c) {
  1090. c._recursivelyRemoveChildrenFromMap(bounds, minZoom, previousZoomLevel + 1);
  1091. });
  1092. }
  1093. me._animationEnd();
  1094. });
  1095. },
  1096. _animationEnd: function () {
  1097. if (this._map) {
  1098. this._map._mapPane.className = this._map._mapPane.className.replace(' leaflet-cluster-anim', '');
  1099. }
  1100. this._inZoomAnimation--;
  1101. this.fire('animationend');
  1102. },
  1103. //Force a browser layout of stuff in the map
  1104. // Should apply the current opacity and location to all elements so we can update them again for an animation
  1105. _forceLayout: function () {
  1106. //In my testing this works, infact offsetWidth of any element seems to work.
  1107. //Could loop all this._layers and do this for each _icon if it stops working
  1108. L.Util.falseFn(document.body.offsetWidth);
  1109. }
  1110. });
  1111. L.markerClusterGroup = function (options) {
  1112. return new L.MarkerClusterGroup(options);
  1113. };
  1114. L.MarkerCluster = L.Marker.extend({
  1115. initialize: function (group, zoom, a, b) {
  1116. L.Marker.prototype.initialize.call(this, a ? (a._cLatLng || a.getLatLng()) : new L.LatLng(0, 0),
  1117. { icon: this, pane: group.options.clusterPane });
  1118. this._group = group;
  1119. this._zoom = zoom;
  1120. this._markers = [];
  1121. this._childClusters = [];
  1122. this._childCount = 0;
  1123. this._iconNeedsUpdate = true;
  1124. this._boundsNeedUpdate = true;
  1125. this._bounds = new L.LatLngBounds();
  1126. if (a) {
  1127. this._addChild(a);
  1128. }
  1129. if (b) {
  1130. this._addChild(b);
  1131. }
  1132. },
  1133. //Recursively retrieve all child markers of this cluster
  1134. getAllChildMarkers: function (storageArray) {
  1135. storageArray = storageArray || [];
  1136. for (var i = this._childClusters.length - 1; i >= 0; i--) {
  1137. this._childClusters[i].getAllChildMarkers(storageArray);
  1138. }
  1139. for (var j = this._markers.length - 1; j >= 0; j--) {
  1140. storageArray.push(this._markers[j]);
  1141. }
  1142. return storageArray;
  1143. },
  1144. //Returns the count of how many child markers we have
  1145. getChildCount: function () {
  1146. return this._childCount;
  1147. },
  1148. //Zoom to the minimum of showing all of the child markers, or the extents of this cluster
  1149. zoomToBounds: function (fitBoundsOptions) {
  1150. var childClusters = this._childClusters.slice(),
  1151. map = this._group._map,
  1152. boundsZoom = map.getBoundsZoom(this._bounds),
  1153. zoom = this._zoom + 1,
  1154. mapZoom = map.getZoom(),
  1155. i;
  1156. //calculate how far we need to zoom down to see all of the markers
  1157. while (childClusters.length > 0 && boundsZoom > zoom) {
  1158. zoom++;
  1159. var newClusters = [];
  1160. for (i = 0; i < childClusters.length; i++) {
  1161. newClusters = newClusters.concat(childClusters[i]._childClusters);
  1162. }
  1163. childClusters = newClusters;
  1164. }
  1165. if (boundsZoom > zoom) {
  1166. this._group._map.setView(this._latlng, zoom);
  1167. } else if (boundsZoom <= mapZoom) { //If fitBounds wouldn't zoom us down, zoom us down instead
  1168. this._group._map.setView(this._latlng, mapZoom + 1);
  1169. } else {
  1170. this._group._map.fitBounds(this._bounds, fitBoundsOptions);
  1171. }
  1172. },
  1173. getBounds: function () {
  1174. var bounds = new L.LatLngBounds();
  1175. bounds.extend(this._bounds);
  1176. return bounds;
  1177. },
  1178. _updateIcon: function () {
  1179. this._iconNeedsUpdate = true;
  1180. if (this._icon) {
  1181. this.setIcon(this);
  1182. }
  1183. },
  1184. //Cludge for Icon, we pretend to be an icon for performance
  1185. createIcon: function () {
  1186. if (this._iconNeedsUpdate) {
  1187. this._iconObj = this._group.options.iconCreateFunction(this);
  1188. this._iconNeedsUpdate = false;
  1189. }
  1190. return this._iconObj.createIcon();
  1191. },
  1192. createShadow: function () {
  1193. return this._iconObj.createShadow();
  1194. },
  1195. _addChild: function (new1, isNotificationFromChild) {
  1196. this._iconNeedsUpdate = true;
  1197. this._boundsNeedUpdate = true;
  1198. this._setClusterCenter(new1);
  1199. if (new1 instanceof L.MarkerCluster) {
  1200. if (!isNotificationFromChild) {
  1201. this._childClusters.push(new1);
  1202. new1.__parent = this;
  1203. }
  1204. this._childCount += new1._childCount;
  1205. } else {
  1206. if (!isNotificationFromChild) {
  1207. this._markers.push(new1);
  1208. }
  1209. this._childCount++;
  1210. }
  1211. if (this.__parent) {
  1212. this.__parent._addChild(new1, true);
  1213. }
  1214. },
  1215. /**
  1216. * Makes sure the cluster center is set. If not, uses the child center if it is a cluster, or the marker position.
  1217. * @param child L.MarkerCluster|L.Marker that will be used as cluster center if not defined yet.
  1218. * @private
  1219. */
  1220. _setClusterCenter: function (child) {
  1221. if (!this._cLatLng) {
  1222. // when clustering, take position of the first point as the cluster center
  1223. this._cLatLng = child._cLatLng || child._latlng;
  1224. }
  1225. },
  1226. /**
  1227. * Assigns impossible bounding values so that the next extend entirely determines the new bounds.
  1228. * This method avoids having to trash the previous L.LatLngBounds object and to create a new one, which is much slower for this class.
  1229. * As long as the bounds are not extended, most other methods would probably fail, as they would with bounds initialized but not extended.
  1230. * @private
  1231. */
  1232. _resetBounds: function () {
  1233. var bounds = this._bounds;
  1234. if (bounds._southWest) {
  1235. bounds._southWest.lat = Infinity;
  1236. bounds._southWest.lng = Infinity;
  1237. }
  1238. if (bounds._northEast) {
  1239. bounds._northEast.lat = -Infinity;
  1240. bounds._northEast.lng = -Infinity;
  1241. }
  1242. },
  1243. _recalculateBounds: function () {
  1244. var markers = this._markers,
  1245. childClusters = this._childClusters,
  1246. latSum = 0,
  1247. lngSum = 0,
  1248. totalCount = this._childCount,
  1249. i, child, childLatLng, childCount;
  1250. // Case where all markers are removed from the map and we are left with just an empty _topClusterLevel.
  1251. if (totalCount === 0) {
  1252. return;
  1253. }
  1254. // Reset rather than creating a new object, for performance.
  1255. this._resetBounds();
  1256. // Child markers.
  1257. for (i = 0; i < markers.length; i++) {
  1258. childLatLng = markers[i]._latlng;
  1259. this._bounds.extend(childLatLng);
  1260. latSum += childLatLng.lat;
  1261. lngSum += childLatLng.lng;
  1262. }
  1263. // Child clusters.
  1264. for (i = 0; i < childClusters.length; i++) {
  1265. child = childClusters[i];
  1266. // Re-compute child bounds and weighted position first if necessary.
  1267. if (child._boundsNeedUpdate) {
  1268. child._recalculateBounds();
  1269. }
  1270. this._bounds.extend(child._bounds);
  1271. childLatLng = child._wLatLng;
  1272. childCount = child._childCount;
  1273. latSum += childLatLng.lat * childCount;
  1274. lngSum += childLatLng.lng * childCount;
  1275. }
  1276. this._latlng = this._wLatLng = new L.LatLng(latSum / totalCount, lngSum / totalCount);
  1277. // Reset dirty flag.
  1278. this._boundsNeedUpdate = false;
  1279. },
  1280. //Set our markers position as given and add it to the map
  1281. _addToMap: function (startPos) {
  1282. if (startPos) {
  1283. this._backupLatlng = this._latlng;
  1284. this.setLatLng(startPos);
  1285. }
  1286. this._group._featureGroup.addLayer(this);
  1287. },
  1288. _recursivelyAnimateChildrenIn: function (bounds, center, maxZoom) {
  1289. this._recursively(bounds, this._group._map.getMinZoom(), maxZoom - 1,
  1290. function (c) {
  1291. var markers = c._markers,
  1292. i, m;
  1293. for (i = markers.length - 1; i >= 0; i--) {
  1294. m = markers[i];
  1295. //Only do it if the icon is still on the map
  1296. if (m._icon) {
  1297. m._setPos(center);
  1298. m.clusterHide();
  1299. }
  1300. }
  1301. },
  1302. function (c) {
  1303. var childClusters = c._childClusters,
  1304. j, cm;
  1305. for (j = childClusters.length - 1; j >= 0; j--) {
  1306. cm = childClusters[j];
  1307. if (cm._icon) {
  1308. cm._setPos(center);
  1309. cm.clusterHide();
  1310. }
  1311. }
  1312. }
  1313. );
  1314. },
  1315. _recursivelyAnimateChildrenInAndAddSelfToMap: function (bounds, mapMinZoom, previousZoomLevel, newZoomLevel) {
  1316. this._recursively(bounds, newZoomLevel, mapMinZoom,
  1317. function (c) {
  1318. c._recursivelyAnimateChildrenIn(bounds, c._group._map.latLngToLayerPoint(c.getLatLng()).round(), previousZoomLevel);
  1319. //TODO: depthToAnimateIn affects _isSingleParent, if there is a multizoom we may/may not be.
  1320. //As a hack we only do a animation free zoom on a single level zoom, if someone does multiple levels then we always animate
  1321. if (c._isSingleParent() && previousZoomLevel - 1 === newZoomLevel) {
  1322. c.clusterShow();
  1323. c._recursivelyRemoveChildrenFromMap(bounds, mapMinZoom, previousZoomLevel); //Immediately remove our children as we are replacing them. TODO previousBounds not bounds
  1324. } else {
  1325. c.clusterHide();
  1326. }
  1327. c._addToMap();
  1328. }
  1329. );
  1330. },
  1331. _recursivelyBecomeVisible: function (bounds, zoomLevel) {
  1332. this._recursively(bounds, this._group._map.getMinZoom(), zoomLevel, null, function (c) {
  1333. c.clusterShow();
  1334. });
  1335. },
  1336. _recursivelyAddChildrenToMap: function (startPos, zoomLevel, bounds) {
  1337. this._recursively(bounds, this._group._map.getMinZoom() - 1, zoomLevel,
  1338. function (c) {
  1339. if (zoomLevel === c._zoom) {
  1340. return;
  1341. }
  1342. //Add our child markers at startPos (so they can be animated out)
  1343. for (var i = c._markers.length - 1; i >= 0; i--) {
  1344. var nm = c._markers[i];
  1345. if (!bounds.contains(nm._latlng)) {
  1346. continue;
  1347. }
  1348. if (startPos) {
  1349. nm._backupLatlng = nm.getLatLng();
  1350. nm.setLatLng(startPos);
  1351. if (nm.clusterHide) {
  1352. nm.clusterHide();
  1353. }
  1354. }
  1355. c._group._featureGroup.addLayer(nm);
  1356. }
  1357. },
  1358. function (c) {
  1359. c._addToMap(startPos);
  1360. }
  1361. );
  1362. },
  1363. _recursivelyRestoreChildPositions: function (zoomLevel) {
  1364. //Fix positions of child markers
  1365. for (var i = this._markers.length - 1; i >= 0; i--) {
  1366. var nm = this._markers[i];
  1367. if (nm._backupLatlng) {
  1368. nm.setLatLng(nm._backupLatlng);
  1369. delete nm._backupLatlng;
  1370. }
  1371. }
  1372. if (zoomLevel - 1 === this._zoom) {
  1373. //Reposition child clusters
  1374. for (var j = this._childClusters.length - 1; j >= 0; j--) {
  1375. this._childClusters[j]._restorePosition();
  1376. }
  1377. } else {
  1378. for (var k = this._childClusters.length - 1; k >= 0; k--) {
  1379. this._childClusters[k]._recursivelyRestoreChildPositions(zoomLevel);
  1380. }
  1381. }
  1382. },
  1383. _restorePosition: function () {
  1384. if (this._backupLatlng) {
  1385. this.setLatLng(this._backupLatlng);
  1386. delete this._backupLatlng;
  1387. }
  1388. },
  1389. //exceptBounds: If set, don't remove any markers/clusters in it
  1390. _recursivelyRemoveChildrenFromMap: function (previousBounds, mapMinZoom, zoomLevel, exceptBounds) {
  1391. var m, i;
  1392. this._recursively(previousBounds, mapMinZoom - 1, zoomLevel - 1,
  1393. function (c) {
  1394. //Remove markers at every level
  1395. for (i = c._markers.length - 1; i >= 0; i--) {
  1396. m = c._markers[i];
  1397. if (!exceptBounds || !exceptBounds.contains(m._latlng)) {
  1398. c._group._featureGroup.removeLayer(m);
  1399. if (m.clusterShow) {
  1400. m.clusterShow();
  1401. }
  1402. }
  1403. }
  1404. },
  1405. function (c) {
  1406. //Remove child clusters at just the bottom level
  1407. for (i = c._childClusters.length - 1; i >= 0; i--) {
  1408. m = c._childClusters[i];
  1409. if (!exceptBounds || !exceptBounds.contains(m._latlng)) {
  1410. c._group._featureGroup.removeLayer(m);
  1411. if (m.clusterShow) {
  1412. m.clusterShow();
  1413. }
  1414. }
  1415. }
  1416. }
  1417. );
  1418. },
  1419. //Run the given functions recursively to this and child clusters
  1420. // boundsToApplyTo: a L.LatLngBounds representing the bounds of what clusters to recurse in to
  1421. // zoomLevelToStart: zoom level to start running functions (inclusive)
  1422. // zoomLevelToStop: zoom level to stop running functions (inclusive)
  1423. // runAtEveryLevel: function that takes an L.MarkerCluster as an argument that should be applied on every level
  1424. // runAtBottomLevel: function that takes an L.MarkerCluster as an argument that should be applied at only the bottom level
  1425. _recursively: function (boundsToApplyTo, zoomLevelToStart, zoomLevelToStop, runAtEveryLevel, runAtBottomLevel) {
  1426. var childClusters = this._childClusters,
  1427. zoom = this._zoom,
  1428. i, c;
  1429. if (zoomLevelToStart <= zoom) {
  1430. if (runAtEveryLevel) {
  1431. runAtEveryLevel(this);
  1432. }
  1433. if (runAtBottomLevel && zoom === zoomLevelToStop) {
  1434. runAtBottomLevel(this);
  1435. }
  1436. }
  1437. if (zoom < zoomLevelToStart || zoom < zoomLevelToStop) {
  1438. for (i = childClusters.length - 1; i >= 0; i--) {
  1439. c = childClusters[i];
  1440. if (boundsToApplyTo.intersects(c._bounds)) {
  1441. c._recursively(boundsToApplyTo, zoomLevelToStart, zoomLevelToStop, runAtEveryLevel, runAtBottomLevel);
  1442. }
  1443. }
  1444. }
  1445. },
  1446. //Returns true if we are the parent of only one cluster and that cluster is the same as us
  1447. _isSingleParent: function () {
  1448. //Don't need to check this._markers as the rest won't work if there are any
  1449. return this._childClusters.length > 0 && this._childClusters[0]._childCount === this._childCount;
  1450. }
  1451. });
  1452. /*
  1453. * Extends L.Marker to include two extra methods: clusterHide and clusterShow.
  1454. *
  1455. * They work as setOpacity(0) and setOpacity(1) respectively, but
  1456. * they will remember the marker's opacity when hiding and showing it again.
  1457. *
  1458. */
  1459. L.Marker.include({
  1460. clusterHide: function () {
  1461. this.options.opacityWhenUnclustered = this.options.opacity || 1;
  1462. return this.setOpacity(0);
  1463. },
  1464. clusterShow: function () {
  1465. var ret = this.setOpacity(this.options.opacity || this.options.opacityWhenUnclustered);
  1466. delete this.options.opacityWhenUnclustered;
  1467. return ret;
  1468. }
  1469. });
  1470. L.DistanceGrid = function (cellSize) {
  1471. this._cellSize = cellSize;
  1472. this._sqCellSize = cellSize * cellSize;
  1473. this._grid = {};
  1474. this._objectPoint = { };
  1475. };
  1476. L.DistanceGrid.prototype = {
  1477. addObject: function (obj, point) {
  1478. var x = this._getCoord(point.x),
  1479. y = this._getCoord(point.y),
  1480. grid = this._grid,
  1481. row = grid[y] = grid[y] || {},
  1482. cell = row[x] = row[x] || [],
  1483. stamp = L.Util.stamp(obj);
  1484. this._objectPoint[stamp] = point;
  1485. cell.push(obj);
  1486. },
  1487. updateObject: function (obj, point) {
  1488. this.removeObject(obj);
  1489. this.addObject(obj, point);
  1490. },
  1491. //Returns true if the object was found
  1492. removeObject: function (obj, point) {
  1493. var x = this._getCoord(point.x),
  1494. y = this._getCoord(point.y),
  1495. grid = this._grid,
  1496. row = grid[y] = grid[y] || {},
  1497. cell = row[x] = row[x] || [],
  1498. i, len;
  1499. delete this._objectPoint[L.Util.stamp(obj)];
  1500. for (i = 0, len = cell.length; i < len; i++) {
  1501. if (cell[i] === obj) {
  1502. cell.splice(i, 1);
  1503. if (len === 1) {
  1504. delete row[x];
  1505. }
  1506. return true;
  1507. }
  1508. }
  1509. },
  1510. eachObject: function (fn, context) {
  1511. var i, j, k, len, row, cell, removed,
  1512. grid = this._grid;
  1513. for (i in grid) {
  1514. row = grid[i];
  1515. for (j in row) {
  1516. cell = row[j];
  1517. for (k = 0, len = cell.length; k < len; k++) {
  1518. removed = fn.call(context, cell[k]);
  1519. if (removed) {
  1520. k--;
  1521. len--;
  1522. }
  1523. }
  1524. }
  1525. }
  1526. },
  1527. getNearObject: function (point) {
  1528. var x = this._getCoord(point.x),
  1529. y = this._getCoord(point.y),
  1530. i, j, k, row, cell, len, obj, dist,
  1531. objectPoint = this._objectPoint,
  1532. closestDistSq = this._sqCellSize,
  1533. closest = null;
  1534. for (i = y - 1; i <= y + 1; i++) {
  1535. row = this._grid[i];
  1536. if (row) {
  1537. for (j = x - 1; j <= x + 1; j++) {
  1538. cell = row[j];
  1539. if (cell) {
  1540. for (k = 0, len = cell.length; k < len; k++) {
  1541. obj = cell[k];
  1542. dist = this._sqDist(objectPoint[L.Util.stamp(obj)], point);
  1543. if (dist < closestDistSq) {
  1544. closestDistSq = dist;
  1545. closest = obj;
  1546. }
  1547. }
  1548. }
  1549. }
  1550. }
  1551. }
  1552. return closest;
  1553. },
  1554. _getCoord: function (x) {
  1555. return Math.floor(x / this._cellSize);
  1556. },
  1557. _sqDist: function (p, p2) {
  1558. var dx = p2.x - p.x,
  1559. dy = p2.y - p.y;
  1560. return dx * dx + dy * dy;
  1561. }
  1562. };
  1563. /* Copyright (c) 2012 the authors listed at the following URL, and/or
  1564. the authors of referenced articles or incorporated external code:
  1565. http://en.literateprograms.org/Quickhull_(Javascript)?action=history&offset=20120410175256
  1566. Permission is hereby granted, free of charge, to any person obtaining
  1567. a copy of this software and associated documentation files (the
  1568. "Software"), to deal in the Software without restriction, including
  1569. without limitation the rights to use, copy, modify, merge, publish,
  1570. distribute, sublicense, and/or sell copies of the Software, and to
  1571. permit persons to whom the Software is furnished to do so, subject to
  1572. the following conditions:
  1573. The above copyright notice and this permission notice shall be
  1574. included in all copies or substantial portions of the Software.
  1575. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  1576. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  1577. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  1578. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  1579. CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  1580. TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  1581. SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  1582. Retrieved from: http://en.literateprograms.org/Quickhull_(Javascript)?oldid=18434
  1583. */
  1584. (function () {
  1585. L.QuickHull = {
  1586. /*
  1587. * @param {Object} cpt a point to be measured from the baseline
  1588. * @param {Array} bl the baseline, as represented by a two-element
  1589. * array of latlng objects.
  1590. * @returns {Number} an approximate distance measure
  1591. */
  1592. getDistant: function (cpt, bl) {
  1593. var vY = bl[1].lat - bl[0].lat,
  1594. vX = bl[0].lng - bl[1].lng;
  1595. return (vX * (cpt.lat - bl[0].lat) + vY * (cpt.lng - bl[0].lng));
  1596. },
  1597. /*
  1598. * @param {Array} baseLine a two-element array of latlng objects
  1599. * representing the baseline to project from
  1600. * @param {Array} latLngs an array of latlng objects
  1601. * @returns {Object} the maximum point and all new points to stay
  1602. * in consideration for the hull.
  1603. */
  1604. findMostDistantPointFromBaseLine: function (baseLine, latLngs) {
  1605. var maxD = 0,
  1606. maxPt = null,
  1607. newPoints = [],
  1608. i, pt, d;
  1609. for (i = latLngs.length - 1; i >= 0; i--) {
  1610. pt = latLngs[i];
  1611. d = this.getDistant(pt, baseLine);
  1612. if (d > 0) {
  1613. newPoints.push(pt);
  1614. } else {
  1615. continue;
  1616. }
  1617. if (d > maxD) {
  1618. maxD = d;
  1619. maxPt = pt;
  1620. }
  1621. }
  1622. return { maxPoint: maxPt, newPoints: newPoints };
  1623. },
  1624. /*
  1625. * Given a baseline, compute the convex hull of latLngs as an array
  1626. * of latLngs.
  1627. *
  1628. * @param {Array} latLngs
  1629. * @returns {Array}
  1630. */
  1631. buildConvexHull: function (baseLine, latLngs) {
  1632. var convexHullBaseLines = [],
  1633. t = this.findMostDistantPointFromBaseLine(baseLine, latLngs);
  1634. if (t.maxPoint) { // if there is still a point "outside" the base line
  1635. convexHullBaseLines =
  1636. convexHullBaseLines.concat(
  1637. this.buildConvexHull([baseLine[0], t.maxPoint], t.newPoints)
  1638. );
  1639. convexHullBaseLines =
  1640. convexHullBaseLines.concat(
  1641. this.buildConvexHull([t.maxPoint, baseLine[1]], t.newPoints)
  1642. );
  1643. return convexHullBaseLines;
  1644. } else { // if there is no more point "outside" the base line, the current base line is part of the convex hull
  1645. return [baseLine[0]];
  1646. }
  1647. },
  1648. /*
  1649. * Given an array of latlngs, compute a convex hull as an array
  1650. * of latlngs
  1651. *
  1652. * @param {Array} latLngs
  1653. * @returns {Array}
  1654. */
  1655. getConvexHull: function (latLngs) {
  1656. // find first baseline
  1657. var maxLat = false, minLat = false,
  1658. maxLng = false, minLng = false,
  1659. maxLatPt = null, minLatPt = null,
  1660. maxLngPt = null, minLngPt = null,
  1661. maxPt = null, minPt = null,
  1662. i;
  1663. for (i = latLngs.length - 1; i >= 0; i--) {
  1664. var pt = latLngs[i];
  1665. if (maxLat === false || pt.lat > maxLat) {
  1666. maxLatPt = pt;
  1667. maxLat = pt.lat;
  1668. }
  1669. if (minLat === false || pt.lat < minLat) {
  1670. minLatPt = pt;
  1671. minLat = pt.lat;
  1672. }
  1673. if (maxLng === false || pt.lng > maxLng) {
  1674. maxLngPt = pt;
  1675. maxLng = pt.lng;
  1676. }
  1677. if (minLng === false || pt.lng < minLng) {
  1678. minLngPt = pt;
  1679. minLng = pt.lng;
  1680. }
  1681. }
  1682. if (minLat !== maxLat) {
  1683. minPt = minLatPt;
  1684. maxPt = maxLatPt;
  1685. } else {
  1686. minPt = minLngPt;
  1687. maxPt = maxLngPt;
  1688. }
  1689. var ch = [].concat(this.buildConvexHull([minPt, maxPt], latLngs),
  1690. this.buildConvexHull([maxPt, minPt], latLngs));
  1691. return ch;
  1692. }
  1693. };
  1694. }());
  1695. L.MarkerCluster.include({
  1696. getConvexHull: function () {
  1697. var childMarkers = this.getAllChildMarkers(),
  1698. points = [],
  1699. p, i;
  1700. for (i = childMarkers.length - 1; i >= 0; i--) {
  1701. p = childMarkers[i].getLatLng();
  1702. points.push(p);
  1703. }
  1704. return L.QuickHull.getConvexHull(points);
  1705. }
  1706. });
  1707. //This code is 100% based on https://github.com/jawj/OverlappingMarkerSpiderfier-Leaflet
  1708. //Huge thanks to jawj for implementing it first to make my job easy :-)
  1709. L.MarkerCluster.include({
  1710. _2PI: Math.PI * 2,
  1711. _circleFootSeparation: 25, //related to circumference of circle
  1712. _circleStartAngle: Math.PI / 6,
  1713. _spiralFootSeparation: 28, //related to size of spiral (experiment!)
  1714. _spiralLengthStart: 11,
  1715. _spiralLengthFactor: 5,
  1716. _circleSpiralSwitchover: 9, //show spiral instead of circle from this marker count upwards.
  1717. // 0 -> always spiral; Infinity -> always circle
  1718. spiderfy: function () {
  1719. if (this._group._spiderfied === this || this._group._inZoomAnimation) {
  1720. return;
  1721. }
  1722. var childMarkers = this.getAllChildMarkers(),
  1723. group = this._group,
  1724. map = group._map,
  1725. center = map.latLngToLayerPoint(this._latlng),
  1726. positions;
  1727. this._group._unspiderfy();
  1728. this._group._spiderfied = this;
  1729. //TODO Maybe: childMarkers order by distance to center
  1730. if (childMarkers.length >= this._circleSpiralSwitchover) {
  1731. positions = this._generatePointsSpiral(childMarkers.length, center);
  1732. } else {
  1733. center.y += 10; // Otherwise circles look wrong => hack for standard blue icon, renders differently for other icons.
  1734. positions = this._generatePointsCircle(childMarkers.length, center);
  1735. }
  1736. this._animationSpiderfy(childMarkers, positions);
  1737. },
  1738. unspiderfy: function (zoomDetails) {
  1739. /// <param Name="zoomDetails">Argument from zoomanim if being called in a zoom animation or null otherwise</param>
  1740. if (this._group._inZoomAnimation) {
  1741. return;
  1742. }
  1743. this._animationUnspiderfy(zoomDetails);
  1744. this._group._spiderfied = null;
  1745. },
  1746. _generatePointsCircle: function (count, centerPt) {
  1747. var circumference = this._group.options.spiderfyDistanceMultiplier * this._circleFootSeparation * (2 + count),
  1748. legLength = circumference / this._2PI, //radius from circumference
  1749. angleStep = this._2PI / count,
  1750. res = [],
  1751. i, angle;
  1752. res.length = count;
  1753. for (i = count - 1; i >= 0; i--) {
  1754. angle = this._circleStartAngle + i * angleStep;
  1755. res[i] = new L.Point(centerPt.x + legLength * Math.cos(angle), centerPt.y + legLength * Math.sin(angle))._round();
  1756. }
  1757. return res;
  1758. },
  1759. _generatePointsSpiral: function (count, centerPt) {
  1760. var spiderfyDistanceMultiplier = this._group.options.spiderfyDistanceMultiplier,
  1761. legLength = spiderfyDistanceMultiplier * this._spiralLengthStart,
  1762. separation = spiderfyDistanceMultiplier * this._spiralFootSeparation,
  1763. lengthFactor = spiderfyDistanceMultiplier * this._spiralLengthFactor * this._2PI,
  1764. angle = 0,
  1765. res = [],
  1766. i;
  1767. res.length = count;
  1768. // Higher index, closer position to cluster center.
  1769. for (i = count - 1; i >= 0; i--) {
  1770. angle += separation / legLength + i * 0.0005;
  1771. res[i] = new L.Point(centerPt.x + legLength * Math.cos(angle), centerPt.y + legLength * Math.sin(angle))._round();
  1772. legLength += lengthFactor / angle;
  1773. }
  1774. return res;
  1775. },
  1776. _noanimationUnspiderfy: function () {
  1777. var group = this._group,
  1778. map = group._map,
  1779. fg = group._featureGroup,
  1780. childMarkers = this.getAllChildMarkers(),
  1781. m, i;
  1782. group._ignoreMove = true;
  1783. this.setOpacity(1);
  1784. for (i = childMarkers.length - 1; i >= 0; i--) {
  1785. m = childMarkers[i];
  1786. fg.removeLayer(m);
  1787. if (m._preSpiderfyLatlng) {
  1788. m.setLatLng(m._preSpiderfyLatlng);
  1789. delete m._preSpiderfyLatlng;
  1790. }
  1791. if (m.setZIndexOffset) {
  1792. m.setZIndexOffset(0);
  1793. }
  1794. if (m._spiderLeg) {
  1795. map.removeLayer(m._spiderLeg);
  1796. delete m._spiderLeg;
  1797. }
  1798. }
  1799. group.fire('unspiderfied', {
  1800. cluster: this,
  1801. markers: childMarkers
  1802. });
  1803. group._ignoreMove = false;
  1804. group._spiderfied = null;
  1805. }
  1806. });
  1807. //Non Animated versions of everything
  1808. L.MarkerClusterNonAnimated = L.MarkerCluster.extend({
  1809. _animationSpiderfy: function (childMarkers, positions) {
  1810. var group = this._group,
  1811. map = group._map,
  1812. fg = group._featureGroup,
  1813. legOptions = this._group.options.spiderLegPolylineOptions,
  1814. i, m, leg, newPos;
  1815. group._ignoreMove = true;
  1816. // Traverse in ascending order to make sure that inner circleMarkers are on top of further legs. Normal markers are re-ordered by newPosition.
  1817. // The reverse order trick no longer improves performance on modern browsers.
  1818. for (i = 0; i < childMarkers.length; i++) {
  1819. newPos = map.layerPointToLatLng(positions[i]);
  1820. m = childMarkers[i];
  1821. // Add the leg before the marker, so that in case the latter is a circleMarker, the leg is behind it.
  1822. leg = new L.Polyline([this._latlng, newPos], legOptions);
  1823. map.addLayer(leg);
  1824. m._spiderLeg = leg;
  1825. // Now add the marker.
  1826. m._preSpiderfyLatlng = m._latlng;
  1827. m.setLatLng(newPos);
  1828. if (m.setZIndexOffset) {
  1829. m.setZIndexOffset(1000000); //Make these appear on top of EVERYTHING
  1830. }
  1831. fg.addLayer(m);
  1832. }
  1833. this.setOpacity(0.3);
  1834. group._ignoreMove = false;
  1835. group.fire('spiderfied', {
  1836. cluster: this,
  1837. markers: childMarkers
  1838. });
  1839. },
  1840. _animationUnspiderfy: function () {
  1841. this._noanimationUnspiderfy();
  1842. }
  1843. });
  1844. //Animated versions here
  1845. L.MarkerCluster.include({
  1846. _animationSpiderfy: function (childMarkers, positions) {
  1847. var me = this,
  1848. group = this._group,
  1849. map = group._map,
  1850. fg = group._featureGroup,
  1851. thisLayerLatLng = this._latlng,
  1852. thisLayerPos = map.latLngToLayerPoint(thisLayerLatLng),
  1853. svg = L.Path.SVG,
  1854. legOptions = L.extend({}, this._group.options.spiderLegPolylineOptions), // Copy the options so that we can modify them for animation.
  1855. finalLegOpacity = legOptions.opacity,
  1856. i, m, leg, legPath, legLength, newPos;
  1857. if (finalLegOpacity === undefined) {
  1858. finalLegOpacity = L.MarkerClusterGroup.prototype.options.spiderLegPolylineOptions.opacity;
  1859. }
  1860. if (svg) {
  1861. // If the initial opacity of the spider leg is not 0 then it appears before the animation starts.
  1862. legOptions.opacity = 0;
  1863. // Add the class for CSS transitions.
  1864. legOptions.className = (legOptions.className || '') + ' leaflet-cluster-spider-leg';
  1865. } else {
  1866. // Make sure we have a defined opacity.
  1867. legOptions.opacity = finalLegOpacity;
  1868. }
  1869. group._ignoreMove = true;
  1870. // Add markers and spider legs to map, hidden at our center point.
  1871. // Traverse in ascending order to make sure that inner circleMarkers are on top of further legs. Normal markers are re-ordered by newPosition.
  1872. // The reverse order trick no longer improves performance on modern browsers.
  1873. for (i = 0; i < childMarkers.length; i++) {
  1874. m = childMarkers[i];
  1875. newPos = map.layerPointToLatLng(positions[i]);
  1876. // Add the leg before the marker, so that in case the latter is a circleMarker, the leg is behind it.
  1877. leg = new L.Polyline([thisLayerLatLng, newPos], legOptions);
  1878. map.addLayer(leg);
  1879. m._spiderLeg = leg;
  1880. // Explanations: https://jakearchibald.com/2013/animated-line-drawing-svg/
  1881. // In our case the transition property is declared in the CSS file.
  1882. if (svg) {
  1883. legPath = leg._path;
  1884. legLength = legPath.getTotalLength() + 0.1; // Need a small extra length to avoid remaining dot in Firefox.
  1885. legPath.style.strokeDasharray = legLength; // Just 1 length is enough, it will be duplicated.
  1886. legPath.style.strokeDashoffset = legLength;
  1887. }
  1888. // If it is a marker, add it now and we'll animate it out
  1889. if (m.setZIndexOffset) {
  1890. m.setZIndexOffset(1000000); // Make normal markers appear on top of EVERYTHING
  1891. }
  1892. if (m.clusterHide) {
  1893. m.clusterHide();
  1894. }
  1895. // Vectors just get immediately added
  1896. fg.addLayer(m);
  1897. if (m._setPos) {
  1898. m._setPos(thisLayerPos);
  1899. }
  1900. }
  1901. group._forceLayout();
  1902. group._animationStart();
  1903. // Reveal markers and spider legs.
  1904. for (i = childMarkers.length - 1; i >= 0; i--) {
  1905. newPos = map.layerPointToLatLng(positions[i]);
  1906. m = childMarkers[i];
  1907. //Move marker to new position
  1908. m._preSpiderfyLatlng = m._latlng;
  1909. m.setLatLng(newPos);
  1910. if (m.clusterShow) {
  1911. m.clusterShow();
  1912. }
  1913. // Animate leg (animation is actually delegated to CSS transition).
  1914. if (svg) {
  1915. leg = m._spiderLeg;
  1916. legPath = leg._path;
  1917. legPath.style.strokeDashoffset = 0;
  1918. //legPath.style.strokeOpacity = finalLegOpacity;
  1919. leg.setStyle({opacity: finalLegOpacity});
  1920. }
  1921. }
  1922. this.setOpacity(0.3);
  1923. group._ignoreMove = false;
  1924. setTimeout(function () {
  1925. group._animationEnd();
  1926. group.fire('spiderfied', {
  1927. cluster: me,
  1928. markers: childMarkers
  1929. });
  1930. }, 200);
  1931. },
  1932. _animationUnspiderfy: function (zoomDetails) {
  1933. var me = this,
  1934. group = this._group,
  1935. map = group._map,
  1936. fg = group._featureGroup,
  1937. thisLayerPos = zoomDetails ? map._latLngToNewLayerPoint(this._latlng, zoomDetails.zoom, zoomDetails.center) : map.latLngToLayerPoint(this._latlng),
  1938. childMarkers = this.getAllChildMarkers(),
  1939. svg = L.Path.SVG,
  1940. m, i, leg, legPath, legLength, nonAnimatable;
  1941. group._ignoreMove = true;
  1942. group._animationStart();
  1943. //Make us visible and bring the child markers back in
  1944. this.setOpacity(1);
  1945. for (i = childMarkers.length - 1; i >= 0; i--) {
  1946. m = childMarkers[i];
  1947. //Marker was added to us after we were spiderfied
  1948. if (!m._preSpiderfyLatlng) {
  1949. continue;
  1950. }
  1951. //Close any popup on the marker first, otherwise setting the location of the marker will make the map scroll
  1952. m.closePopup();
  1953. //Fix up the location to the real one
  1954. m.setLatLng(m._preSpiderfyLatlng);
  1955. delete m._preSpiderfyLatlng;
  1956. //Hack override the location to be our center
  1957. nonAnimatable = true;
  1958. if (m._setPos) {
  1959. m._setPos(thisLayerPos);
  1960. nonAnimatable = false;
  1961. }
  1962. if (m.clusterHide) {
  1963. m.clusterHide();
  1964. nonAnimatable = false;
  1965. }
  1966. if (nonAnimatable) {
  1967. fg.removeLayer(m);
  1968. }
  1969. // Animate the spider leg back in (animation is actually delegated to CSS transition).
  1970. if (svg) {
  1971. leg = m._spiderLeg;
  1972. legPath = leg._path;
  1973. legLength = legPath.getTotalLength() + 0.1;
  1974. legPath.style.strokeDashoffset = legLength;
  1975. leg.setStyle({opacity: 0});
  1976. }
  1977. }
  1978. group._ignoreMove = false;
  1979. setTimeout(function () {
  1980. //If we have only <= one child left then that marker will be shown on the map so don't remove it!
  1981. var stillThereChildCount = 0;
  1982. for (i = childMarkers.length - 1; i >= 0; i--) {
  1983. m = childMarkers[i];
  1984. if (m._spiderLeg) {
  1985. stillThereChildCount++;
  1986. }
  1987. }
  1988. for (i = childMarkers.length - 1; i >= 0; i--) {
  1989. m = childMarkers[i];
  1990. if (!m._spiderLeg) { //Has already been unspiderfied
  1991. continue;
  1992. }
  1993. if (m.clusterShow) {
  1994. m.clusterShow();
  1995. }
  1996. if (m.setZIndexOffset) {
  1997. m.setZIndexOffset(0);
  1998. }
  1999. if (stillThereChildCount > 1) {
  2000. fg.removeLayer(m);
  2001. }
  2002. map.removeLayer(m._spiderLeg);
  2003. delete m._spiderLeg;
  2004. }
  2005. group._animationEnd();
  2006. group.fire('unspiderfied', {
  2007. cluster: me,
  2008. markers: childMarkers
  2009. });
  2010. }, 200);
  2011. }
  2012. });
  2013. L.MarkerClusterGroup.include({
  2014. //The MarkerCluster currently spiderfied (if any)
  2015. _spiderfied: null,
  2016. unspiderfy: function () {
  2017. this._unspiderfy.apply(this, arguments);
  2018. },
  2019. _spiderfierOnAdd: function () {
  2020. this._map.on('click', this._unspiderfyWrapper, this);
  2021. if (this._map.options.zoomAnimation) {
  2022. this._map.on('zoomstart', this._unspiderfyZoomStart, this);
  2023. }
  2024. //Browsers without zoomAnimation or a big zoom don't fire zoomstart
  2025. this._map.on('zoomend', this._noanimationUnspiderfy, this);
  2026. if (!L.Browser.touch) {
  2027. this._map.getRenderer(this);
  2028. //Needs to happen in the pageload, not after, or animations don't work in webkit
  2029. // http://stackoverflow.com/questions/8455200/svg-animate-with-dynamically-added-elements
  2030. //Disable on touch browsers as the animation messes up on a touch zoom and isn't very noticable
  2031. }
  2032. },
  2033. _spiderfierOnRemove: function () {
  2034. this._map.off('click', this._unspiderfyWrapper, this);
  2035. this._map.off('zoomstart', this._unspiderfyZoomStart, this);
  2036. this._map.off('zoomanim', this._unspiderfyZoomAnim, this);
  2037. this._map.off('zoomend', this._noanimationUnspiderfy, this);
  2038. //Ensure that markers are back where they should be
  2039. // Use no animation to avoid a sticky leaflet-cluster-anim class on mapPane
  2040. this._noanimationUnspiderfy();
  2041. },
  2042. //On zoom start we add a zoomanim handler so that we are guaranteed to be last (after markers are animated)
  2043. //This means we can define the animation they do rather than Markers doing an animation to their actual location
  2044. _unspiderfyZoomStart: function () {
  2045. if (!this._map) { //May have been removed from the map by a zoomEnd handler
  2046. return;
  2047. }
  2048. this._map.on('zoomanim', this._unspiderfyZoomAnim, this);
  2049. },
  2050. _unspiderfyZoomAnim: function (zoomDetails) {
  2051. //Wait until the first zoomanim after the user has finished touch-zooming before running the animation
  2052. if (L.DomUtil.hasClass(this._map._mapPane, 'leaflet-touching')) {
  2053. return;
  2054. }
  2055. this._map.off('zoomanim', this._unspiderfyZoomAnim, this);
  2056. this._unspiderfy(zoomDetails);
  2057. },
  2058. _unspiderfyWrapper: function () {
  2059. /// <summary>_unspiderfy but passes no arguments</summary>
  2060. this._unspiderfy();
  2061. },
  2062. _unspiderfy: function (zoomDetails) {
  2063. if (this._spiderfied) {
  2064. this._spiderfied.unspiderfy(zoomDetails);
  2065. }
  2066. },
  2067. _noanimationUnspiderfy: function () {
  2068. if (this._spiderfied) {
  2069. this._spiderfied._noanimationUnspiderfy();
  2070. }
  2071. },
  2072. //If the given layer is currently being spiderfied then we unspiderfy it so it isn't on the map anymore etc
  2073. _unspiderfyLayer: function (layer) {
  2074. if (layer._spiderLeg) {
  2075. this._featureGroup.removeLayer(layer);
  2076. if (layer.clusterShow) {
  2077. layer.clusterShow();
  2078. }
  2079. //Position will be fixed up immediately in _animationUnspiderfy
  2080. if (layer.setZIndexOffset) {
  2081. layer.setZIndexOffset(0);
  2082. }
  2083. this._map.removeLayer(layer._spiderLeg);
  2084. delete layer._spiderLeg;
  2085. }
  2086. }
  2087. });
  2088. /**
  2089. * Adds 1 public method to MCG and 1 to L.Marker to facilitate changing
  2090. * markers' icon options and refreshing their icon and their parent clusters
  2091. * accordingly (case where their iconCreateFunction uses data of childMarkers
  2092. * to make up the cluster icon).
  2093. */
  2094. L.MarkerClusterGroup.include({
  2095. /**
  2096. * Updates the icon of all clusters which are parents of the given marker(s).
  2097. * In singleMarkerMode, also updates the given marker(s) icon.
  2098. * @param layers L.MarkerClusterGroup|L.LayerGroup|Array(L.Marker)|Map(L.Marker)|
  2099. * L.MarkerCluster|L.Marker (optional) list of markers (or single marker) whose parent
  2100. * clusters need to be updated. If not provided, retrieves all child markers of this.
  2101. * @returns {L.MarkerClusterGroup}
  2102. */
  2103. refreshClusters: function (layers) {
  2104. if (!layers) {
  2105. layers = this._topClusterLevel.getAllChildMarkers();
  2106. } else if (layers instanceof L.MarkerClusterGroup) {
  2107. layers = layers._topClusterLevel.getAllChildMarkers();
  2108. } else if (layers instanceof L.LayerGroup) {
  2109. layers = layers._layers;
  2110. } else if (layers instanceof L.MarkerCluster) {
  2111. layers = layers.getAllChildMarkers();
  2112. } else if (layers instanceof L.Marker) {
  2113. layers = [layers];
  2114. } // else: must be an Array(L.Marker)|Map(L.Marker)
  2115. this._flagParentsIconsNeedUpdate(layers);
  2116. this._refreshClustersIcons();
  2117. // In case of singleMarkerMode, also re-draw the markers.
  2118. if (this.options.singleMarkerMode) {
  2119. this._refreshSingleMarkerModeMarkers(layers);
  2120. }
  2121. return this;
  2122. },
  2123. /**
  2124. * Simply flags all parent clusters of the given markers as having a "dirty" icon.
  2125. * @param layers Array(L.Marker)|Map(L.Marker) list of markers.
  2126. * @private
  2127. */
  2128. _flagParentsIconsNeedUpdate: function (layers) {
  2129. var id, parent;
  2130. // Assumes layers is an Array or an Object whose prototype is non-enumerable.
  2131. for (id in layers) {
  2132. // Flag parent clusters' icon as "dirty", all the way up.
  2133. // Dumb process that flags multiple times upper parents, but still
  2134. // much more efficient than trying to be smart and make short lists,
  2135. // at least in the case of a hierarchy following a power law:
  2136. // http://jsperf.com/flag-nodes-in-power-hierarchy/2
  2137. parent = layers[id].__parent;
  2138. while (parent) {
  2139. parent._iconNeedsUpdate = true;
  2140. parent = parent.__parent;
  2141. }
  2142. }
  2143. },
  2144. /**
  2145. * Re-draws the icon of the supplied markers.
  2146. * To be used in singleMarkerMode only.
  2147. * @param layers Array(L.Marker)|Map(L.Marker) list of markers.
  2148. * @private
  2149. */
  2150. _refreshSingleMarkerModeMarkers: function (layers) {
  2151. var id, layer;
  2152. for (id in layers) {
  2153. layer = layers[id];
  2154. // Make sure we do not override markers that do not belong to THIS group.
  2155. if (this.hasLayer(layer)) {
  2156. // Need to re-create the icon first, then re-draw the marker.
  2157. layer.setIcon(this._overrideMarkerIcon(layer));
  2158. }
  2159. }
  2160. }
  2161. });
  2162. L.Marker.include({
  2163. /**
  2164. * Updates the given options in the marker's icon and refreshes the marker.
  2165. * @param options map object of icon options.
  2166. * @param directlyRefreshClusters boolean (optional) true to trigger
  2167. * MCG.refreshClustersOf() right away with this single marker.
  2168. * @returns {L.Marker}
  2169. */
  2170. refreshIconOptions: function (options, directlyRefreshClusters) {
  2171. var icon = this.options.icon;
  2172. L.setOptions(icon, options);
  2173. this.setIcon(icon);
  2174. // Shortcut to refresh the associated MCG clusters right away.
  2175. // To be used when refreshing a single marker.
  2176. // Otherwise, better use MCG.refreshClusters() once at the end with
  2177. // the list of modified markers.
  2178. if (directlyRefreshClusters && this.__parent) {
  2179. this.__parent._group.refreshClusters(this);
  2180. }
  2181. return this;
  2182. }
  2183. });

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/234901
推荐阅读
相关标签
  

闽ICP备14008679号