22

I am instantiating a leaflet map, but the tiles are basically being scattered all over the page - while the map is within a div, most of the tiles are not respecting that boundary:

<div class="widget-content listing-search-map-widget-content">
    <div class="ih-map" 
         id="Map_5333811_16"
         style="height:450px;"
         data-centerpoint="38.573955 -121.442478" 
         data-mousewheel="true" 
         data-maptype="TERRAIN"
         data-zoom="8"
        >
    </div>
</div>

The javascript comes down to:

mapOptions = {
               attributionControl: true,
               center: {
                   lat: 38.573955
                   lng: -121.442478
               },
               centerpoint: "38.573955,-121.442478", 
               layers: {},
               maptype: "Terrain", 
               scrollWheelZoom: false, 
               zoom: 8
            }

var map = L.map( "Map_5333811_16", mapOptions );

What would cause the tiles to plot all over the place?a couple of tiles are within the bounds of the div, but not the rest of them. You can see a screenshot of what happens here:

screenshot of jumbled tiles

3 Answers 3

42

This sounds like a symptom of missing Leaflet CSS file, or incorrect version of that file.

3
  • arg... missed a line deep in a smarty template. Thanks for the pointer, that fixed it all up. Commented Aug 8, 2016 at 23:08
  • I am using Polymer 2 with leaflet and don't seem to find any CSS issue. Here is my post in SO: stackoverflow.com/questions/53872322/…. Hope you can help.
    – sd1517
    Commented Dec 27, 2018 at 14:13
  • what file was missing?
    – HazeyAce
    Commented Oct 10, 2019 at 19:39
9

As has been pointed out, this is solved by importing the CSS files.

Classically, you include the link to your CSS stylesheet in the head section of your document:

 <link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/leaflet.css"
   integrity="sha512-xwE/Az9zrjBIphAcBb3F6JVqxf46+CDLwfLMHloNu6KEQCAWi6HcDUbeOfBIptF7tcCzusKFjFw2yuvEpDL9wQ=="
   crossorigin=""/>

Or, in my case, stumbling across this problem while adapting Vue CLI's Webpack template, by adding

import "leaflet/dist/leaflet.css";

into the main.js file.


The key lines of CSS appear to be these:

.leaflet-layer {
    position: absolute;
    left: 0;
    top: 0;
    }

so knowing that you can insert them at whatever point in your project suits your style.

2
  • Note: importing a CSS file in JS is specific to your build engine (typically webpack). In this very case, OP does not seem to use any.
    – ghybs
    Commented May 3, 2020 at 2:15
  • 1
    Fair point, but the OP's problem was solved three and a half years ago. Thought it worth sharing the specific solution I needed! Commented May 3, 2020 at 21:42
2

add these to your index.html

 <link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/leaflet.css"
    integrity="sha512-xodZBNTC5n17Xt2atTPuE1HxjVMSvLVW9ocqUKLsCC5CXdbqCmblAshOMAS6/keqq/sMZMZ19scR4PsZChSR7A=="
    crossorigin=""/>


<script src="https://unpkg.com/[email protected]/dist/leaflet.js"
          integrity="sha512-XQoYMqMTK8LvdxXYG3nZ448hOEQiglfqkJs1NOQV44cWnUrBc8PkAOcXy20w0vlaXaVUearIOBhiXZ5V3ynxwA=="
          crossorigin=""></script>

Not the answer you're looking for? Browse other questions tagged or ask your own question.