Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

stephen waweru's avatar

leaflet js map not showing fully on page load

on loading a page load my leaflet map is unable to load fully and instead it shows a small section of the map.but when I try to resize the screen the map fully shows as a whole.i don't understand why yet i have initialized the map correctly in the script.this is my codebase where might be doing it wrong.i have added the necessary leaflet js and CSS cdn linksenter image description here this is my HTML

<div class="row" style="margin: 40px 5px;">
    <div class="col-lg-12">
        <div class="ms_heading">
            <h3 class="text-white p-2">properties Locations</h3>
        </div>
        <div id="map"
            style="height: 400px; margin-top:5px;">
        </div>
    </div>
    

this is my script

$(document).ready(function () {

        //1. initialize leaflet map
        var center = [-0.421498116026705, 36.950147769312004];
        var propertiesmap = L.map('map').setView(center, 10);

        propertiesmap.invalidateSize();

        var googleStreets = L.tileLayer('http://{s}.google.com/vt/lyrs=m&x={x}&y={y}&z={z}',{
            maxZoom: 10,
            subdomains:['mt0','mt1','mt2','mt3']
        });

        googleStreets.addTo(propertiesmap)

        var marker = L.marker([-0.421498116026705, 36.950147769312004]).addTo(propertiesmap);
                
    });
0 likes
3 replies
Niush's avatar
Niush
Best Answer
Level 50

First try setting width: 100% to #map. Then make sure when the JavaScript is loaded, the container around the map is loaded (i.e. not hidden, absolutely positioned etc.).

Else you can add this at the end of your JS code.

setTimeout(function () {
   window.dispatchEvent(new Event("resize"));
}, 500);

This will dispatch a window level resize event, which causes leaflet to listen for it and render map properly.

5 likes
JulianR00's avatar

Yo desarrollé un sistema de formulario por pasos, por lo que yo tenia div ocultos cuando no estaban en la sección. El mapa estaba en uno de esos divs ocultos, por lo que no me mostraba el mapa correctamente y era por estilos CSS. Talvez lo corregí de manera algo cutre pero espero te sirva.

Primero, establece una clase a cada div oculto con estas propiedades CSS:

.step { left: -9999px; top: -9999px; height: 0px; }

Lo que hace es ocultarlo sin usar el display: none;

Cuando avances y ahora quieras mostrar el mapa, solo remueve la clase con JS.

Espero te ayude como me ayudo a mi.

==================================

I developed a step form system, so I had hidden divs when they weren't in the section. The map was in one of those hidden divs, so it wasn't showing me the map correctly and it was due to CSS styles. Maybe I corrected it in a somewhat tacky way but I hope it helps you.

First, set a class to each hidden div with these CSS properties: .step { left: -9999px; top: -9999px; height: 0px; } What it does is hide it without using display: none;

When you move forward and now want to show the map, just remove the class with JS.

I hope it helps you like it helped me.

1 like

Please or to participate in this conversation.