Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

leaflet loads incomplete map

Tags:

leaflet

I am dynamically adding a leaflet map to a div. The map is only shown partially, see picture. Once I resize the browser window the map is completely shown. Is there a way around this? I tried some initial ideas, see // in code. But none of them worked.incomplete map

    function mapThis(lat,long,wikiTitle){       

     var div_Id= "#wikiExtract"+wikiTitle
     var map_Id= "map"+wikiTitle
     $(div_Id).append("<div id='"+map_Id+"' style='width: 600px; height:
     400px'></div>"); 
     var map = L.map(map_Id).setView([lat, long], 10);

     L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
     maxZoom: 18,
     attribution: 'Map data &copy; <a
     href="http://openstreetmap.org">OpenStreetMap</a> contributors, ' +
        '<a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-
          SA</a>'
      }).addTo(map);


     //$("#"+map_Id).css( "height", "500px" );
     //map.invalidateSize()
     //$("#"+map_Id).hide()   
     //$("#"+map_Id).show()
     //$(window).trigger('resize');

    }
like image 782
user2133375 Avatar asked Mar 16 '16 13:03

user2133375


3 Answers

you can use this code : it work find for me.

setInterval(function () {
   map.invalidateSize();
}, 100);
like image 158
HamidReza Avatar answered Nov 16 '22 13:11

HamidReza


If at least you have the map working (except for the incompleteness of tiles loading), you could simply call map.invalidateSize() once your map's div container is revealed / inserted into DOM with final size.

Checks if the map container size changed and updates the map if so — call it after you've changed the map size dynamically, also animating pan by default.

like image 10
ghybs Avatar answered Nov 16 '22 12:11

ghybs


If using JQuery, the following code updates the size once the document is ready:

$(document).ready(function () {
    mymap.invalidateSize();
});
like image 2
Daniel H Avatar answered Nov 16 '22 12:11

Daniel H