Skip to content

Instantly share code, notes, and snippets.

@turban
Created November 4, 2012 17:43
Show Gist options
  • Save turban/4012740 to your computer and use it in GitHub Desktop.
Save turban/4012740 to your computer and use it in GitHub Desktop.
L.Wax = L.Class.extend({
initialize: function (map) {
this.map = map;
var layers = map.options.layers || [];
var wax = false;
var self = this;
map.on('layeradd', function(e) {
if (e.layer.options.wax) {
self._getTilejson(e.layer, self._addWax);
wax = true;
} else {
wax = false;
}
});
map.on('layerremove', function(e) {
if (e.layer.options.wax && !wax) {
self._removeWax();
}
});
for (var i = 0; i < layers.length; i++) {
if (layers[i].options.wax) {
this._getTilejson(layers[i], this._addWax);
wax = true;
}
}
},
_getTilejson: function (layer, callback) {
var self = this;
var tilejson = layer.options.wax;
if (typeof tilejson === 'string') {
wax.tilejson(tilejson, function(response) {
layer.options.wax = response;
callback.call(self, response)
});
} else {
callback.call(this, tilejson)
}
},
_addWax: function (tilejson) {
this._removeWax();
if (tilejson.legend) {
this.legend = wax.leaf.legend(this.map, tilejson).appendTo(this.map._container);
};
if (tilejson.grids) {
this.tooltip = wax.tooltip();
this.interaction = wax.leaf.interaction()
.map(map)
.tilejson(tilejson)
.on(this.tooltip.animate(true).parent(map._container).events());
}
},
_removeWax: function() {
if (this.legend) {
var el = this.legend.element();
el.parentNode.removeChild(el);
this.legend = null;
}
if (this.interaction) {
var el = document.getElementsByClassName('wax-tooltip')[0];
if (el) {
this.tooltip.parent().removeChild(el);
}
this.interaction.remove();
this.interaction = null;
}
}
});
L.wax = function (map) {
return new L.Wax(map);
};
@brunob
Copy link

brunob commented Jun 11, 2013

Hi Bjørn, you should reference this nice plugin on plugins page of @Leaflet website ;)

You can do that on this repo https://github.com/Leaflet/Leaflet/tree/gh-pages-master

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment