1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731 |
- (function (global, factory) {
- typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
- typeof define === 'function' && define.amd ? define(['exports'], factory) :
- (factory((global.BM = global.BM || {}, global.BM.markercluster = global.BM.markercluster || {})));
- }(this, (function (exports) {
- 'use strict';
- var MarkerClusterGroup = BM.MarkerClusterGroup = BM.FeatureGroup.extend({
- options: {
- maxClusterRadius: 80, //A cluster will cover at most this many pixels from its center
- iconCreateFunction: null,
- clusterPane: BM.Marker.prototype.options.pane,
- spiderfyOnMaxZoom: true,
- showCoverageOnHover: true,
- zoomToBoundsOnClick: true,
- singleMarkerMode: false,
- disableClusteringAtZoom: null,
- // Setting this to false prevents the removal of any clusters outside of the viewpoint, which
- // is the default behaviour for performance reasons.
- removeOutsideVisibleBounds: true,
- // Set to false to disable all animations (zoom and spiderfy).
- // If false, option animateAddingMarkers below has no effect.
- // If BM.DomUtil.TRANSITION is falsy, this option has no effect.
- animate: true,
- //Whether to animate adding markers after adding the MarkerClusterGroup to the map
- // If you are adding individual markers set to true, if adding bulk markers leave false for massive performance gains.
- animateAddingMarkers: false,
- //Increase to increase the distance away that spiderfied markers appear from the center
- spiderfyDistanceMultiplier: 1,
- // Make it possible to specify a polyline options on a spider leg
- spiderLegPolylineOptions: {
- weight: 1.5,
- color: '#222',
- opacity: 0.5
- },
- // 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
- chunkedLoading: false,
- chunkInterval: 200, // process markers for a maximum of ~ n milliseconds (then trigger the chunkProgress callback)
- chunkDelay: 50, // at the end of each interval, give n milliseconds back to system/browser
- chunkProgress: null, // progress callback: function(processed, total, elapsed) (e.g. for a progress indicator)
- //Options to pass to the BM.Polygon constructor
- polygonOptions: {}
- },
- initialize: function (options) {
- BM.Util.setOptions(this, options);
- if (!this.options.iconCreateFunction) {
- this.options.iconCreateFunction = this._defaultIconCreateFunction;
- }
- this._featureGroup = BM.featureGroup();
- this._featureGroup.addEventParent(this);
- this._nonPointGroup = BM.featureGroup();
- this._nonPointGroup.addEventParent(this);
- this._inZoomAnimation = 0;
- this._needsClustering = [];
- this._needsRemoving = []; //Markers removed while we aren't on the map need to be kept track of
- //The bounds of the currently shown area (from _getExpandedVisibleBounds) Updated on zoom/move
- this._currentShownBounds = null;
- this._queue = [];
- this._childMarkerEventHandlers = {
- 'dragstart': this._childMarkerDragStart,
- 'move': this._childMarkerMoved,
- 'dragend': this._childMarkerDragEnd,
- };
- // Hook the appropriate animation methods.
- var animate = BM.DomUtil.TRANSITION && this.options.animate;
- BM.extend(this, animate ? this._withAnimation : this._noAnimation);
- // Remember which MarkerCluster class to instantiate (animated or not).
- this._markerCluster = animate ? BM.MarkerCluster : BM.MarkerClusterNonAnimated;
- },
- addLayer: function (layer) {
- if (layer instanceof BM.LayerGroup) {
- return this.addLayers([layer]);
- }
- //Don't cluster non point data
- if (!layer.getLatLng) {
- this._nonPointGroup.addLayer(layer);
- this.fire('layeradd', {
- layer: layer
- });
- return this;
- }
- if (!this._map) {
- this._needsClustering.push(layer);
- this.fire('layeradd', {
- layer: layer
- });
- return this;
- }
- if (this.hasLayer(layer)) {
- return this;
- }
- //If we have already clustered we'll need to add this one to a cluster
- if (this._unspiderfy) {
- this._unspiderfy();
- }
- this._addLayer(layer, this._maxZoom);
- this.fire('layeradd', {
- layer: layer
- });
- // Refresh bounds and weighted positions.
- this._topClusterLevel._recalculateBounds();
- this._refreshClustersIcons();
- //Work out what is visible
- var visibleLayer = layer,
- currentZoom = this._zoom;
- if (layer.__parent) {
- while (visibleLayer.__parent._zoom >= currentZoom) {
- visibleLayer = visibleLayer.__parent;
- }
- }
- if (this._currentShownBounds.contains(visibleLayer.getLatLng())) {
- if (this.options.animateAddingMarkers) {
- this._animationAddLayer(layer, visibleLayer);
- } else {
- this._animationAddLayerNonAnimated(layer, visibleLayer);
- }
- }
- return this;
- },
- removeLayer: function (layer) {
- if (layer instanceof BM.LayerGroup) {
- return this.removeLayers([layer]);
- }
- //Non point layers
- if (!layer.getLatLng) {
- this._nonPointGroup.removeLayer(layer);
- this.fire('layerremove', {
- layer: layer
- });
- return this;
- }
- if (!this._map) {
- if (!this._arraySplice(this._needsClustering, layer) && this.hasLayer(layer)) {
- this._needsRemoving.push({
- layer: layer,
- latlng: layer._latlng
- });
- }
- this.fire('layerremove', {
- layer: layer
- });
- return this;
- }
- if (!layer.__parent) {
- return this;
- }
- if (this._unspiderfy) {
- this._unspiderfy();
- this._unspiderfyLayer(layer);
- }
- //Remove the marker from clusters
- this._removeLayer(layer, true);
- this.fire('layerremove', {
- layer: layer
- });
- // Refresh bounds and weighted positions.
- this._topClusterLevel._recalculateBounds();
- this._refreshClustersIcons();
- layer.off(this._childMarkerEventHandlers, this);
- if (this._featureGroup.hasLayer(layer)) {
- this._featureGroup.removeLayer(layer);
- if (layer.clusterShow) {
- layer.clusterShow();
- }
- }
- return this;
- },
- //Takes an array of markers and adds them in bulk
- addLayers: function (layersArray, skipLayerAddEvent) {
- if (!BM.Util.isArray(layersArray)) {
- return this.addLayer(layersArray);
- }
- var fg = this._featureGroup,
- npg = this._nonPointGroup,
- chunked = this.options.chunkedLoading,
- chunkInterval = this.options.chunkInterval,
- chunkProgress = this.options.chunkProgress,
- l = layersArray.length,
- offset = 0,
- originalArray = true,
- m;
- if (this._map) {
- var started = (new Date()).getTime();
- var process = BM.bind(function () {
- var start = (new Date()).getTime();
- for (; offset < l; offset++) {
- if (chunked && offset % 200 === 0) {
- // every couple hundred markers, instrument the time elapsed since processing started:
- var elapsed = (new Date()).getTime() - start;
- if (elapsed > chunkInterval) {
- break; // been working too hard, time to take a break :-)
- }
- }
- m = layersArray[offset];
- // Group of layers, append children to layersArray and skip.
- // Side effects:
- // - Total increases, so chunkProgress ratio jumps backward.
- // - Groups are not included in this group, only their non-group child layers (hasLayer).
- // Changing array length while looping does not affect performance in current browsers:
- // http://jsperf.com/for-loop-changing-length/6
- if (m instanceof BM.LayerGroup) {
- if (originalArray) {
- layersArray = layersArray.slice();
- originalArray = false;
- }
- this._extractNonGroupLayers(m, layersArray);
- l = layersArray.length;
- continue;
- }
- //Not point data, can't be clustered
- if (!m.getLatLng) {
- npg.addLayer(m);
- if (!skipLayerAddEvent) {
- this.fire('layeradd', {
- layer: m
- });
- }
- continue;
- }
- if (this.hasLayer(m)) {
- continue;
- }
- this._addLayer(m, this._maxZoom);
- if (!skipLayerAddEvent) {
- this.fire('layeradd', {
- layer: m
- });
- }
- //If we just made a cluster of size 2 then we need to remove the other marker from the map (if it is) or we never will
- if (m.__parent) {
- if (m.__parent.getChildCount() === 2) {
- var markers = m.__parent.getAllChildMarkers(),
- otherMarker = markers[0] === m ? markers[1] : markers[0];
- fg.removeLayer(otherMarker);
- }
- }
- }
- if (chunkProgress) {
- // report progress and time elapsed:
- chunkProgress(offset, l, (new Date()).getTime() - started);
- }
- // Completed processing all markers.
- if (offset === l) {
- // Refresh bounds and weighted positions.
- this._topClusterLevel._recalculateBounds();
- this._refreshClustersIcons();
- this._topClusterLevel._recursivelyAddChildrenToMap(null, this._zoom, this._currentShownBounds);
- } else {
- setTimeout(process, this.options.chunkDelay);
- }
- }, this);
- process();
- } else {
- var needsClustering = this._needsClustering;
- for (; offset < l; offset++) {
- m = layersArray[offset];
- // Group of layers, append children to layersArray and skip.
- if (m instanceof BM.LayerGroup) {
- if (originalArray) {
- layersArray = layersArray.slice();
- originalArray = false;
- }
- this._extractNonGroupLayers(m, layersArray);
- l = layersArray.length;
- continue;
- }
- //Not point data, can't be clustered
- if (!m.getLatLng) {
- npg.addLayer(m);
- continue;
- }
- if (this.hasLayer(m)) {
- continue;
- }
- needsClustering.push(m);
- }
- }
- return this;
- },
- //Takes an array of markers and removes them in bulk
- removeLayers: function (layersArray) {
- var i, m,
- l = layersArray.length,
- fg = this._featureGroup,
- npg = this._nonPointGroup,
- originalArray = true;
- if (!this._map) {
- for (i = 0; i < l; i++) {
- m = layersArray[i];
- // Group of layers, append children to layersArray and skip.
- if (m instanceof BM.LayerGroup) {
- if (originalArray) {
- layersArray = layersArray.slice();
- originalArray = false;
- }
- this._extractNonGroupLayers(m, layersArray);
- l = layersArray.length;
- continue;
- }
- this._arraySplice(this._needsClustering, m);
- npg.removeLayer(m);
- if (this.hasLayer(m)) {
- this._needsRemoving.push({
- layer: m,
- latlng: m._latlng
- });
- }
- this.fire('layerremove', {
- layer: m
- });
- }
- return this;
- }
- if (this._unspiderfy) {
- this._unspiderfy();
- // Work on a copy of the array, so that next loop is not affected.
- var layersArray2 = layersArray.slice(),
- l2 = l;
- for (i = 0; i < l2; i++) {
- m = layersArray2[i];
- // Group of layers, append children to layersArray and skip.
- if (m instanceof BM.LayerGroup) {
- this._extractNonGroupLayers(m, layersArray2);
- l2 = layersArray2.length;
- continue;
- }
- this._unspiderfyLayer(m);
- }
- }
- for (i = 0; i < l; i++) {
- m = layersArray[i];
- // Group of layers, append children to layersArray and skip.
- if (m instanceof BM.LayerGroup) {
- if (originalArray) {
- layersArray = layersArray.slice();
- originalArray = false;
- }
- this._extractNonGroupLayers(m, layersArray);
- l = layersArray.length;
- continue;
- }
- if (!m.__parent) {
- npg.removeLayer(m);
- this.fire('layerremove', {
- layer: m
- });
- continue;
- }
- this._removeLayer(m, true, true);
- this.fire('layerremove', {
- layer: m
- });
- if (fg.hasLayer(m)) {
- fg.removeLayer(m);
- if (m.clusterShow) {
- m.clusterShow();
- }
- }
- }
- // Refresh bounds and weighted positions.
- this._topClusterLevel._recalculateBounds();
- this._refreshClustersIcons();
- //Fix up the clusters and markers on the map
- this._topClusterLevel._recursivelyAddChildrenToMap(null, this._zoom, this._currentShownBounds);
- return this;
- },
- //Removes all layers from the MarkerClusterGroup
- clearLayers: function () {
- //Need our own special implementation as the LayerGroup one doesn't work for us
- //If we aren't on the map (yet), blow away the markers we know of
- if (!this._map) {
- this._needsClustering = [];
- this._needsRemoving = [];
- delete this._gridClusters;
- delete this._gridUnclustered;
- }
- if (this._noanimationUnspiderfy) {
- this._noanimationUnspiderfy();
- }
- //Remove all the visible layers
- this._featureGroup.clearLayers();
- this._nonPointGroup.clearLayers();
- this.eachLayer(function (marker) {
- marker.off(this._childMarkerEventHandlers, this);
- delete marker.__parent;
- }, this);
- if (this._map) {
- //Reset _topClusterLevel and the DistanceGrids
- this._generateInitialClusters();
- }
- return this;
- },
- //Override FeatureGroup.getBounds as it doesn't work
- getBounds: function () {
- var bounds = new BM.LatLngBounds();
- if (this._topClusterLevel) {
- bounds.extend(this._topClusterLevel._bounds);
- }
- for (var i = this._needsClustering.length - 1; i >= 0; i--) {
- bounds.extend(this._needsClustering[i].getLatLng());
- }
- bounds.extend(this._nonPointGroup.getBounds());
- return bounds;
- },
- //Overrides LayerGroup.eachLayer
- eachLayer: function (method, context) {
- var markers = this._needsClustering.slice(),
- needsRemoving = this._needsRemoving,
- thisNeedsRemoving, i, j;
- if (this._topClusterLevel) {
- this._topClusterLevel.getAllChildMarkers(markers);
- }
- for (i = markers.length - 1; i >= 0; i--) {
- thisNeedsRemoving = true;
- for (j = needsRemoving.length - 1; j >= 0; j--) {
- if (needsRemoving[j].layer === markers[i]) {
- thisNeedsRemoving = false;
- break;
- }
- }
- if (thisNeedsRemoving) {
- method.call(context, markers[i]);
- }
- }
- this._nonPointGroup.eachLayer(method, context);
- },
- //Overrides LayerGroup.getLayers
- getLayers: function () {
- var layers = [];
- this.eachLayer(function (l) {
- layers.push(l);
- });
- return layers;
- },
- //Overrides LayerGroup.getLayer, WARNING: Really bad performance
- getLayer: function (id) {
- var result = null;
- id = parseInt(id, 10);
- this.eachLayer(function (l) {
- if (BM.stamp(l) === id) {
- result = l;
- }
- });
- return result;
- },
- //Returns true if the given layer is in this MarkerClusterGroup
- hasLayer: function (layer) {
- if (!layer) {
- return false;
- }
- var i, anArray = this._needsClustering;
- for (i = anArray.length - 1; i >= 0; i--) {
- if (anArray[i] === layer) {
- return true;
- }
- }
- anArray = this._needsRemoving;
- for (i = anArray.length - 1; i >= 0; i--) {
- if (anArray[i].layer === layer) {
- return false;
- }
- }
- return !!(layer.__parent && layer.__parent._group === this) || this._nonPointGroup.hasLayer(layer);
- },
- //Zoom down to show the given layer (spiderfying if necessary) then calls the callback
- zoomToShowLayer: function (layer, callback) {
- if (typeof callback !== 'function') {
- callback = function () {};
- }
- var showMarker = function () {
- if ((layer._icon || layer.__parent._icon) && !this._inZoomAnimation) {
- this._map.off('moveend', showMarker, this);
- this.off('animationend', showMarker, this);
- if (layer._icon) {
- callback();
- } else if (layer.__parent._icon) {
- this.once('spiderfied', callback, this);
- layer.__parent.spiderfy();
- }
- }
- };
- if (layer._icon && this._map.getBounds().contains(layer.getLatLng())) {
- //Layer is visible ond on screen, immediate return
- callback();
- } else if (layer.__parent._zoom < Math.round(this._map._zoom)) {
- //Layer should be visible at this zoom level. It must not be on screen so just pan over to it
- this._map.on('moveend', showMarker, this);
- this._map.panTo(layer.getLatLng());
- } else {
- this._map.on('moveend', showMarker, this);
- this.on('animationend', showMarker, this);
- layer.__parent.zoomToBounds();
- }
- },
- //Overrides FeatureGroup.onAdd
- onAdd: function (map) {
- this._map = map;
- var i, l, layer;
- if (!isFinite(this._map.getMaxZoom())) {
- throw "Map has no maxZoom specified";
- }
- this._featureGroup.addTo(map);
- this._nonPointGroup.addTo(map);
- if (!this._gridClusters) {
- this._generateInitialClusters();
- }
- this._maxLat = map.options.crs.projection.MAX_LATITUDE;
- //Restore all the positions as they are in the MCG before removing them
- for (i = 0, l = this._needsRemoving.length; i < l; i++) {
- layer = this._needsRemoving[i];
- layer.newlatlng = layer.layer._latlng;
- layer.layer._latlng = layer.latlng;
- }
- //Remove them, then restore their new positions
- for (i = 0, l = this._needsRemoving.length; i < l; i++) {
- layer = this._needsRemoving[i];
- this._removeLayer(layer.layer, true);
- layer.layer._latlng = layer.newlatlng;
- }
- this._needsRemoving = [];
- //Remember the current zoom level and bounds
- this._zoom = Math.round(this._map._zoom);
- this._currentShownBounds = this._getExpandedVisibleBounds();
- this._map.on('zoomend', this._zoomEnd, this);
- this._map.on('moveend', this._moveEnd, this);
- if (this._spiderfierOnAdd) { //TODO FIXME: Not sure how to have spiderfier add something on here nicely
- this._spiderfierOnAdd();
- }
- this._bindEvents();
- //Actually add our markers to the map:
- l = this._needsClustering;
- this._needsClustering = [];
- this.addLayers(l, true);
- },
- //Overrides FeatureGroup.onRemove
- onRemove: function (map) {
- map.off('zoomend', this._zoomEnd, this);
- map.off('moveend', this._moveEnd, this);
- this._unbindEvents();
- //In case we are in a cluster animation
- this._map._mapPane.className = this._map._mapPane.className.replace(' bigemap-cluster-anim', '');
- if (this._spiderfierOnRemove) { //TODO FIXME: Not sure how to have spiderfier add something on here nicely
- this._spiderfierOnRemove();
- }
- delete this._maxLat;
- //Clean up all the layers we added to the map
- this._hideCoverage();
- this._featureGroup.remove();
- this._nonPointGroup.remove();
- this._featureGroup.clearLayers();
- this._map = null;
- },
- getVisibleParent: function (marker) {
- var vMarker = marker;
- while (vMarker && !vMarker._icon) {
- vMarker = vMarker.__parent;
- }
- return vMarker || null;
- },
- //Remove the given object from the given array
- _arraySplice: function (anArray, obj) {
- for (var i = anArray.length - 1; i >= 0; i--) {
- if (anArray[i] === obj) {
- anArray.splice(i, 1);
- return true;
- }
- }
- },
- /**
- * Removes a marker from all _gridUnclustered zoom levels, starting at the supplied zoom.
- * @param marker to be removed from _gridUnclustered.
- * @param z integer bottom start zoom level (included)
- * @private
- */
- _removeFromGridUnclustered: function (marker, z) {
- var map = this._map,
- gridUnclustered = this._gridUnclustered,
- minZoom = Math.floor(this._map.getMinZoom());
- for (; z >= minZoom; z--) {
- if (!gridUnclustered[z].removeObject(marker, map.project(marker.getLatLng(), z))) {
- break;
- }
- }
- },
- _childMarkerDragStart: function (e) {
- e.target.__dragStart = e.target._latlng;
- },
- _childMarkerMoved: function (e) {
- if (!this._ignoreMove && !e.target.__dragStart) {
- var isPopupOpen = e.target._popup && e.target._popup.isOpen();
- this._moveChild(e.target, e.oldLatLng, e.latlng);
- if (isPopupOpen) {
- e.target.openPopup();
- }
- }
- },
- _moveChild: function (layer, from, to) {
- layer._latlng = from;
- this.removeLayer(layer);
- layer._latlng = to;
- this.addLayer(layer);
- },
- _childMarkerDragEnd: function (e) {
- var dragStart = e.target.__dragStart;
- delete e.target.__dragStart;
- if (dragStart) {
- this._moveChild(e.target, dragStart, e.target._latlng);
- }
- },
- //Internal function for removing a marker from everything.
- //dontUpdateMap: set to true if you will handle updating the map manually (for bulk functions)
- _removeLayer: function (marker, removeFromDistanceGrid, dontUpdateMap) {
- var gridClusters = this._gridClusters,
- gridUnclustered = this._gridUnclustered,
- fg = this._featureGroup,
- map = this._map,
- minZoom = Math.floor(this._map.getMinZoom());
- //Remove the marker from distance clusters it might be in
- if (removeFromDistanceGrid) {
- this._removeFromGridUnclustered(marker, this._maxZoom);
- }
- //Work our way up the clusters removing them as we go if required
- var cluster = marker.__parent,
- markers = cluster._markers,
- otherMarker;
- //Remove the marker from the immediate parents marker list
- this._arraySplice(markers, marker);
- while (cluster) {
- cluster._childCount--;
- cluster._boundsNeedUpdate = true;
- if (cluster._zoom < minZoom) {
- //Top level, do nothing
- break;
- } else if (removeFromDistanceGrid && cluster._childCount <= 1) { //Cluster no longer required
- //We need to push the other marker up to the parent
- otherMarker = cluster._markers[0] === marker ? cluster._markers[1] : cluster._markers[0];
- //Update distance grid
- gridClusters[cluster._zoom].removeObject(cluster, map.project(cluster._cLatLng, cluster._zoom));
- gridUnclustered[cluster._zoom].addObject(otherMarker, map.project(otherMarker.getLatLng(), cluster._zoom));
- //Move otherMarker up to parent
- this._arraySplice(cluster.__parent._childClusters, cluster);
- cluster.__parent._markers.push(otherMarker);
- otherMarker.__parent = cluster.__parent;
- if (cluster._icon) {
- //Cluster is currently on the map, need to put the marker on the map instead
- fg.removeLayer(cluster);
- if (!dontUpdateMap) {
- fg.addLayer(otherMarker);
- }
- }
- } else {
- cluster._iconNeedsUpdate = true;
- }
- cluster = cluster.__parent;
- }
- delete marker.__parent;
- },
- _isOrIsParent: function (el, oel) {
- while (oel) {
- if (el === oel) {
- return true;
- }
- oel = oel.parentNode;
- }
- return false;
- },
- //Override BM.Evented.fire
- fire: function (type, data, propagate) {
- if (data && data.layer instanceof BM.MarkerCluster) {
- //Prevent multiple clustermouseover/off events if the icon is made up of stacked divs (Doesn't work in ie <= 8, no relatedTarget)
- if (data.originalEvent && this._isOrIsParent(data.layer._icon, data.originalEvent.relatedTarget)) {
- return;
- }
- type = 'cluster' + type;
- }
- BM.FeatureGroup.prototype.fire.call(this, type, data, propagate);
- },
- //Override BM.Evented.listens
- listens: function (type, propagate) {
- return BM.FeatureGroup.prototype.listens.call(this, type, propagate) || BM.FeatureGroup.prototype.listens.call(this, 'cluster' + type, propagate);
- },
- //Default functionality
- _defaultIconCreateFunction: function (cluster) {
- var childCount = cluster.getChildCount();
- var c = ' marker-cluster-';
- if (childCount < 10) {
- c += 'small';
- } else if (childCount < 100) {
- c += 'medium';
- } else {
- c += 'large';
- }
- return new BM.DivIcon({
- html: '<div><span>' + childCount + '</span></div>',
- className: 'marker-cluster' + c,
- iconSize: new BM.Point(40, 40)
- });
- },
- _bindEvents: function () {
- var map = this._map,
- spiderfyOnMaxZoom = this.options.spiderfyOnMaxZoom,
- showCoverageOnHover = this.options.showCoverageOnHover,
- zoomToBoundsOnClick = this.options.zoomToBoundsOnClick;
- //Zoom on cluster click or spiderfy if we are at the lowest level
- if (spiderfyOnMaxZoom || zoomToBoundsOnClick) {
- this.on('clusterclick', this._zoomOrSpiderfy, this);
- }
- //Show convex hull (boundary) polygon on mouse over
- if (showCoverageOnHover) {
- this.on('clustermouseover', this._showCoverage, this);
- this.on('clustermouseout', this._hideCoverage, this);
- map.on('zoomend', this._hideCoverage, this);
- }
- },
- _zoomOrSpiderfy: function (e) {
- var cluster = e.layer,
- bottomCluster = cluster;
- while (bottomCluster._childClusters.length === 1) {
- bottomCluster = bottomCluster._childClusters[0];
- }
- if (bottomCluster._zoom === this._maxZoom &&
- bottomCluster._childCount === cluster._childCount &&
- this.options.spiderfyOnMaxZoom) {
- // All child markers are contained in a single cluster from this._maxZoom to this cluster.
- cluster.spiderfy();
- } else if (this.options.zoomToBoundsOnClick) {
- cluster.zoomToBounds();
- }
- // Focus the map again for keyboard users.
- if (e.originalEvent && e.originalEvent.keyCode === 13) {
- this._map._container.focus();
- }
- },
- _showCoverage: function (e) {
- var map = this._map;
- if (this._inZoomAnimation) {
- return;
- }
- if (this._shownPolygon) {
- map.removeLayer(this._shownPolygon);
- }
- if (e.layer.getChildCount() > 2 && e.layer !== this._spiderfied) {
- this._shownPolygon = new BM.Polygon(e.layer.getConvexHull(), this.options.polygonOptions);
- map.addLayer(this._shownPolygon);
- }
- },
- _hideCoverage: function () {
- if (this._shownPolygon) {
- this._map.removeLayer(this._shownPolygon);
- this._shownPolygon = null;
- }
- },
- _unbindEvents: function () {
- var spiderfyOnMaxZoom = this.options.spiderfyOnMaxZoom,
- showCoverageOnHover = this.options.showCoverageOnHover,
- zoomToBoundsOnClick = this.options.zoomToBoundsOnClick,
- map = this._map;
- if (spiderfyOnMaxZoom || zoomToBoundsOnClick) {
- this.off('clusterclick', this._zoomOrSpiderfy, this);
- }
- if (showCoverageOnHover) {
- this.off('clustermouseover', this._showCoverage, this);
- this.off('clustermouseout', this._hideCoverage, this);
- map.off('zoomend', this._hideCoverage, this);
- }
- },
- _zoomEnd: function () {
- if (!this._map) { //May have been removed from the map by a zoomEnd handler
- return;
- }
- this._mergeSplitClusters();
- this._zoom = Math.round(this._map._zoom);
- this._currentShownBounds = this._getExpandedVisibleBounds();
- },
- _moveEnd: function () {
- if (this._inZoomAnimation) {
- return;
- }
- var newBounds = this._getExpandedVisibleBounds();
- this._topClusterLevel._recursivelyRemoveChildrenFromMap(this._currentShownBounds, Math.floor(this._map.getMinZoom()), this._zoom, newBounds);
- this._topClusterLevel._recursivelyAddChildrenToMap(null, Math.round(this._map._zoom), newBounds);
- this._currentShownBounds = newBounds;
- return;
- },
- _generateInitialClusters: function () {
- var maxZoom = Math.ceil(this._map.getMaxZoom()),
- minZoom = Math.floor(this._map.getMinZoom()),
- radius = this.options.maxClusterRadius,
- radiusFn = radius;
- //If we just set maxClusterRadius to a single number, we need to create
- //a simple function to return that number. Otherwise, we just have to
- //use the function we've passed in.
- if (typeof radius !== "function") {
- radiusFn = function () {
- return radius;
- };
- }
- if (this.options.disableClusteringAtZoom !== null) {
- maxZoom = this.options.disableClusteringAtZoom - 1;
- }
- this._maxZoom = maxZoom;
- this._gridClusters = {};
- this._gridUnclustered = {};
- //Set up DistanceGrids for each zoom
- for (var zoom = maxZoom; zoom >= minZoom; zoom--) {
- this._gridClusters[zoom] = new BM.DistanceGrid(radiusFn(zoom));
- this._gridUnclustered[zoom] = new BM.DistanceGrid(radiusFn(zoom));
- }
- // Instantiate the appropriate BM.MarkerCluster class (animated or not).
- this._topClusterLevel = new this._markerCluster(this, minZoom - 1);
- },
- //Zoom: Zoom to start adding at (Pass this._maxZoom to start at the bottom)
- _addLayer: function (layer, zoom) {
- var gridClusters = this._gridClusters,
- gridUnclustered = this._gridUnclustered,
- minZoom = Math.floor(this._map.getMinZoom()),
- markerPoint, z;
- if (this.options.singleMarkerMode) {
- this._overrideMarkerIcon(layer);
- }
- layer.on(this._childMarkerEventHandlers, this);
- //Find the lowest zoom level to slot this one in
- for (; zoom >= minZoom; zoom--) {
- markerPoint = this._map.project(layer.getLatLng(), zoom); // calculate pixel position
- //Try find a cluster close by
- var closest = gridClusters[zoom].getNearObject(markerPoint);
- if (closest) {
- closest._addChild(layer);
- layer.__parent = closest;
- return;
- }
- //Try find a marker close by to form a new cluster with
- closest = gridUnclustered[zoom].getNearObject(markerPoint);
- if (closest) {
- var parent = closest.__parent;
- if (parent) {
- this._removeLayer(closest, false);
- }
- //Create new cluster with these 2 in it
- var newCluster = new this._markerCluster(this, zoom, closest, layer);
- gridClusters[zoom].addObject(newCluster, this._map.project(newCluster._cLatLng, zoom));
- closest.__parent = newCluster;
- layer.__parent = newCluster;
- //First create any new intermediate parent clusters that don't exist
- var lastParent = newCluster;
- for (z = zoom - 1; z > parent._zoom; z--) {
- lastParent = new this._markerCluster(this, z, lastParent);
- gridClusters[z].addObject(lastParent, this._map.project(closest.getLatLng(), z));
- }
- parent._addChild(lastParent);
- //Remove closest from this zoom level and any above that it is in, replace with newCluster
- this._removeFromGridUnclustered(closest, zoom);
- return;
- }
- //Didn't manage to cluster in at this zoom, record us as a marker here and continue upwards
- gridUnclustered[zoom].addObject(layer, markerPoint);
- }
- //Didn't get in anything, add us to the top
- this._topClusterLevel._addChild(layer);
- layer.__parent = this._topClusterLevel;
- return;
- },
- /**
- * Refreshes the icon of all "dirty" visible clusters.
- * Non-visible "dirty" clusters will be updated when they are added to the map.
- * @private
- */
- _refreshClustersIcons: function () {
- this._featureGroup.eachLayer(function (c) {
- if (c instanceof BM.MarkerCluster && c._iconNeedsUpdate) {
- c._updateIcon();
- }
- });
- },
- //Enqueue code to fire after the marker expand/contract has happened
- _enqueue: function (fn) {
- this._queue.push(fn);
- if (!this._queueTimeout) {
- this._queueTimeout = setTimeout(BM.bind(this._processQueue, this), 300);
- }
- },
- _processQueue: function () {
- for (var i = 0; i < this._queue.length; i++) {
- this._queue[i].call(this);
- }
- this._queue.length = 0;
- clearTimeout(this._queueTimeout);
- this._queueTimeout = null;
- },
- //Merge and split any existing clusters that are too big or small
- _mergeSplitClusters: function () {
- var mapZoom = Math.round(this._map._zoom);
- //In case we are starting to split before the animation finished
- this._processQueue();
- if (this._zoom < mapZoom && this._currentShownBounds.intersects(this._getExpandedVisibleBounds())) { //Zoom in, split
- this._animationStart();
- //Remove clusters now off screen
- this._topClusterLevel._recursivelyRemoveChildrenFromMap(this._currentShownBounds, Math.floor(this._map.getMinZoom()), this._zoom, this._getExpandedVisibleBounds());
- this._animationZoomIn(this._zoom, mapZoom);
- } else if (this._zoom > mapZoom) { //Zoom out, merge
- this._animationStart();
- this._animationZoomOut(this._zoom, mapZoom);
- } else {
- this._moveEnd();
- }
- },
- //Gets the maps visible bounds expanded in each direction by the size of the screen (so the user cannot see an area we do not cover in one pan)
- _getExpandedVisibleBounds: function () {
- if (!this.options.removeOutsideVisibleBounds) {
- return this._mapBoundsInfinite;
- } else if (BM.Browser.mobile) {
- return this._checkBoundsMaxLat(this._map.getBounds());
- }
- return this._checkBoundsMaxLat(this._map.getBounds().pad(1)); // Padding expands the bounds by its own dimensions but scaled with the given factor.
- },
- /**
- * Expands the latitude to Infinity (or -Infinity) if the input bounds reach the map projection maximum defined latitude
- * (in the case of Web/Spherical Mercator, it is 85.0511287798 / see https://en.wikipedia.org/wiki/Web_Mercator#Formulas).
- * Otherwise, the removeOutsideVisibleBounds option will remove markers beyond that limit, whereas the same markers without
- * this option (or outside MCG) will have their position floored (ceiled) by the projection and rendered at that limit,
- * making the user think that MCG "eats" them and never displays them again.
- * @param bounds BM.LatLngBounds
- * @returns {BM.LatLngBounds}
- * @private
- */
- _checkBoundsMaxLat: function (bounds) {
- var maxLat = this._maxLat;
- if (maxLat !== undefined) {
- if (bounds.getNorth() >= maxLat) {
- bounds._northEast.lat = Infinity;
- }
- if (bounds.getSouth() <= -maxLat) {
- bounds._southWest.lat = -Infinity;
- }
- }
- return bounds;
- },
- //Shared animation code
- _animationAddLayerNonAnimated: function (layer, newCluster) {
- if (newCluster === layer) {
- this._featureGroup.addLayer(layer);
- } else if (newCluster._childCount === 2) {
- newCluster._addToMap();
- var markers = newCluster.getAllChildMarkers();
- this._featureGroup.removeLayer(markers[0]);
- this._featureGroup.removeLayer(markers[1]);
- } else {
- newCluster._updateIcon();
- }
- },
- /**
- * Extracts individual (i.e. non-group) layers from a Layer Group.
- * @param group to extract layers from.
- * @param output {Array} in which to store the extracted layers.
- * @returns {*|Array}
- * @private
- */
- _extractNonGroupLayers: function (group, output) {
- var layers = group.getLayers(),
- i = 0,
- layer;
- output = output || [];
- for (; i < layers.length; i++) {
- layer = layers[i];
- if (layer instanceof BM.LayerGroup) {
- this._extractNonGroupLayers(layer, output);
- continue;
- }
- output.push(layer);
- }
- return output;
- },
- /**
- * Implements the singleMarkerMode option.
- * @param layer Marker to re-style using the Clusters iconCreateFunction.
- * @returns {BM.Icon} The newly created icon.
- * @private
- */
- _overrideMarkerIcon: function (layer) {
- var icon = layer.options.icon = this.options.iconCreateFunction({
- getChildCount: function () {
- return 1;
- },
- getAllChildMarkers: function () {
- return [layer];
- }
- });
- return icon;
- }
- });
- // Constant bounds used in case option "removeOutsideVisibleBounds" is set to false.
- BM.MarkerClusterGroup.include({
- _mapBoundsInfinite: new BM.LatLngBounds(new BM.LatLng(-Infinity, -Infinity), new BM.LatLng(Infinity, Infinity))
- });
- BM.MarkerClusterGroup.include({
- _noAnimation: {
- //Non Animated versions of everything
- _animationStart: function () {
- //Do nothing...
- },
- _animationZoomIn: function (previousZoomLevel, newZoomLevel) {
- this._topClusterLevel._recursivelyRemoveChildrenFromMap(this._currentShownBounds, Math.floor(this._map.getMinZoom()), previousZoomLevel);
- this._topClusterLevel._recursivelyAddChildrenToMap(null, newZoomLevel, this._getExpandedVisibleBounds());
- //We didn't actually animate, but we use this event to mean "clustering animations have finished"
- this.fire('animationend');
- },
- _animationZoomOut: function (previousZoomLevel, newZoomLevel) {
- this._topClusterLevel._recursivelyRemoveChildrenFromMap(this._currentShownBounds, Math.floor(this._map.getMinZoom()), previousZoomLevel);
- this._topClusterLevel._recursivelyAddChildrenToMap(null, newZoomLevel, this._getExpandedVisibleBounds());
- //We didn't actually animate, but we use this event to mean "clustering animations have finished"
- this.fire('animationend');
- },
- _animationAddLayer: function (layer, newCluster) {
- this._animationAddLayerNonAnimated(layer, newCluster);
- }
- },
- _withAnimation: {
- //Animated versions here
- _animationStart: function () {
- this._map._mapPane.className += ' bigemap-cluster-anim';
- this._inZoomAnimation++;
- },
- _animationZoomIn: function (previousZoomLevel, newZoomLevel) {
- var bounds = this._getExpandedVisibleBounds(),
- fg = this._featureGroup,
- minZoom = Math.floor(this._map.getMinZoom()),
- i;
- this._ignoreMove = true;
- //Add all children of current clusters to map and remove those clusters from map
- this._topClusterLevel._recursively(bounds, previousZoomLevel, minZoom, function (c) {
- var startPos = c._latlng,
- markers = c._markers,
- m;
- if (!bounds.contains(startPos)) {
- startPos = null;
- }
- if (c._isSingleParent() && previousZoomLevel + 1 === newZoomLevel) { //Immediately add the new child and remove us
- fg.removeLayer(c);
- c._recursivelyAddChildrenToMap(null, newZoomLevel, bounds);
- } else {
- //Fade out old cluster
- c.clusterHide();
- c._recursivelyAddChildrenToMap(startPos, newZoomLevel, bounds);
- }
- //Remove all markers that aren't visible any more
- //TODO: Do we actually need to do this on the higher levels too?
- for (i = markers.length - 1; i >= 0; i--) {
- m = markers[i];
- if (!bounds.contains(m._latlng)) {
- fg.removeLayer(m);
- }
- }
- });
- this._forceLayout();
- //Update opacities
- this._topClusterLevel._recursivelyBecomeVisible(bounds, newZoomLevel);
- //TODO Maybe? Update markers in _recursivelyBecomeVisible
- fg.eachLayer(function (n) {
- if (!(n instanceof BM.MarkerCluster) && n._icon) {
- n.clusterShow();
- }
- });
- //update the positions of the just added clusters/markers
- this._topClusterLevel._recursively(bounds, previousZoomLevel, newZoomLevel, function (c) {
- c._recursivelyRestoreChildPositions(newZoomLevel);
- });
- this._ignoreMove = false;
- //Remove the old clusters and close the zoom animation
- this._enqueue(function () {
- //update the positions of the just added clusters/markers
- this._topClusterLevel._recursively(bounds, previousZoomLevel, minZoom, function (c) {
- fg.removeLayer(c);
- c.clusterShow();
- });
- this._animationEnd();
- });
- },
- _animationZoomOut: function (previousZoomLevel, newZoomLevel) {
- this._animationZoomOutSingle(this._topClusterLevel, previousZoomLevel - 1, newZoomLevel);
- //Need to add markers for those that weren't on the map before but are now
- this._topClusterLevel._recursivelyAddChildrenToMap(null, newZoomLevel, this._getExpandedVisibleBounds());
- //Remove markers that were on the map before but won't be now
- this._topClusterLevel._recursivelyRemoveChildrenFromMap(this._currentShownBounds, Math.floor(this._map.getMinZoom()), previousZoomLevel, this._getExpandedVisibleBounds());
- },
- _animationAddLayer: function (layer, newCluster) {
- var me = this,
- fg = this._featureGroup;
- fg.addLayer(layer);
- if (newCluster !== layer) {
- if (newCluster._childCount > 2) { //Was already a cluster
- newCluster._updateIcon();
- this._forceLayout();
- this._animationStart();
- layer._setPos(this._map.latLngToLayerPoint(newCluster.getLatLng()));
- layer.clusterHide();
- this._enqueue(function () {
- fg.removeLayer(layer);
- layer.clusterShow();
- me._animationEnd();
- });
- } else { //Just became a cluster
- this._forceLayout();
- me._animationStart();
- me._animationZoomOutSingle(newCluster, this._map.getMaxZoom(), this._zoom);
- }
- }
- }
- },
- // Private methods for animated versions.
- _animationZoomOutSingle: function (cluster, previousZoomLevel, newZoomLevel) {
- var bounds = this._getExpandedVisibleBounds(),
- minZoom = Math.floor(this._map.getMinZoom());
- //Animate all of the markers in the clusters to move to their cluster center point
- cluster._recursivelyAnimateChildrenInAndAddSelfToMap(bounds, minZoom, previousZoomLevel + 1, newZoomLevel);
- var me = this;
- //Update the opacity (If we immediately set it they won't animate)
- this._forceLayout();
- cluster._recursivelyBecomeVisible(bounds, newZoomLevel);
- //TODO: Maybe use the transition timing stuff to make this more reliable
- //When the animations are done, tidy up
- this._enqueue(function () {
- //This cluster stopped being a cluster before the timeout fired
- if (cluster._childCount === 1) {
- var m = cluster._markers[0];
- //If we were in a cluster animation at the time then the opacity and position of our child could be wrong now, so fix it
- this._ignoreMove = true;
- m.setLatLng(m.getLatLng());
- this._ignoreMove = false;
- if (m.clusterShow) {
- m.clusterShow();
- }
- } else {
- cluster._recursively(bounds, newZoomLevel, minZoom, function (c) {
- c._recursivelyRemoveChildrenFromMap(bounds, minZoom, previousZoomLevel + 1);
- });
- }
- me._animationEnd();
- });
- },
- _animationEnd: function () {
- if (this._map) {
- this._map._mapPane.className = this._map._mapPane.className.replace(' bigemap-cluster-anim', '');
- }
- this._inZoomAnimation--;
- this.fire('animationend');
- },
- //Force a browser layout of stuff in the map
- // Should apply the current opacity and location to all elements so we can update them again for an animation
- _forceLayout: function () {
- //In my testing this works, infact offsetWidth of any element seems to work.
- //Could loop all this._layers and do this for each _icon if it stops working
- BM.Util.falseFn(document.body.offsetWidth);
- }
- });
- BM.markerClusterGroup = function (options) {
- return new BM.MarkerClusterGroup(options);
- };
- var MarkerCluster = BM.MarkerCluster = BM.Marker.extend({
- options: BM.Icon.prototype.options,
- initialize: function (group, zoom, a, b) {
- BM.Marker.prototype.initialize.call(this, a ? (a._cLatLng || a.getLatLng()) : new BM.LatLng(0, 0), {
- icon: this,
- pane: group.options.clusterPane
- });
- this._group = group;
- this._zoom = zoom;
- this._markers = [];
- this._childClusters = [];
- this._childCount = 0;
- this._iconNeedsUpdate = true;
- this._boundsNeedUpdate = true;
- this._bounds = new BM.LatLngBounds();
- if (a) {
- this._addChild(a);
- }
- if (b) {
- this._addChild(b);
- }
- },
- //Recursively retrieve all child markers of this cluster
- getAllChildMarkers: function (storageArray, ignoreDraggedMarker) {
- storageArray = storageArray || [];
- for (var i = this._childClusters.length - 1; i >= 0; i--) {
- this._childClusters[i].getAllChildMarkers(storageArray);
- }
- for (var j = this._markers.length - 1; j >= 0; j--) {
- if (ignoreDraggedMarker && this._markers[j].__dragStart) {
- continue;
- }
- storageArray.push(this._markers[j]);
- }
- return storageArray;
- },
- //Returns the count of how many child markers we have
- getChildCount: function () {
- return this._childCount;
- },
- //Zoom to the minimum of showing all of the child markers, or the extents of this cluster
- zoomToBounds: function (fitBoundsOptions) {
- var childClusters = this._childClusters.slice(),
- map = this._group._map,
- boundsZoom = map.getBoundsZoom(this._bounds),
- zoom = this._zoom + 1,
- mapZoom = map.getZoom(),
- i;
- //calculate how far we need to zoom down to see all of the markers
- while (childClusters.length > 0 && boundsZoom > zoom) {
- zoom++;
- var newClusters = [];
- for (i = 0; i < childClusters.length; i++) {
- newClusters = newClusters.concat(childClusters[i]._childClusters);
- }
- childClusters = newClusters;
- }
- if (boundsZoom > zoom) {
- this._group._map.setView(this._latlng, zoom);
- } else if (boundsZoom <= mapZoom) { //If fitBounds wouldn't zoom us down, zoom us down instead
- this._group._map.setView(this._latlng, mapZoom + 1);
- } else {
- this._group._map.fitBounds(this._bounds, fitBoundsOptions);
- }
- },
- getBounds: function () {
- var bounds = new BM.LatLngBounds();
- bounds.extend(this._bounds);
- return bounds;
- },
- _updateIcon: function () {
- this._iconNeedsUpdate = true;
- if (this._icon) {
- this.setIcon(this);
- }
- },
- //Cludge for Icon, we pretend to be an icon for performance
- createIcon: function () {
- if (this._iconNeedsUpdate) {
- this._iconObj = this._group.options.iconCreateFunction(this);
- this._iconNeedsUpdate = false;
- }
- return this._iconObj.createIcon();
- },
- createShadow: function () {
- return this._iconObj.createShadow();
- },
- _addChild: function (new1, isNotificationFromChild) {
- this._iconNeedsUpdate = true;
- this._boundsNeedUpdate = true;
- this._setClusterCenter(new1);
- if (new1 instanceof BM.MarkerCluster) {
- if (!isNotificationFromChild) {
- this._childClusters.push(new1);
- new1.__parent = this;
- }
- this._childCount += new1._childCount;
- } else {
- if (!isNotificationFromChild) {
- this._markers.push(new1);
- }
- this._childCount++;
- }
- if (this.__parent) {
- this.__parent._addChild(new1, true);
- }
- },
- /**
- * Makes sure the cluster center is set. If not, uses the child center if it is a cluster, or the marker position.
- * @param child BM.MarkerCluster|BM.Marker that will be used as cluster center if not defined yet.
- * @private
- */
- _setClusterCenter: function (child) {
- if (!this._cLatLng) {
- // when clustering, take position of the first point as the cluster center
- this._cLatLng = child._cLatLng || child._latlng;
- }
- },
- /**
- * Assigns impossible bounding values so that the next extend entirely determines the new bounds.
- * This method avoids having to trash the previous BM.LatLngBounds object and to create a new one, which is much slower for this class.
- * As long as the bounds are not extended, most other methods would probably fail, as they would with bounds initialized but not extended.
- * @private
- */
- _resetBounds: function () {
- var bounds = this._bounds;
- if (bounds._southWest) {
- bounds._southWest.lat = Infinity;
- bounds._southWest.lng = Infinity;
- }
- if (bounds._northEast) {
- bounds._northEast.lat = -Infinity;
- bounds._northEast.lng = -Infinity;
- }
- },
- _recalculateBounds: function () {
- var markers = this._markers,
- childClusters = this._childClusters,
- latSum = 0,
- lngSum = 0,
- totalCount = this._childCount,
- i, child, childLatLng, childCount;
- // Case where all markers are removed from the map and we are left with just an empty _topClusterLevel.
- if (totalCount === 0) {
- return;
- }
- // Reset rather than creating a new object, for performance.
- this._resetBounds();
- // Child markers.
- for (i = 0; i < markers.length; i++) {
- childLatLng = markers[i]._latlng;
- this._bounds.extend(childLatLng);
- latSum += childLatLng.lat;
- lngSum += childLatLng.lng;
- }
- // Child clusters.
- for (i = 0; i < childClusters.length; i++) {
- child = childClusters[i];
- // Re-compute child bounds and weighted position first if necessary.
- if (child._boundsNeedUpdate) {
- child._recalculateBounds();
- }
- this._bounds.extend(child._bounds);
- childLatLng = child._wLatLng;
- childCount = child._childCount;
- latSum += childLatLng.lat * childCount;
- lngSum += childLatLng.lng * childCount;
- }
- this._latlng = this._wLatLng = new BM.LatLng(latSum / totalCount, lngSum / totalCount);
- // Reset dirty flag.
- this._boundsNeedUpdate = false;
- },
- //Set our markers position as given and add it to the map
- _addToMap: function (startPos) {
- if (startPos) {
- this._backupLatlng = this._latlng;
- this.setLatLng(startPos);
- }
- this._group._featureGroup.addLayer(this);
- },
- _recursivelyAnimateChildrenIn: function (bounds, center, maxZoom) {
- this._recursively(bounds, this._group._map.getMinZoom(), maxZoom - 1,
- function (c) {
- var markers = c._markers,
- i, m;
- for (i = markers.length - 1; i >= 0; i--) {
- m = markers[i];
- //Only do it if the icon is still on the map
- if (m._icon) {
- m._setPos(center);
- m.clusterHide();
- }
- }
- },
- function (c) {
- var childClusters = c._childClusters,
- j, cm;
- for (j = childClusters.length - 1; j >= 0; j--) {
- cm = childClusters[j];
- if (cm._icon) {
- cm._setPos(center);
- cm.clusterHide();
- }
- }
- }
- );
- },
- _recursivelyAnimateChildrenInAndAddSelfToMap: function (bounds, mapMinZoom, previousZoomLevel, newZoomLevel) {
- this._recursively(bounds, newZoomLevel, mapMinZoom,
- function (c) {
- c._recursivelyAnimateChildrenIn(bounds, c._group._map.latLngToLayerPoint(c.getLatLng()).round(), previousZoomLevel);
- //TODO: depthToAnimateIn affects _isSingleParent, if there is a multizoom we may/may not be.
- //As a hack we only do a animation free zoom on a single level zoom, if someone does multiple levels then we always animate
- if (c._isSingleParent() && previousZoomLevel - 1 === newZoomLevel) {
- c.clusterShow();
- c._recursivelyRemoveChildrenFromMap(bounds, mapMinZoom, previousZoomLevel); //Immediately remove our children as we are replacing them. TODO previousBounds not bounds
- } else {
- c.clusterHide();
- }
- c._addToMap();
- }
- );
- },
- _recursivelyBecomeVisible: function (bounds, zoomLevel) {
- this._recursively(bounds, this._group._map.getMinZoom(), zoomLevel, null, function (c) {
- c.clusterShow();
- });
- },
- _recursivelyAddChildrenToMap: function (startPos, zoomLevel, bounds) {
- this._recursively(bounds, this._group._map.getMinZoom() - 1, zoomLevel,
- function (c) {
- if (zoomLevel === c._zoom) {
- return;
- }
- //Add our child markers at startPos (so they can be animated out)
- for (var i = c._markers.length - 1; i >= 0; i--) {
- var nm = c._markers[i];
- if (!bounds.contains(nm._latlng)) {
- continue;
- }
- if (startPos) {
- nm._backupLatlng = nm.getLatLng();
- nm.setLatLng(startPos);
- if (nm.clusterHide) {
- nm.clusterHide();
- }
- }
- c._group._featureGroup.addLayer(nm);
- }
- },
- function (c) {
- c._addToMap(startPos);
- }
- );
- },
- _recursivelyRestoreChildPositions: function (zoomLevel) {
- //Fix positions of child markers
- for (var i = this._markers.length - 1; i >= 0; i--) {
- var nm = this._markers[i];
- if (nm._backupLatlng) {
- nm.setLatLng(nm._backupLatlng);
- delete nm._backupLatlng;
- }
- }
- if (zoomLevel - 1 === this._zoom) {
- //Reposition child clusters
- for (var j = this._childClusters.length - 1; j >= 0; j--) {
- this._childClusters[j]._restorePosition();
- }
- } else {
- for (var k = this._childClusters.length - 1; k >= 0; k--) {
- this._childClusters[k]._recursivelyRestoreChildPositions(zoomLevel);
- }
- }
- },
- _restorePosition: function () {
- if (this._backupLatlng) {
- this.setLatLng(this._backupLatlng);
- delete this._backupLatlng;
- }
- },
- //exceptBounds: If set, don't remove any markers/clusters in it
- _recursivelyRemoveChildrenFromMap: function (previousBounds, mapMinZoom, zoomLevel, exceptBounds) {
- var m, i;
- this._recursively(previousBounds, mapMinZoom - 1, zoomLevel - 1,
- function (c) {
- //Remove markers at every level
- for (i = c._markers.length - 1; i >= 0; i--) {
- m = c._markers[i];
- if (!exceptBounds || !exceptBounds.contains(m._latlng)) {
- c._group._featureGroup.removeLayer(m);
- if (m.clusterShow) {
- m.clusterShow();
- }
- }
- }
- },
- function (c) {
- //Remove child clusters at just the bottom level
- for (i = c._childClusters.length - 1; i >= 0; i--) {
- m = c._childClusters[i];
- if (!exceptBounds || !exceptBounds.contains(m._latlng)) {
- c._group._featureGroup.removeLayer(m);
- if (m.clusterShow) {
- m.clusterShow();
- }
- }
- }
- }
- );
- },
- //Run the given functions recursively to this and child clusters
- // boundsToApplyTo: a BM.LatLngBounds representing the bounds of what clusters to recurse in to
- // zoomLevelToStart: zoom level to start running functions (inclusive)
- // zoomLevelToStop: zoom level to stop running functions (inclusive)
- // runAtEveryLevel: function that takes an BM.MarkerCluster as an argument that should be applied on every level
- // runAtBottomLevel: function that takes an BM.MarkerCluster as an argument that should be applied at only the bottom level
- _recursively: function (boundsToApplyTo, zoomLevelToStart, zoomLevelToStop, runAtEveryLevel, runAtBottomLevel) {
- var childClusters = this._childClusters,
- zoom = this._zoom,
- i, c;
- if (zoomLevelToStart <= zoom) {
- if (runAtEveryLevel) {
- runAtEveryLevel(this);
- }
- if (runAtBottomLevel && zoom === zoomLevelToStop) {
- runAtBottomLevel(this);
- }
- }
- if (zoom < zoomLevelToStart || zoom < zoomLevelToStop) {
- for (i = childClusters.length - 1; i >= 0; i--) {
- c = childClusters[i];
- if (c._boundsNeedUpdate) {
- c._recalculateBounds();
- }
- if (boundsToApplyTo.intersects(c._bounds)) {
- c._recursively(boundsToApplyTo, zoomLevelToStart, zoomLevelToStop, runAtEveryLevel, runAtBottomLevel);
- }
- }
- }
- },
- //Returns true if we are the parent of only one cluster and that cluster is the same as us
- _isSingleParent: function () {
- //Don't need to check this._markers as the rest won't work if there are any
- return this._childClusters.length > 0 && this._childClusters[0]._childCount === this._childCount;
- }
- });
- /*
- * Extends BM.Marker to include two extra methods: clusterHide and clusterShow.
- *
- * They work as setOpacity(0) and setOpacity(1) respectively, but
- * don't overwrite the options.opacity
- *
- */
- BM.Marker.include({
- clusterHide: function () {
- var backup = this.options.opacity;
- this.setOpacity(0);
- this.options.opacity = backup;
- return this;
- },
- clusterShow: function () {
- return this.setOpacity(this.options.opacity);
- }
- });
- BM.DistanceGrid = function (cellSize) {
- this._cellSize = cellSize;
- this._sqCellSize = cellSize * cellSize;
- this._grid = {};
- this._objectPoint = {};
- };
- BM.DistanceGrid.prototype = {
- addObject: function (obj, point) {
- var x = this._getCoord(point.x),
- y = this._getCoord(point.y),
- grid = this._grid,
- row = grid[y] = grid[y] || {},
- cell = row[x] = row[x] || [],
- stamp = BM.Util.stamp(obj);
- this._objectPoint[stamp] = point;
- cell.push(obj);
- },
- updateObject: function (obj, point) {
- this.removeObject(obj);
- this.addObject(obj, point);
- },
- //Returns true if the object was found
- removeObject: function (obj, point) {
- var x = this._getCoord(point.x),
- y = this._getCoord(point.y),
- grid = this._grid,
- row = grid[y] = grid[y] || {},
- cell = row[x] = row[x] || [],
- i, len;
- delete this._objectPoint[BM.Util.stamp(obj)];
- for (i = 0, len = cell.length; i < len; i++) {
- if (cell[i] === obj) {
- cell.splice(i, 1);
- if (len === 1) {
- delete row[x];
- }
- return true;
- }
- }
- },
- eachObject: function (fn, context) {
- var i, j, k, len, row, cell, removed,
- grid = this._grid;
- for (i in grid) {
- row = grid[i];
- for (j in row) {
- cell = row[j];
- for (k = 0, len = cell.length; k < len; k++) {
- removed = fn.call(context, cell[k]);
- if (removed) {
- k--;
- len--;
- }
- }
- }
- }
- },
- getNearObject: function (point) {
- var x = this._getCoord(point.x),
- y = this._getCoord(point.y),
- i, j, k, row, cell, len, obj, dist,
- objectPoint = this._objectPoint,
- closestDistSq = this._sqCellSize,
- closest = null;
- for (i = y - 1; i <= y + 1; i++) {
- row = this._grid[i];
- if (row) {
- for (j = x - 1; j <= x + 1; j++) {
- cell = row[j];
- if (cell) {
- for (k = 0, len = cell.length; k < len; k++) {
- obj = cell[k];
- dist = this._sqDist(objectPoint[BM.Util.stamp(obj)], point);
- if (dist < closestDistSq ||
- dist <= closestDistSq && closest === null) {
- closestDistSq = dist;
- closest = obj;
- }
- }
- }
- }
- }
- }
- return closest;
- },
- _getCoord: function (x) {
- var coord = Math.floor(x / this._cellSize);
- return isFinite(coord) ? coord : x;
- },
- _sqDist: function (p, p2) {
- var dx = p2.x - p.x,
- dy = p2.y - p.y;
- return dx * dx + dy * dy;
- }
- };
- /* Copyright (c) 2012 the authors listed at the following URL, and/or
- the authors of referenced articles or incorporated external code:
- http://en.literateprograms.org/Quickhull_(Javascript)?action=history&offset=20120410175256
- Permission is hereby granted, free of charge, to any person obtaining
- a copy of this software and associated documentation files (the
- "Software"), to deal in the Software without restriction, including
- without limitation the rights to use, copy, modify, merge, publish,
- distribute, sublicense, and/or sell copies of the Software, and to
- permit persons to whom the Software is furnished to do so, subject to
- the following conditions:
- The above copyright notice and this permission notice shall be
- included in all copies or substantial portions of the Software.
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
- IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
- CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
- TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
- SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- Retrieved from: http://en.literateprograms.org/Quickhull_(Javascript)?oldid=18434
- */
- (function () {
- BM.QuickHull = {
- /*
- * @param {Object} cpt a point to be measured from the baseline
- * @param {Array} bl the baseline, as represented by a two-element
- * array of latlng objects.
- * @returns {Number} an approximate distance measure
- */
- getDistant: function (cpt, bl) {
- var vY = bl[1].lat - bl[0].lat,
- vX = bl[0].lng - bl[1].lng;
- return (vX * (cpt.lat - bl[0].lat) + vY * (cpt.lng - bl[0].lng));
- },
- /*
- * @param {Array} baseLine a two-element array of latlng objects
- * representing the baseline to project from
- * @param {Array} latLngs an array of latlng objects
- * @returns {Object} the maximum point and all new points to stay
- * in consideration for the hull.
- */
- findMostDistantPointFromBaseLine: function (baseLine, latLngs) {
- var maxD = 0,
- maxPt = null,
- newPoints = [],
- i, pt, d;
- for (i = latLngs.length - 1; i >= 0; i--) {
- pt = latLngs[i];
- d = this.getDistant(pt, baseLine);
- if (d > 0) {
- newPoints.push(pt);
- } else {
- continue;
- }
- if (d > maxD) {
- maxD = d;
- maxPt = pt;
- }
- }
- return {
- maxPoint: maxPt,
- newPoints: newPoints
- };
- },
- /*
- * Given a baseline, compute the convex hull of latLngs as an array
- * of latLngs.
- *
- * @param {Array} latLngs
- * @returns {Array}
- */
- buildConvexHull: function (baseLine, latLngs) {
- var convexHullBaseLines = [],
- t = this.findMostDistantPointFromBaseLine(baseLine, latLngs);
- if (t.maxPoint) { // if there is still a point "outside" the base line
- convexHullBaseLines =
- convexHullBaseLines.concat(
- this.buildConvexHull([baseLine[0], t.maxPoint], t.newPoints)
- );
- convexHullBaseLines =
- convexHullBaseLines.concat(
- this.buildConvexHull([t.maxPoint, baseLine[1]], t.newPoints)
- );
- return convexHullBaseLines;
- } else { // if there is no more point "outside" the base line, the current base line is part of the convex hull
- return [baseLine[0]];
- }
- },
- /*
- * Given an array of latlngs, compute a convex hull as an array
- * of latlngs
- *
- * @param {Array} latLngs
- * @returns {Array}
- */
- getConvexHull: function (latLngs) {
- // find first baseline
- var maxLat = false,
- minLat = false,
- maxLng = false,
- minLng = false,
- maxLatPt = null,
- minLatPt = null,
- maxLngPt = null,
- minLngPt = null,
- maxPt = null,
- minPt = null,
- i;
- for (i = latLngs.length - 1; i >= 0; i--) {
- var pt = latLngs[i];
- if (maxLat === false || pt.lat > maxLat) {
- maxLatPt = pt;
- maxLat = pt.lat;
- }
- if (minLat === false || pt.lat < minLat) {
- minLatPt = pt;
- minLat = pt.lat;
- }
- if (maxLng === false || pt.lng > maxLng) {
- maxLngPt = pt;
- maxLng = pt.lng;
- }
- if (minLng === false || pt.lng < minLng) {
- minLngPt = pt;
- minLng = pt.lng;
- }
- }
- if (minLat !== maxLat) {
- minPt = minLatPt;
- maxPt = maxLatPt;
- } else {
- minPt = minLngPt;
- maxPt = maxLngPt;
- }
- var ch = [].concat(this.buildConvexHull([minPt, maxPt], latLngs),
- this.buildConvexHull([maxPt, minPt], latLngs));
- return ch;
- }
- };
- }());
- BM.MarkerCluster.include({
- getConvexHull: function () {
- var childMarkers = this.getAllChildMarkers(),
- points = [],
- p, i;
- for (i = childMarkers.length - 1; i >= 0; i--) {
- p = childMarkers[i].getLatLng();
- points.push(p);
- }
- return BM.QuickHull.getConvexHull(points);
- }
- });
- //This code is 100% based on https://github.com/jawj/OverlappingMarkerSpiderfier-bigemap
- //Huge thanks to jawj for implementing it first to make my job easy :-)
- BM.MarkerCluster.include({
- _2PI: Math.PI * 2,
- _circleFootSeparation: 25, //related to circumference of circle
- _circleStartAngle: 0,
- _spiralFootSeparation: 28, //related to size of spiral (experiment!)
- _spiralLengthStart: 11,
- _spiralLengthFactor: 5,
- _circleSpiralSwitchover: 9, //show spiral instead of circle from this marker count upwards.
- // 0 -> always spiral; Infinity -> always circle
- spiderfy: function () {
- if (this._group._spiderfied === this || this._group._inZoomAnimation) {
- return;
- }
- var childMarkers = this.getAllChildMarkers(null, true),
- group = this._group,
- map = group._map,
- center = map.latLngToLayerPoint(this._latlng),
- positions;
- this._group._unspiderfy();
- this._group._spiderfied = this;
- //TODO Maybe: childMarkers order by distance to center
- if (childMarkers.length >= this._circleSpiralSwitchover) {
- positions = this._generatePointsSpiral(childMarkers.length, center);
- } else {
- center.y += 10; // Otherwise circles look wrong => hack for standard blue icon, renders differently for other icons.
- positions = this._generatePointsCircle(childMarkers.length, center);
- }
- this._animationSpiderfy(childMarkers, positions);
- },
- unspiderfy: function (zoomDetails) {
- /// <param Name="zoomDetails">Argument from zoomanim if being called in a zoom animation or null otherwise</param>
- if (this._group._inZoomAnimation) {
- return;
- }
- this._animationUnspiderfy(zoomDetails);
- this._group._spiderfied = null;
- },
- _generatePointsCircle: function (count, centerPt) {
- var circumference = this._group.options.spiderfyDistanceMultiplier * this._circleFootSeparation * (2 + count),
- legLength = circumference / this._2PI, //radius from circumference
- angleStep = this._2PI / count,
- res = [],
- i, angle;
- legLength = Math.max(legLength, 35); // Minimum distance to get outside the cluster icon.
- res.length = count;
- for (i = 0; i < count; i++) { // Clockwise, like spiral.
- angle = this._circleStartAngle + i * angleStep;
- res[i] = new BM.Point(centerPt.x + legLength * Math.cos(angle), centerPt.y + legLength * Math.sin(angle))._round();
- }
- return res;
- },
- _generatePointsSpiral: function (count, centerPt) {
- var spiderfyDistanceMultiplier = this._group.options.spiderfyDistanceMultiplier,
- legLength = spiderfyDistanceMultiplier * this._spiralLengthStart,
- separation = spiderfyDistanceMultiplier * this._spiralFootSeparation,
- lengthFactor = spiderfyDistanceMultiplier * this._spiralLengthFactor * this._2PI,
- angle = 0,
- res = [],
- i;
- res.length = count;
- // Higher index, closer position to cluster center.
- for (i = count; i >= 0; i--) {
- // Skip the first position, so that we are already farther from center and we avoid
- // being under the default cluster icon (especially important for Circle Markers).
- if (i < count) {
- res[i] = new BM.Point(centerPt.x + legLength * Math.cos(angle), centerPt.y + legLength * Math.sin(angle))._round();
- }
- angle += separation / legLength + i * 0.0005;
- legLength += lengthFactor / angle;
- }
- return res;
- },
- _noanimationUnspiderfy: function () {
- var group = this._group,
- map = group._map,
- fg = group._featureGroup,
- childMarkers = this.getAllChildMarkers(null, true),
- m, i;
- group._ignoreMove = true;
- this.setOpacity(1);
- for (i = childMarkers.length - 1; i >= 0; i--) {
- m = childMarkers[i];
- fg.removeLayer(m);
- if (m._preSpiderfyLatlng) {
- m.setLatLng(m._preSpiderfyLatlng);
- delete m._preSpiderfyLatlng;
- }
- if (m.setZIndexOffset) {
- m.setZIndexOffset(0);
- }
- if (m._spiderLeg) {
- map.removeLayer(m._spiderLeg);
- delete m._spiderLeg;
- }
- }
- group.fire('unspiderfied', {
- cluster: this,
- markers: childMarkers
- });
- group._ignoreMove = false;
- group._spiderfied = null;
- }
- });
- //Non Animated versions of everything
- BM.MarkerClusterNonAnimated = BM.MarkerCluster.extend({
- _animationSpiderfy: function (childMarkers, positions) {
- var group = this._group,
- map = group._map,
- fg = group._featureGroup,
- legOptions = this._group.options.spiderLegPolylineOptions,
- i, m, leg, newPos;
- group._ignoreMove = true;
- // Traverse in ascending order to make sure that inner circleMarkers are on top of further legs. Normal markers are re-ordered by newPosition.
- // The reverse order trick no longer improves performance on modern browsers.
- for (i = 0; i < childMarkers.length; i++) {
- newPos = map.layerPointToLatLng(positions[i]);
- m = childMarkers[i];
- // Add the leg before the marker, so that in case the latter is a circleMarker, the leg is behind it.
- leg = new BM.Polyline([this._latlng, newPos], legOptions);
- map.addLayer(leg);
- m._spiderLeg = leg;
- // Now add the marker.
- m._preSpiderfyLatlng = m._latlng;
- m.setLatLng(newPos);
- if (m.setZIndexOffset) {
- m.setZIndexOffset(1000000); //Make these appear on top of EVERYTHING
- }
- fg.addLayer(m);
- }
- this.setOpacity(0.3);
- group._ignoreMove = false;
- group.fire('spiderfied', {
- cluster: this,
- markers: childMarkers
- });
- },
- _animationUnspiderfy: function () {
- this._noanimationUnspiderfy();
- }
- });
- //Animated versions here
- BM.MarkerCluster.include({
- _animationSpiderfy: function (childMarkers, positions) {
- var me = this,
- group = this._group,
- map = group._map,
- fg = group._featureGroup,
- thisLayerLatLng = this._latlng,
- thisLayerPos = map.latLngToLayerPoint(thisLayerLatLng),
- svg = BM.Path.SVG,
- legOptions = BM.extend({}, this._group.options.spiderLegPolylineOptions), // Copy the options so that we can modify them for animation.
- finalLegOpacity = legOptions.opacity,
- i, m, leg, legPath, legLength, newPos;
- if (finalLegOpacity === undefined) {
- finalLegOpacity = BM.MarkerClusterGroup.prototype.options.spiderLegPolylineOptions.opacity;
- }
- if (svg) {
- // If the initial opacity of the spider leg is not 0 then it appears before the animation starts.
- legOptions.opacity = 0;
- // Add the class for CSS transitions.
- legOptions.className = (legOptions.className || '') + ' bigemap-cluster-spider-leg';
- } else {
- // Make sure we have a defined opacity.
- legOptions.opacity = finalLegOpacity;
- }
- group._ignoreMove = true;
- // Add markers and spider legs to map, hidden at our center point.
- // Traverse in ascending order to make sure that inner circleMarkers are on top of further legs. Normal markers are re-ordered by newPosition.
- // The reverse order trick no longer improves performance on modern browsers.
- for (i = 0; i < childMarkers.length; i++) {
- m = childMarkers[i];
- newPos = map.layerPointToLatLng(positions[i]);
- // Add the leg before the marker, so that in case the latter is a circleMarker, the leg is behind it.
- leg = new BM.Polyline([thisLayerLatLng, newPos], legOptions);
- map.addLayer(leg);
- m._spiderLeg = leg;
- // Explanations: https://jakearchibald.com/2013/animated-line-drawing-svg/
- // In our case the transition property is declared in the CSS file.
- if (svg) {
- legPath = leg._path;
- legLength = legPath.getTotalLength() + 0.1; // Need a small extra length to avoid remaining dot in Firefox.
- legPath.style.strokeDasharray = legLength; // Just 1 length is enough, it will be duplicated.
- legPath.style.strokeDashoffset = legLength;
- }
- // If it is a marker, add it now and we'll animate it out
- if (m.setZIndexOffset) {
- m.setZIndexOffset(1000000); // Make normal markers appear on top of EVERYTHING
- }
- if (m.clusterHide) {
- m.clusterHide();
- }
- // Vectors just get immediately added
- fg.addLayer(m);
- if (m._setPos) {
- m._setPos(thisLayerPos);
- }
- }
- group._forceLayout();
- group._animationStart();
- // Reveal markers and spider legs.
- for (i = childMarkers.length - 1; i >= 0; i--) {
- newPos = map.layerPointToLatLng(positions[i]);
- m = childMarkers[i];
- //Move marker to new position
- m._preSpiderfyLatlng = m._latlng;
- m.setLatLng(newPos);
- if (m.clusterShow) {
- m.clusterShow();
- }
- // Animate leg (animation is actually delegated to CSS transition).
- if (svg) {
- leg = m._spiderLeg;
- legPath = leg._path;
- legPath.style.strokeDashoffset = 0;
- //legPath.style.strokeOpacity = finalLegOpacity;
- leg.setStyle({
- opacity: finalLegOpacity
- });
- }
- }
- this.setOpacity(0.3);
- group._ignoreMove = false;
- setTimeout(function () {
- group._animationEnd();
- group.fire('spiderfied', {
- cluster: me,
- markers: childMarkers
- });
- }, 200);
- },
- _animationUnspiderfy: function (zoomDetails) {
- var me = this,
- group = this._group,
- map = group._map,
- fg = group._featureGroup,
- thisLayerPos = zoomDetails ? map._latLngToNewLayerPoint(this._latlng, zoomDetails.zoom, zoomDetails.center) : map.latLngToLayerPoint(this._latlng),
- childMarkers = this.getAllChildMarkers(null, true),
- svg = BM.Path.SVG,
- m, i, leg, legPath, legLength, nonAnimatable;
- group._ignoreMove = true;
- group._animationStart();
- //Make us visible and bring the child markers back in
- this.setOpacity(1);
- for (i = childMarkers.length - 1; i >= 0; i--) {
- m = childMarkers[i];
- //Marker was added to us after we were spiderfied
- if (!m._preSpiderfyLatlng) {
- continue;
- }
- //Close any popup on the marker first, otherwise setting the location of the marker will make the map scroll
- m.closePopup();
- //Fix up the location to the real one
- m.setLatLng(m._preSpiderfyLatlng);
- delete m._preSpiderfyLatlng;
- //Hack override the location to be our center
- nonAnimatable = true;
- if (m._setPos) {
- m._setPos(thisLayerPos);
- nonAnimatable = false;
- }
- if (m.clusterHide) {
- m.clusterHide();
- nonAnimatable = false;
- }
- if (nonAnimatable) {
- fg.removeLayer(m);
- }
- // Animate the spider leg back in (animation is actually delegated to CSS transition).
- if (svg) {
- leg = m._spiderLeg;
- legPath = leg._path;
- legLength = legPath.getTotalLength() + 0.1;
- legPath.style.strokeDashoffset = legLength;
- leg.setStyle({
- opacity: 0
- });
- }
- }
- group._ignoreMove = false;
- setTimeout(function () {
- //If we have only <= one child left then that marker will be shown on the map so don't remove it!
- var stillThereChildCount = 0;
- for (i = childMarkers.length - 1; i >= 0; i--) {
- m = childMarkers[i];
- if (m._spiderLeg) {
- stillThereChildCount++;
- }
- }
- for (i = childMarkers.length - 1; i >= 0; i--) {
- m = childMarkers[i];
- if (!m._spiderLeg) { //Has already been unspiderfied
- continue;
- }
- if (m.clusterShow) {
- m.clusterShow();
- }
- if (m.setZIndexOffset) {
- m.setZIndexOffset(0);
- }
- if (stillThereChildCount > 1) {
- fg.removeLayer(m);
- }
- map.removeLayer(m._spiderLeg);
- delete m._spiderLeg;
- }
- group._animationEnd();
- group.fire('unspiderfied', {
- cluster: me,
- markers: childMarkers
- });
- }, 200);
- }
- });
- BM.MarkerClusterGroup.include({
- //The MarkerCluster currently spiderfied (if any)
- _spiderfied: null,
- unspiderfy: function () {
- this._unspiderfy.apply(this, arguments);
- },
- _spiderfierOnAdd: function () {
- this._map.on('click', this._unspiderfyWrapper, this);
- if (this._map.options.zoomAnimation) {
- this._map.on('zoomstart', this._unspiderfyZoomStart, this);
- }
- //Browsers without zoomAnimation or a big zoom don't fire zoomstart
- this._map.on('zoomend', this._noanimationUnspiderfy, this);
- if (!BM.Browser.touch) {
- this._map.getRenderer(this);
- //Needs to happen in the pageload, not after, or animations don't work in webkit
- // http://stackoverflow.com/questions/8455200/svg-animate-with-dynamically-added-elements
- //Disable on touch browsers as the animation messes up on a touch zoom and isn't very noticable
- }
- },
- _spiderfierOnRemove: function () {
- this._map.off('click', this._unspiderfyWrapper, this);
- this._map.off('zoomstart', this._unspiderfyZoomStart, this);
- this._map.off('zoomanim', this._unspiderfyZoomAnim, this);
- this._map.off('zoomend', this._noanimationUnspiderfy, this);
- //Ensure that markers are back where they should be
- // Use no animation to avoid a sticky bigemap-cluster-anim class on mapPane
- this._noanimationUnspiderfy();
- },
- //On zoom start we add a zoomanim handler so that we are guaranteed to be last (after markers are animated)
- //This means we can define the animation they do rather than Markers doing an animation to their actual location
- _unspiderfyZoomStart: function () {
- if (!this._map) { //May have been removed from the map by a zoomEnd handler
- return;
- }
- this._map.on('zoomanim', this._unspiderfyZoomAnim, this);
- },
- _unspiderfyZoomAnim: function (zoomDetails) {
- //Wait until the first zoomanim after the user has finished touch-zooming before running the animation
- if (BM.DomUtil.hasClass(this._map._mapPane, 'bigemap-touching')) {
- return;
- }
- this._map.off('zoomanim', this._unspiderfyZoomAnim, this);
- this._unspiderfy(zoomDetails);
- },
- _unspiderfyWrapper: function () {
- /// <summary>_unspiderfy but passes no arguments</summary>
- this._unspiderfy();
- },
- _unspiderfy: function (zoomDetails) {
- if (this._spiderfied) {
- this._spiderfied.unspiderfy(zoomDetails);
- }
- },
- _noanimationUnspiderfy: function () {
- if (this._spiderfied) {
- this._spiderfied._noanimationUnspiderfy();
- }
- },
- //If the given layer is currently being spiderfied then we unspiderfy it so it isn't on the map anymore etc
- _unspiderfyLayer: function (layer) {
- if (layer._spiderLeg) {
- this._featureGroup.removeLayer(layer);
- if (layer.clusterShow) {
- layer.clusterShow();
- }
- //Position will be fixed up immediately in _animationUnspiderfy
- if (layer.setZIndexOffset) {
- layer.setZIndexOffset(0);
- }
- this._map.removeLayer(layer._spiderLeg);
- delete layer._spiderLeg;
- }
- }
- });
- /**
- * Adds 1 public method to MCG and 1 to BM.Marker to facilitate changing
- * markers' icon options and refreshing their icon and their parent clusters
- * accordingly (case where their iconCreateFunction uses data of childMarkers
- * to make up the cluster icon).
- */
- BM.MarkerClusterGroup.include({
- /**
- * Updates the icon of all clusters which are parents of the given marker(s).
- * In singleMarkerMode, also updates the given marker(s) icon.
- * @param layers BM.MarkerClusterGroup|BM.LayerGroup|Array(BM.Marker)|Map(BM.Marker)|
- * BM.MarkerCluster|BM.Marker (optional) list of markers (or single marker) whose parent
- * clusters need to be updated. If not provided, retrieves all child markers of this.
- * @returns {BM.MarkerClusterGroup}
- */
- refreshClusters: function (layers) {
- if (!layers) {
- layers = this._topClusterLevel.getAllChildMarkers();
- } else if (layers instanceof BM.MarkerClusterGroup) {
- layers = layers._topClusterLevel.getAllChildMarkers();
- } else if (layers instanceof BM.LayerGroup) {
- layers = layers._layers;
- } else if (layers instanceof BM.MarkerCluster) {
- layers = layers.getAllChildMarkers();
- } else if (layers instanceof BM.Marker) {
- layers = [layers];
- } // else: must be an Array(BM.Marker)|Map(BM.Marker)
- this._flagParentsIconsNeedUpdate(layers);
- this._refreshClustersIcons();
- // In case of singleMarkerMode, also re-draw the markers.
- if (this.options.singleMarkerMode) {
- this._refreshSingleMarkerModeMarkers(layers);
- }
- return this;
- },
- /**
- * Simply flags all parent clusters of the given markers as having a "dirty" icon.
- * @param layers Array(BM.Marker)|Map(BM.Marker) list of markers.
- * @private
- */
- _flagParentsIconsNeedUpdate: function (layers) {
- var id, parent;
- // Assumes layers is an Array or an Object whose prototype is non-enumerable.
- for (id in layers) {
- // Flag parent clusters' icon as "dirty", all the way up.
- // Dumb process that flags multiple times upper parents, but still
- // much more efficient than trying to be smart and make short lists,
- // at least in the case of a hierarchy following a power law:
- // http://jsperf.com/flag-nodes-in-power-hierarchy/2
- parent = layers[id].__parent;
- while (parent) {
- parent._iconNeedsUpdate = true;
- parent = parent.__parent;
- }
- }
- },
- /**
- * Re-draws the icon of the supplied markers.
- * To be used in singleMarkerMode only.
- * @param layers Array(BM.Marker)|Map(BM.Marker) list of markers.
- * @private
- */
- _refreshSingleMarkerModeMarkers: function (layers) {
- var id, layer;
- for (id in layers) {
- layer = layers[id];
- // Make sure we do not override markers that do not belong to THIS group.
- if (this.hasLayer(layer)) {
- // Need to re-create the icon first, then re-draw the marker.
- layer.setIcon(this._overrideMarkerIcon(layer));
- }
- }
- }
- });
- BM.Marker.include({
- /**
- * Updates the given options in the marker's icon and refreshes the marker.
- * @param options map object of icon options.
- * @param directlyRefreshClusters boolean (optional) true to trigger
- * MCG.refreshClustersOf() right away with this single marker.
- * @returns {BM.Marker}
- */
- refreshIconOptions: function (options, directlyRefreshClusters) {
- var icon = this.options.icon;
- BM.setOptions(icon, options);
- this.setIcon(icon);
- // Shortcut to refresh the associated MCG clusters right away.
- // To be used when refreshing a single marker.
- // Otherwise, better use MCG.refreshClusters() once at the end with
- // the list of modified markers.
- if (directlyRefreshClusters && this.__parent) {
- this.__parent._group.refreshClusters(this);
- }
- return this;
- }
- });
- exports.MarkerClusterGroup = MarkerClusterGroup;
- exports.MarkerCluster = MarkerCluster;
- })));
- //# sourceMappingURL=bigemap.markercluster-src.js.map
|