JSFiddle - React, Tailwind, and code Playground
by Alex Azuero
HTML
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/leaflet/0.7.3/leaflet.css" data-require="[email protected]" data-semver="0.7.3" />
<link rel="stylesheet" href="style.css" />
</head>
<body>
<div id="map"></div>
<script type="text/javascript" src="//code.jquery.com/jquery-2.1.3.min.js"></script>
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/leaflet/0.7.3/leaflet.js" data-require="[email protected]" data-semver="0.7.3"></script>
<script type="text/javascript" src="script.js"></script>
</body>
</html>
CSS
body {
margin: 0;
}
html, body, #map {
height: 100%;
}
JavaScript
var map = L.map('map', {
'center': [0, 0],
'zoom': 1,
'layers': [
L.tileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
'attribution': 'Map data © OpenStreetMap contributors'
})
]
});
var url = 'https://rawgit.com/johan/world.geo.json/master/countries.geo.json';
var geojson, source, selected;
$.getJSON(url, function (collection) {
source = collection;
geojson = L.geoJson(collection, {
'onEachFeature': function (feature, layer) {
layer.on('click', function () {
selected = true;
geojson.clearLayers();
geojson.addData(feature);
map.fitBounds(layer.getBounds());
});
}
}).addTo(map);
});
map.on('click', function () {
if (selected) {
geojson.clearLayers();
geojson.addData(source);
map.fitBounds(geojson.getBounds());
}
});