Search code examples
javascriptleafletopenstreetmap

How to display the map scale in leaflet.js


I wonder how to display the scale map in leafleat.js. That is: I want this enter image description here instead of this enter image description here (taken to leaflet tutorial). See the left corner of the picture.


Solution

  • You can take use of L.control.scale():

    var map = L.map('map').setView([51.505, -0.09], 13);
    
    L.tileLayer('https://tile.openstreetmap.org/{z}/{x}/{y}.png', {
        attribution: '&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
    }).addTo(map);
    
    L.control.scale().addTo(map);
    <link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/leaflet.css" integrity="sha256-kLaT2GOSpHechhsozzB+flnD+zUyjE2LlfWPgU04xyI=" crossorigin="" />
    <script src="https://unpkg.com/[email protected]/dist/leaflet.js" integrity="sha256-WBkoXOwTeyKclOHuWtc+i2uENFpDZ9YPdf5Hf+D7ewM=" crossorigin=""></script>
    
    <div id="map" style="width: 100%; height: 150px;"></div>