javascript - Mapbox.js: Clear a custom legend? -


i using mapbox.js browserify. want create module creates map if not exist, updates if does.

i have got way there, i'm having problems updating custom legend. seem able append it, , can't clear it.

this code:

var analysemap = {   setupmap: function(options) {     var _this = this;     l.mapbox.accesstoken = 'mytoken';     if (typeof this.map === 'undefined') {         this.map = l.mapbox.map('map', 'mapbox.streets').setview([53.1, -2.2], 6);         this.layergroup = l.layergroup().addto(this.map);     } else {         this.layergroup.clearlayers();     }     var geojson_url = "/api/1.0/myurlbasedonoptions";     $.getjson(geojson_url, function(boundaries) {       var orglayer = l.geojson(boundarieswithdata, { style: getstyle });       _this.layergroup.addlayer(orglayer);       _this.map.fitbounds(orglayer.getbounds());       var popup = new l.popup({ autopan: false });        var legendhtml = _this.getlegendhtml(options);       _this.map.legendcontrol.removelegend();       _this.map.legendcontrol.addlegend(legendhtml); 

then call module this:

// on initialise , update map.setupmap(someoptions); 

this works great first time call it. however, on update, map updates , layers update nicely, end 2 legends.

how can clear existing legend before adding new one?

the documentation suggests need know value of legend html in order remove it, seems strange.

ok, fixed doing this:

if (typeof _this.legendhtml !== 'undefined') {     _this.map.legendcontrol.removelegend(_this.legendhtml); } _this.legendhtml = _this.getlegendhtml(_this.quintiles, options); _this.map.legendcontrol.addlegend(_this.legendhtml); 

would still interested see if has more elegant solution though!


Comments

Popular posts from this blog

javascript - Google App Script ContentService downloadAsFile not working -

javascript - Function overwritting -

php - Find a regex to take part of Email -