JSFiddle - React, Tailwind, and code Playground
by alrick
HTML
<link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet-0.6.2/leaflet.css">
<script src="http://cdn.leafletjs.com/leaflet-0.6.2/leaflet.js"></script>
<script src="http://leafletjs.com/examples/us-states.js"></script>
<div id="map"></div>
CSS
#map { height: 600px; }
JavaScript
////////////////////
// FUNCTIONS
////////////////////
// Highlight styling
function highlightFeature(e) {
var layer = e.target;
layer.setStyle({
weight: 5,
color: '#666',
dashArray: '',
fillOpacity: 0.7
});
if (!L.Browser.ie && !L.Browser.opera) {
layer.bringToFront();
}
}
// Reset styling (mouseout)
function resetHighlight(e) {
geojson.resetStyle(e.target);
}
// Default styling
function style(feature) {
return {
fillColor: "#f9c47c",
weight: 2,
opacity: 1,
color: "white",
dashArray: "3",
fillOpacity: 0.7
};
}
// Called when feature clicked
function sayClicked() {
alert("clicked");
}
// Add listener to features
function onEachFeature(feature, layer) {
layer.on({
mouseover: highlightFeature,
mouseout: resetHighlight,
click: sayClicked,
});
}
////////////////////
// FLOW
////////////////////
// Init map
var map = L.map('map').setView([46.961511,8.481445], 1);
// Setup tile layer bg
L.tileLayer("http://{s}.tile.cloudmade.com/{key}/{styleId}/256/{z}/{x}/{y}.png", {
key: "c692bacd28ce471f820690c73a71d75b",
styleId: 22677
}).addTo(map);
// Load geoJSON about Kantons
var url = "http://api.geo.admin.ch/main/wsgi/feature/search?layers=ch.swisstopo.swissboundaries3d-kanton-flaeche.fill&bbox=592725%2C209304.998016%2C595975%2C212554.998016";
var req = $.getJSON(url);
var kantons;
req.done(function(response) { kantons = response; });
var geojson;
geojson = L.geoJson(statesData, {
style: style,
onEachFeature: onEachFeature
}).addTo(map);