Search code examples
leafletjsfiddle

why isn't leaflet demo working on jsfiddle?


https://terrafrost.com/leaflet/demo01.html works as expected when ran it's own but when ran on jsfiddle at https://jsfiddle.net/x2f05eov/ all I get is a blank page. There are no errors that I'm seeing in the console.

Any ideas?

The JS code is as follows:

function layerParams(id)
{
    return {
        maxZoom: 18,
        attribution: 'Map data &copy; <a href="https://www.openstreetmap.org/">OpenStreetMap</a> contributors, ' +
            '<a href="https://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, ' +
            'Imagery © <a href="https://www.mapbox.com/">Mapbox</a>',
        id: id,
        tileSize: 512,
        zoomOffset: -1
    };
}

var mapboxUrl = 'https://api.mapbox.com/styles/v1/{id}/tiles/{z}/{x}/{y}?access_token=pk.eyJ1IjoibWFwYm94IiwiYSI6ImNpejY4NXVycTA2emYycXBndHRqcmZ3N3gifQ.rJcFIG214AriISLbB6B5aw';

var light = L.tileLayer(mapboxUrl, layerParams('mapbox/light-v10'));
var streets = L.tileLayer(mapboxUrl, layerParams('mapbox/streets-v11'));
var satellite = L.tileLayer(mapboxUrl, layerParams('mapbox/satellite-streets-v11'));

var mymap = L.map('mapid', {
    center: [30.267222, -97.743056],
    zoom: 13,
    layers: [light]
});

var baseMaps = {
    "Light": light,
    "Streets" :streets,
    "Satellite": satellite
};

lightLines = L.layerGroup();
streetsLines = L.layerGroup();
satelliteLines = L.layerGroup();

var params = {
    color: 'blue',
    opacity: 0.75,
    smoothFactor: 1
};

var latlngs = [[30.267222, -97.743056],[31.267222, -96.743056]];

tempParams = Object.assign(params, {weight: 10});
satelliteLines.addLayer(L.polyline(latlngs, tempParams));
tempParams = Object.assign(params, {weight: 3});
lightLines.addLayer(L.polyline(latlngs, tempParams));
tempParams = Object.assign(params, {weight: 5});
streetsLines.addLayer(L.polyline(latlngs, tempParams));

var hasOverlay = false;

mymap.on('baselayerchange', function(e) {
    switch (e.name) {
        case 'Light':
            if (hasOverlay) {
                lightLines.addTo(mymap);
                streetsLines.removeFrom(mymap);
                satelliteLines.removeFrom(mymap);
            }
            break;
        case 'Streets':
            if (hasOverlay) {
                lightLines.removeFrom(mymap);
                streetsLines.addTo(mymap);
                satelliteLines.removeFrom(mymap);
            }
            break;
        case 'Satellite':
            if (hasOverlay) {
                lightLines.removeFrom(mymap);
                streetsLines.removeFrom(mymap);
                satelliteLines.addTo(mymap);
            }
    }
});

mymap.on('overlayadd', function(e) {
    console.log('overlayadd called');
    hasOverlay = true;
});

mymap.on('overlayremove', function(e) {
    console.log('overlayremove called');
    lightLines.removeFrom(mymap);
    streetsLines.removeFrom(mymap);
    satelliteLines.removeFrom(mymap);
});

var overlays = {
    'Demo': lightLines
};

L.control.layers(baseMaps, overlays).addTo(mymap);

The HTML code is as follows:

<body style="margin: 0; padding: 0; height: 100%">
<div id="mapid" style="width: 100%; height: 100%"></div>

The leaflet CSS and JS are being included as well.


Solution

  • You just miss a closing body and html tag

    <!DOCTYPE html>
    <html style="height: 100%">
    
    <head>
      <link rel="stylesheet" href="https://unpkg.com/leaflet@1.7.1/dist/leaflet.css" integrity="sha512-xodZBNTC5n17Xt2atTPuE1HxjVMSvLVW9ocqUKLsCC5CXdbqCmblAshOMAS6/keqq/sMZMZ19scR4PsZChSR7A==" crossorigin="" />
      <script src="https://unpkg.com/leaflet@1.7.1/dist/leaflet.js" integrity="sha512-XQoYMqMTK8LvdxXYG3nZ448hOEQiglfqkJs1NOQV44cWnUrBc8PkAOcXy20w0vlaXaVUearIOBhiXZ5V3ynxwA==" crossorigin=""></script>
    </head>
    
    <body style="margin: 0; padding: 0; height: 100%">
      <div id="mapid" style="width: 100%; height: 100%"></div>
      <script>
        function layerParams(id) {
          return {
            maxZoom: 18,
            attribution: 'Map data &copy; <a href="https://www.openstreetmap.org/">OpenStreetMap</a> contributors, ' +
              '<a href="https://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, ' +
              'Imagery © <a href="https://www.mapbox.com/">Mapbox</a>',
            id: id,
            tileSize: 512,
            zoomOffset: -1
          };
        }
    
        var mapboxUrl = 'https://api.mapbox.com/styles/v1/{id}/tiles/{z}/{x}/{y}?access_token=pk.eyJ1IjoibWFwYm94IiwiYSI6ImNpejY4NXVycTA2emYycXBndHRqcmZ3N3gifQ.rJcFIG214AriISLbB6B5aw';
    
        var light = L.tileLayer(mapboxUrl, layerParams('mapbox/light-v10'));
        var streets = L.tileLayer(mapboxUrl, layerParams('mapbox/streets-v11'));
        var satellite = L.tileLayer(mapboxUrl, layerParams('mapbox/satellite-streets-v11'));
    
        var mymap = L.map('mapid', {
          center: [30.267222, -97.743056],
          zoom: 13,
          layers: [light]
        });
    
        var baseMaps = {
          "Light": light,
          "Streets": streets,
          "Satellite": satellite
        };
    
        lightLines = L.layerGroup();
        streetsLines = L.layerGroup();
        satelliteLines = L.layerGroup();
    
        var params = {
          color: 'blue',
          opacity: 0.75,
          smoothFactor: 1
        };
    
        var latlngs = [
          [30.267222, -97.743056],
          [31.267222, -96.743056]
        ];
    
        tempParams = Object.assign(params, {
          weight: 10
        });
        satelliteLines.addLayer(L.polyline(latlngs, tempParams));
        tempParams = Object.assign(params, {
          weight: 3
        });
        lightLines.addLayer(L.polyline(latlngs, tempParams));
        tempParams = Object.assign(params, {
          weight: 5
        });
        streetsLines.addLayer(L.polyline(latlngs, tempParams));
    
        var hasOverlay = false;
    
        mymap.on('baselayerchange', function(e) {
          switch (e.name) {
            case 'Light':
              if (hasOverlay) {
                lightLines.addTo(mymap);
                streetsLines.removeFrom(mymap);
                satelliteLines.removeFrom(mymap);
              }
              break;
            case 'Streets':
              if (hasOverlay) {
                lightLines.removeFrom(mymap);
                streetsLines.addTo(mymap);
                satelliteLines.removeFrom(mymap);
              }
              break;
            case 'Satellite':
              if (hasOverlay) {
                lightLines.removeFrom(mymap);
                streetsLines.removeFrom(mymap);
                satelliteLines.addTo(mymap);
              }
          }
        });
    
        mymap.on('overlayadd', function(e) {
          console.log('overlayadd called');
          hasOverlay = true;
        });
    
        mymap.on('overlayremove', function(e) {
          console.log('overlayremove called');
          lightLines.removeFrom(mymap);
          streetsLines.removeFrom(mymap);
          satelliteLines.removeFrom(mymap);
        });
    
        var overlays = {
          'Demo': lightLines
        };
    
        L.control.layers(baseMaps, overlays).addTo(mymap);
      </script>
    
    </body>
    
    </html>

    Edit: you need to define explicit height (f.i 100vh) because setting 100% height means the height of the parent div which does not exist in this case fiddle