Skip to content

Commit

Permalink
Merge pull request Esri#413 from MandarinConLaBarba/minMaxZoom
Browse files Browse the repository at this point in the history
Min max zoom
  • Loading branch information
patrickarlt committed Dec 5, 2014
2 parents 69ed3c5 + 4830a05 commit 94f4f9c
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 2 deletions.
55 changes: 54 additions & 1 deletion spec/Layers/FeatureLayer/FeatureManagerSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ describe('L.esri.Layers.FeatureManager', function () {

layer = new MockLayer(url, {
timeField: 'Time',
attribution: 'Esri'
attribution: 'Esri',
minZoom : 1,
maxZoom : 15
});
});

Expand Down Expand Up @@ -768,4 +770,55 @@ describe('L.esri.Layers.FeatureManager', function () {
expect(requeststartSpy.callCount).to.be.above(0);
expect(requestendSpy.callCount).to.be.above(0);
});

it('should NOT create layers when the zoom level is outside allowed range', function (done) {

map.setZoom(14);

server.respondWith('GET', 'http://services.arcgis.com/mock/arcgis/rest/services/MockService/MockFeatureServer/0/query?returnGeometry=true&where=1%3D1&outSr=4326&outFields=*&inSr=4326&geometry=%7B%22xmin%22%3A-122.6953125%2C%22ymin%22%3A45.521743896993634%2C%22xmax%22%3A-122.6513671875%2C%22ymax%22%3A45.55252525134013%2C%22spatialReference%22%3A%7B%22wkid%22%3A4326%7D%7D&geometryType=esriGeometryEnvelope&spatialRel=esriSpatialRelIntersects&geometryPrecision=6&f=json', JSON.stringify({
fields: fields,
features: [feature1, feature2],
objectIdFieldName: 'OBJECTID'
}));

layer.addTo(map);

map.once('zoomend', function () {

server.respond();

expect(layer.createLayers).not.to.have.been.called;
done();
});

map.setZoom(17);


});

it('should create layers when the zoom level is inside allowed range', function (done) {

map.setZoom(14);

server.respondWith('GET', 'http://services.arcgis.com/mock/arcgis/rest/services/MockService/MockFeatureServer/0/query?returnGeometry=true&where=1%3D1&outSr=4326&outFields=*&inSr=4326&geometry=%7B%22xmin%22%3A-122.6953125%2C%22ymin%22%3A45.521743896993634%2C%22xmax%22%3A-122.6513671875%2C%22ymax%22%3A45.55252525134013%2C%22spatialReference%22%3A%7B%22wkid%22%3A4326%7D%7D&geometryType=esriGeometryEnvelope&spatialRel=esriSpatialRelIntersects&geometryPrecision=6&f=json', JSON.stringify({
fields: fields,
features: [feature1, feature2],
objectIdFieldName: 'OBJECTID'
}));

layer.addTo(map);

map.once('zoomend', function () {

server.respond();

expect(layer.createLayers).to.have.been.called;
done();
});

map.setZoom(11);


});

});
17 changes: 16 additions & 1 deletion src/Layers/FeatureLayer/FeatureGrid.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ EsriLeaflet.Layers.FeatureGrid = L.Class.extend({
getEvents: function () {
var events = {
viewreset: this._reset,
moveend: this._update
moveend: this._update,
zoomend : this._onZoom
};

return events;
Expand All @@ -46,6 +47,20 @@ EsriLeaflet.Layers.FeatureGrid = L.Class.extend({
return this;
},

_onZoom : function () {
var zoom = this._map.getZoom();

if (zoom > this.options.maxZoom ||
zoom < this.options.minZoom) {
this.removeFrom(this._map);
this._map.addEventListener('zoomend', this.getEvents().zoomend, this);
} else if (!this._map.hasLayer(this)) {
this._map.removeEventListener('zoomend', this.getEvents().zoomend, this);
this.addTo(this._map);
}

},

_reset: function () {
this._removeCells();

Expand Down
5 changes: 5 additions & 0 deletions src/Layers/FeatureLayer/FeatureManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,11 @@
this._buildTimeIndexes(features);
}

var zoom = this._map.getZoom();

if (zoom > this.options.maxZoom ||
zoom < this.options.minZoom) { return; }

this.createLayers(features);
},

Expand Down

0 comments on commit 94f4f9c

Please sign in to comment.