Skip to content

Instantly share code, notes, and snippets.

@stefanocudini
Created July 4, 2017 17:49
Show Gist options
  • Save stefanocudini/892d4337ccad5c00f97f6921f0c0d57c to your computer and use it in GitHub Desktop.
Save stefanocudini/892d4337ccad5c00f97f6921f0c0d57c to your computer and use it in GitHub Desktop.
Leaflet layer geojson self cleaning on zoom out
L.GeoJSONAutoClear = L.GeoJSON.extend({
options: {
autoclear: true,
zoomdiff: 2
},
_autoClear: function(e) {
if(this.getLayers().length) {
if(e.target.getZoom() < e.target.getBoundsZoom(this.getBounds())-this.options.zoomdiff)
this.clearLayers();
}
},
onAdd: function(map) {
L.GeoJSON.prototype.onAdd.call(this, map);
if(this.options.autoclear)
map.on('zoomend', this._autoClear, this);
},
onRemove: function(map) {
L.GeoJSON.prototype.onRemove.call(this, map);
if(this.options.autoclear)
map.off('zoomend', this._autoClear, this);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment