block by almccon 5ec33135bea51edfe17c32f1331c5182

Leaflet styling and interaction

Full Screen

Basic mouse interactivity with Leaflet

Built with blockbuilder.org

forked from enjalot‘s block: WWSD #2-b: Leaflet Choropleth

index.html

<!DOCTYPE html>
<head>
  <meta charset="utf-8">
  <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js"></script>
  <link rel="stylesheet" href="//cdn.leafletjs.com/leaflet/v0.7.7/leaflet.css" />
   <script src="//cdn.leafletjs.com/leaflet/v0.7.7/leaflet.js"></script>
  
  <style>
    body { margin:0;position:fixed;top:0;right:0;bottom:0;left:0; }
    #map {
      height: 100%;
    }
  </style>
</head>

<body>
  <div id="map"></div>
  <script>
    var map = L.map('map').setView([30, 0], 2);

    L.tileLayer('//{s}.tile.stamen.com/toner/{z}/{x}/{y}.png', {
        attribution: 'Map tiles by <a href="//stamen.com">Stamen Design</a>, under <a href="//creativecommons.org/licenses/by/3.0">CC BY 3.0</a>. Data  &copy; <a href="//openstreetmap.org">OpenStreetMap</a>, under <a href="//www.openstreetmap.org/copyright">ODbL</a>'
    }).addTo(map);
    
    var url = "https://stamen.github.io/spatial-dataviz-for-data-scientists/data/world/ne_50m_admin_0_countries.geojson"
    d3.json(url, function(err, data) {
      
      function highlightFeature(e) {
        var layer = e.target;

        layer.setStyle({
          weight: 5,
          color: '#666',
          fillOpacity: 0.7
        });
      }
      function resetHighlight(e) {
        geojsonLayer.resetStyle(e.target);
      }
      function zoomToFeature(e) {
 		    map.fitBounds(e.target.getBounds());
      }
      function onEachFeature(feature, layer) {
        layer.on({
          mouseover: highlightFeature,
          mouseout: resetHighlight,
          click: zoomToFeature
        });
      }

      var geojsonLayer = L.geoJson(data, { 
        style: function(feature) {
          return { 
            fillColor: 'lightgreen', 
            fillOpacity:0.4, 
            color: 'darkgreen',
            weight: 2,
      		}
        },
        onEachFeature: onEachFeature
      }).addTo(map)
    })
  </script>
</body>