leaflet - creating lealetjs map in an invisible div -
i adding leafletjs
map in remarkjs
slideshow. map works fine if slide containing map div visible on initial load of web page. however, if slide map div not visible slide map div invisible, leaflet map doesn't work tiles not loaded in div. i'd load map divs in slideshow, no matter slide may on, , have them show when slide containing maps comes up.
update: suggested answer seem should answer question, still stuck , not sure if on right track. below more details.
since adding many maps slideshow, using class name map divs.
// css .map { width: 100vh; height: 100vh; } // html <div class="map" data-lat="47" data-lon="106"></div> <div class="map" data-lat="0" data-lon="0"></div> <!-- possibly on different slide --> <div class="map" data-lat="30" data-lon="28"></div> // js var maps = []; // storing map elements var mapdivs = document.getelementsbyclassname('map'); (var i=0, j=mapdivs.length; i<j; i++) { var map = l.map(mapdivs[i]).setview( [mapdivs[i].dataset.lat, mapdivs[i].dataset.lon], 13 ); // store map later maps.push(map); l.tilelayer(…).addto(map); } slideshow.on('showslide', function (slide) { (var i=0, j=maps.length; i<j; i++) { maps[i].invalidatesize(); } });
my logic says above should work, doesn't. doing wrong?
update2: map.invalidatesize()
answer indeed part of solution. fault was calling slideshow.on('showslide', fn)
should have been calling slideshow.on('aftershowslide', fn)
. latter gives remarkjs
opportunity create current slide may have map div in turn allows map.invalidatesize()
correctly fire , repaint map.
leafletjs
requires div of width , height > 0px exist, is, visible, not hidden, download required map tiles , show map. adding map divs remarkjs
slideshow means 1 or more of divs hidden when slideshow launched. consequently, leafletjs
not able show map in divs (random tiles missing) until browser resized forcing leafletjs
recalc size of map divs , download correct tiles. solution fire map.invalidatesize()
method when slide map div displayed. here how did finally
/* css ******************************/ .map { width: 100vh; height: 100vh; } <!--------------------------------------------- html map divs possibly on different slides, `map: true` property on each slide map -----------------------------------------------> --- map: true <div class="map" data-lat="47" data-lon="106"></div> --- map: true <div class="map" data-lat="0" data-lon="0"></div> --- map: true <div class="map" data-lat="30" data-lon="28"></div> // js ///////////////////////////////////////// var maps = []; // storing leaflet map elements var mapdivs = document.getelementsbyclassname('map'); (var i=0, j=mapdivs.length; i<j; i++) { // store map later maps.push(l.map(mapdivs[i]).setview( [mapdivs[i].dataset.lat, mapdivs[i].dataset.lon], zoomlevel )); // add map tilelayer l.tilelayer(…).addto(maps[i]); } // use 'aftershowslide' event ensure // map div has been created slideshow.on('aftershowslide', function (slide) { // if slide has 'map' property if (slide.properties.map) { (var i=0, j=maps.length; i<j; i++) { maps[i].invalidatesize(); } } });
Comments
Post a Comment