Merge branch 'better-loading'

Conflicts:
	dist/leaflet.markercluster.js
This commit is contained in:
danzel
2014-01-22 09:26:06 +13:00
5 changed files with 201 additions and 128 deletions
+71 -62
View File
@@ -31,6 +31,10 @@ L.MarkerClusterGroup = L.FeatureGroup.extend({
//Increase to increase the distance away that spiderfied markers appear from the center
spiderfyDistanceMultiplier: 1,
//When bulk adding layers, runs chunks at a time. Means addLayers may not add all the layers in the call, others will be loaded during setTimeouts
chunkedLoading: false,
chunkSize: 500,
//Options to pass to the L.Polygon constructor
polygonOptions: {}
},
@@ -157,52 +161,76 @@ L.MarkerClusterGroup = L.FeatureGroup.extend({
//Takes an array of markers and adds them in bulk
addLayers: function (layersArray) {
var i, l, m,
onMap = this._map,
fg = this._featureGroup,
npg = this._nonPointGroup;
var fg = this._featureGroup,
npg = this._nonPointGroup,
chunkSize = this.options.chunkSize,
newMarkers, i, l, m;
for (i = 0, l = layersArray.length; i < l; i++) {
m = layersArray[i];
if (this._map) {
var start = 0;
var end = this.options.chunkedLoading && chunkSize < layersArray.length ? chunkSize : layersArray.length;
var process = L.bind(function () {
for (i = start; i < end; i++) {
m = layersArray[i];
//Not point data, can't be clustered
if (!m.getLatLng) {
npg.addLayer(m);
continue;
}
//Not point data, can't be clustered
if (!m.getLatLng) {
npg.addLayer(m);
continue;
}
if (this.hasLayer(m)) {
continue;
}
if (this.hasLayer(m)) {
continue;
}
if (!onMap) {
this._needsClustering.push(m);
continue;
}
this._addLayer(m, this._maxZoom);
this._addLayer(m, this._maxZoom);
//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 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 (onMap) {
//Update the icons of all those visible clusters that were affected
fg.eachLayer(function (c) {
if (c instanceof L.MarkerCluster && c._iconNeedsUpdate) {
c._updateIcon();
if (end === layersArray.length) {
//Update the icons of all those visible clusters that were affected
this._featureGroup.eachLayer(function (c) {
if (c instanceof L.MarkerCluster && c._iconNeedsUpdate) {
c._updateIcon();
}
});
this._topClusterLevel._recursivelyAddChildrenToMap(null, this._zoom, this._currentShownBounds);
} else {
start = end;
end = Math.min(end + chunkSize, layersArray.length);
setTimeout(process, 0);
}
});
}, this);
this._topClusterLevel._recursivelyAddChildrenToMap(null, this._zoom, this._currentShownBounds);
process();
} else {
newMarkers = [];
for (i = 0, l = layersArray.length; i < l; i++) {
m = layersArray[i];
//Not point data, can't be clustered
if (!m.getLatLng) {
npg.addLayer(m);
continue;
}
if (this.hasLayer(m)) {
continue;
}
newMarkers.push(m);
}
this._needsClustering = this._needsClustering.concat(newMarkers);
}
return this;
},
@@ -418,23 +446,9 @@ L.MarkerClusterGroup = L.FeatureGroup.extend({
}
this._needsRemoving = [];
for (i = 0, l = this._needsClustering.length; i < l; i++) {
layer = this._needsClustering[i];
//If the layer doesn't have a getLatLng then we can't cluster it, so add it to our child featureGroup
if (!layer.getLatLng) {
this._featureGroup.addLayer(layer);
continue;
}
if (layer.__parent) {
continue;
}
this._addLayer(layer, this._maxZoom);
}
this._needsClustering = [];
//Remember the current zoom level and bounds
this._zoom = this._map.getZoom();
this._currentShownBounds = this._getExpandedVisibleBounds();
this._map.on('zoomend', this._zoomEnd, this);
this._map.on('moveend', this._moveEnd, this);
@@ -445,15 +459,10 @@ L.MarkerClusterGroup = L.FeatureGroup.extend({
this._bindEvents();
//Actually add our markers to the map:
//Remember the current zoom level and bounds
this._zoom = this._map.getZoom();
this._currentShownBounds = this._getExpandedVisibleBounds();
//Make things appear on the map
this._topClusterLevel._recursivelyAddChildrenToMap(null, this._zoom, this._currentShownBounds);
l = this._needsClustering;
this._needsClustering = [];
this.addLayers(l);
},
//Overrides FeatureGroup.onRemove
@@ -631,7 +640,7 @@ L.MarkerClusterGroup = L.FeatureGroup.extend({
e.layer.zoomToBounds();
}
// Focus the map again for keyboard users.
// Focus the map again for keyboard users.
if (e.originalEvent && e.originalEvent.keyCode === 13) {
map._container.focus();
}
@@ -27,7 +27,7 @@
latlng = L.latLng(-37.89, 175.46);
var map = L.map('map', {center: latlng, zoom: 13, layers: [cloudmade]});
var markers = L.markerClusterGroup();
var markers = L.markerClusterGroup({ chunkedLoading: true });
for (var i = 0; i < addressPoints.length; i++) {
var a = addressPoints[i];
+19 -2
View File
@@ -18,6 +18,7 @@
</head>
<body>
<div id="progress"><div id="progress-bar"></div></div>
<div id="map"></div>
<span>Mouse over a cluster to see the bounds of its children and click a cluster to zoom to those bounds</span>
<script type="text/javascript">
@@ -28,12 +29,28 @@
var map = L.map('map', { center: latlng, zoom: 13, layers: [cloudmade] });
var markers = L.markerClusterGroup();
var progress = document.getElementById('progress');
var progressBar = document.getElementById('progress-bar');
function updateProgressBar(processed, total, elapsed, layersArray) {
if (elapsed > 1000) {
// if it takes more than a second to load, display the progress bar:
progress.style.display = 'block';
progressBar.style.width = Math.round(processed/total*100) + '%';
}
if (processed === total) {
// all markers processed - hide the progress bar:
progress.style.display = 'none';
}
}
var markers = L.markerClusterGroup({ chunkedLoading: true, chunkProgress: updateProgressBar });
var markerList = [];
//console.log('start creating markers: ' + window.performance.now());
for (var i = 0; i < addressPoints.length; i++) {
var a = addressPoints[i];
var title = a[2];
+24 -1
View File
@@ -2,4 +2,27 @@
width: 800px;
height: 600px;
border: 1px solid #ccc;
}
}
#progress {
display: none;
position: absolute;
z-index: 1000;
left: 400px;
top: 300px;
width: 200px;
height: 20px;
margin-top: -20px;
margin-left: -100px;
background-color: #fff;
background-color: rgba(255, 255, 255, 0.7);
border-radius: 4px;
padding: 2px;
}
#progress-bar {
width: 0;
height: 100%;
background-color: #76A6FC;
border-radius: 4px;
}
+86 -62
View File
@@ -26,6 +26,12 @@ L.MarkerClusterGroup = L.FeatureGroup.extend({
//Increase to increase the distance away that spiderfied markers appear from the center
spiderfyDistanceMultiplier: 1,
// 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 L.Polygon constructor
polygonOptions: {}
},
@@ -152,52 +158,89 @@ L.MarkerClusterGroup = L.FeatureGroup.extend({
//Takes an array of markers and adds them in bulk
addLayers: function (layersArray) {
var i, l, m,
onMap = this._map,
fg = this._featureGroup,
npg = this._nonPointGroup;
var fg = this._featureGroup,
npg = this._nonPointGroup,
chunkInterval = this.options.chunkInterval,
chunkProgress = this.options.chunkProgress,
newMarkers, i, l, m;
for (i = 0, l = layersArray.length; i < l; i++) {
m = layersArray[i];
if (this._map) {
var offset = 0,
started = (new Date()).getTime();
var process = L.bind(function () {
var start = (new Date()).getTime();
for (; offset < layersArray.length; offset++) {
if (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 :-)
}
}
//Not point data, can't be clustered
if (!m.getLatLng) {
npg.addLayer(m);
continue;
}
m = layersArray[offset];
if (this.hasLayer(m)) {
continue;
}
//Not point data, can't be clustered
if (!m.getLatLng) {
npg.addLayer(m);
continue;
}
if (!onMap) {
this._needsClustering.push(m);
continue;
}
if (this.hasLayer(m)) {
continue;
}
this._addLayer(m, this._maxZoom);
this._addLayer(m, this._maxZoom);
//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 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 (onMap) {
//Update the icons of all those visible clusters that were affected
fg.eachLayer(function (c) {
if (c instanceof L.MarkerCluster && c._iconNeedsUpdate) {
c._updateIcon();
if (chunkProgress) {
// report progress and time elapsed:
chunkProgress(offset, layersArray.length, (new Date()).getTime() - started);
}
});
this._topClusterLevel._recursivelyAddChildrenToMap(null, this._zoom, this._currentShownBounds);
if (offset === layersArray.length) {
//Update the icons of all those visible clusters that were affected
this._featureGroup.eachLayer(function (c) {
if (c instanceof L.MarkerCluster && c._iconNeedsUpdate) {
c._updateIcon();
}
});
this._topClusterLevel._recursivelyAddChildrenToMap(null, this._zoom, this._currentShownBounds);
} else {
setTimeout(process, this.options.chunkDelay);
}
}, this);
process();
} else {
newMarkers = [];
for (i = 0, l = layersArray.length; i < l; i++) {
m = layersArray[i];
//Not point data, can't be clustered
if (!m.getLatLng) {
npg.addLayer(m);
continue;
}
if (this.hasLayer(m)) {
continue;
}
newMarkers.push(m);
}
this._needsClustering = this._needsClustering.concat(newMarkers);
}
return this;
},
@@ -296,7 +339,7 @@ L.MarkerClusterGroup = L.FeatureGroup.extend({
//Overrides LayerGroup.eachLayer
eachLayer: function (method, context) {
var markers = this._needsClustering.slice(),
i;
i;
if (this._topClusterLevel) {
this._topClusterLevel.getAllChildMarkers(markers);
@@ -413,23 +456,9 @@ L.MarkerClusterGroup = L.FeatureGroup.extend({
}
this._needsRemoving = [];
for (i = 0, l = this._needsClustering.length; i < l; i++) {
layer = this._needsClustering[i];
//If the layer doesn't have a getLatLng then we can't cluster it, so add it to our child featureGroup
if (!layer.getLatLng) {
this._featureGroup.addLayer(layer);
continue;
}
if (layer.__parent) {
continue;
}
this._addLayer(layer, this._maxZoom);
}
this._needsClustering = [];
//Remember the current zoom level and bounds
this._zoom = this._map.getZoom();
this._currentShownBounds = this._getExpandedVisibleBounds();
this._map.on('zoomend', this._zoomEnd, this);
this._map.on('moveend', this._moveEnd, this);
@@ -440,15 +469,10 @@ L.MarkerClusterGroup = L.FeatureGroup.extend({
this._bindEvents();
//Actually add our markers to the map:
//Remember the current zoom level and bounds
this._zoom = this._map.getZoom();
this._currentShownBounds = this._getExpandedVisibleBounds();
//Make things appear on the map
this._topClusterLevel._recursivelyAddChildrenToMap(null, this._zoom, this._currentShownBounds);
l = this._needsClustering;
this._needsClustering = [];
this.addLayers(l);
},
//Overrides FeatureGroup.onRemove
@@ -626,7 +650,7 @@ L.MarkerClusterGroup = L.FeatureGroup.extend({
e.layer.zoomToBounds();
}
// Focus the map again for keyboard users.
// Focus the map again for keyboard users.
if (e.originalEvent && e.originalEvent.keyCode === 13) {
map._container.focus();
}