Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Leaflet map not showing properly in bootstrap 3.0 modal

Tags:

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://bootply.com/98730

<a href="#myModal" role="button" class="btn btn-primary" data-toggle="modal">Open Map</a>  <div id="myModal" class="modal"> <div class="modal-dialog">   <div class="modal-content">     <div class="modal-header">       <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>       <h4 class="modal-title">Map</h4>     </div>     <div class="modal-body">       <div class="modal-body" id="map-canvas"></div>     </div>     <div class="modal-footer">       <button type="button" class="btn" data-dismiss="modal" aria-hidden="true">OK</button>     </div>   </div> </div> 

$.getScript('http://cdn.leafletjs.com/leaflet-0.7/leaflet.js',function(){   /* map settings */  var map = new L.Map('map-canvas');  var cloudmade = new    L.TileLayer('http://{s}.tile.cloudmade.com/f1376bb0c116495e8cb9121360802fb0/997/256/{z}/{x} /{y}.png', {  attribution: 'Map data © <a href="http://openstreetmap.org">OpenStreetMap</a>   contributors, Imagery © <a href="http://cloudmade.com">CloudMade</a>',  maxZoom: 18  });  map.addLayer(cloudmade).setView(new L.LatLng(41.52, -71.09), 13);    }); 

any help much apreciated

like image 284
user2975100 Avatar asked Dec 05 '13 13:12

user2975100


People also ask

Why is map not showing in leaflet?

There are a number of reasons why your map might not be displaying: You have an error in your JavaScript (most likely) - use whichever debugging tool is provided with your browser to verify. you are using Internet Explorer and you have Compatibility mode ON....

How do you make a modal scrollable?

Use the . modal-dialog-scrollable class to enable scrolling inside the modal.

Does leaflet require API key?

API Key. Important: Usage of the Leaflet Plugins requires an API key. If you do not already have one, please sign in to the MapQuest Developer Network and visit the Keys & Reporting page.


2 Answers

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.

like image 153
Patrick D Avatar answered Sep 17 '22 15:09

Patrick D


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)); }) 
like image 31
Erik Avatar answered Sep 20 '22 15:09

Erik