Leaflet map not showing properly in bootstrap 3.0 modal

后端 未结 7 1533
迷失自我
迷失自我 2020-12-03 07:47

i have a big problem. i want to open a leaflet map in a modal. but the map is not showing properly. the tiles are not loading.

here is the script:

http://boo

相关标签:
7条回答
  • 2020-12-03 07:57

    This works for me, you can read here

    map.whenReady(() => {
        console.log('Map ready');
        setTimeout(() => {
            map.invalidateSize();
        }, 0);
    });
    
    0 讨论(0)
  • 2020-12-03 07:57

    leaflet map uncorrect display on load can be solved with this :

      var mapid = $(this).find('[id^=leaflet-map]').attr('id');
      var map = settings.leaflet[mapid].lMap;
      map.invalidateSize();
    
    0 讨论(0)
  • 2020-12-03 08:00

    How I solved this issue: init map in main window. Then move map to opened modal.

    0 讨论(0)
  • 2020-12-03 08:01

    I think what is happening is that, when the map is created, the container width/height for your `map-canvas' element has not yet been adjusted to the width/height of the modal dialog. This causes the map size to be incorrect (smaller) than what it should be.

    You can fix this by calling map.invalidateSize(). This will work to re-adjust the width/height bounds of the L.Map's container.

    You can automatically call this by hooking into the event where the Bootstrap modal becomes shown.

    $('#myModal').on('show.bs.modal', function(){
      setTimeout(function() {
        map.invalidateSize();
      }, 10);
     });
    

    Insert this code into your JavaScript. When the modal is shown, the map will then invalidate its size. The timeout is because there may be some animation/transition time for the modal to display and be added to the DOM.

    0 讨论(0)
  • 2020-12-03 08:02

    You should probably avoid using setTimeout with a randomly chosen delay. A better way using the 'shown.bs.modal' event instead of 'show.bs.modal':

    modal.on('shown.bs.modal', function(){
        setTimeout(function() {
            map.invalidateSize();
       }, 1);
    })
    

    Or use underscore's defer :

    modal.on('shown.bs.modal', function(){
        _.defer(map.invalidateSize.bind(map));
    })
    
    0 讨论(0)
  • 2020-12-03 08:10

    This worked for me -

     $('#gmap').on('shown.bs.tab', function (e) {
        //call the clear map event first
        clearMap();
        //resize the map - this is the important part for you
       map.invalidateSize(true);
       //load the map once all layers cleared
       loadMap();
    })
    
    0 讨论(0)
提交回复
热议问题