Skip to content

Commit

Permalink
eachActiveFeature() to return only layers currently in view (#936)
Browse files Browse the repository at this point in the history
* create method that returns only layers currently in view

* fix snapshot logic and test against it
  • Loading branch information
jgravois authored Mar 13, 2017
1 parent df09fca commit 4fb2078
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
13 changes: 13 additions & 0 deletions src/Layers/FeatureLayer/FeatureLayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,19 @@ export var FeatureLayer = FeatureManager.extend({
* Utility Methods
*/

eachActiveFeature: function (fn, context) {
// figure out (roughly) which layers are in view
if (this._map) {
var activeBounds = this._map.getBounds();
for (var i in this._layers) {
if (activeBounds.intersects(this._layers[i].getBounds()) && this._currentSnapshot.indexOf(this._layers[i].feature.id) !== -1) {
fn.call(context, this._layers[i]);
}
}
}
return this;
},

eachFeature: function (fn, context) {
for (var i in this._layers) {
fn.call(context, this._layers[i]);
Expand Down
14 changes: 8 additions & 6 deletions src/Layers/FeatureLayer/FeatureManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ export var FeatureManager = VirtualGrid.extend({
},

_postProcessFeatures: function (bounds) {
// deincriment the request counter now that we have processed features
// deincrement the request counter now that we have processed features
this._activeRequests--;

// if there are no more active requests fire a load event for this view
Expand All @@ -177,17 +177,19 @@ export var FeatureManager = VirtualGrid.extend({

for (var i = features.length - 1; i >= 0; i--) {
var id = features[i].id;
this._currentSnapshot.push(id);
this._cache[key].push(id);

if (this._currentSnapshot.indexOf(id) === -1) {
this._currentSnapshot.push(id);
}
if (this._cache[key].indexOf(id) === -1) {
this._cache[key].push(id);
}
}

if (this.options.timeField) {
this._buildTimeIndexes(features);
}

// need to PR removal of the logic below too...
// https://github.com/patrickarlt/leaflet-virtual-grid/blob/master/src/virtual-grid.js#L100-L102

this.createLayers(features);
},

Expand Down

0 comments on commit 4fb2078

Please sign in to comment.