15
votes

I have a leaflet map in a div that I loa like this:

  <script src="http://cdn.leafletjs.com/leaflet-0.4/leaflet.js"></script>

  <!--[if lt IE 9]>
  <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
  <![endif]-->
  <link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet-0.4/leaflet.css" />
  <!--[if lte IE 8]>
  <link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet-0.4/leaflet.ie.css" />
  <![endif]-->
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />

I then have a div such as this:

Some js that loads the map:

map = L.map($attrs.id,
                         center: [40.094882122321145, -3.8232421874999996]
                         zoom: 5
      )
      L.tileLayer("http://{s}.tile.cloudmade.com/57cbb6ca8cac418dbb1a402586df4528/997/256/{z}/{x}/{y}.png",
                  maxZoom: 18
      ).addTo map

Then on page load I have some JS that monitors window height and resizes:

$("#map").height($(window).height())
$(window).resize(_.throttle(->
  $("#map").height($(window).height())
, 100
))

However when the map loads it loads with half the tiles in grey. When i resize the map loads fine but the initial load is 1/2 grey

2
I then have a div such as this: where description?tbicr

2 Answers

20
votes

If you don't have a reason to use JS for map sizing its probably better to use CSS:

html, body, #map {
   width: 100%;
   height: 100%;
}

However you can try using map.invalidateSize() after inserting the map into the DOM (or map resized by $("#map").height($(window).height())).

invalidateSize() is called by default when you resize the window, see the following links: https://github.com/Leaflet/Leaflet/blob/master/src/map/Map.js#L609 and https://github.com/Leaflet/Leaflet/blob/master/src/map/Map.js#L616.

0
votes

After a long search on this issue, in my case I had a global css style which was :

<pre>
img {
    position: relative;
    display: block;
    max-width: 100%;
}
</pre>

In my case problem were the "position : relative", so I add a line

<pre>
//Fix personnal bug leaflet
.leaflet-pane{
    img{
        position: absolute;
    }
}
</pre>