Last active
July 26, 2019 05:44
-
-
Save chingchai/51c6962a17ce56312251dbe8b087ee82 to your computer and use it in GitHub Desktop.
Leaflet with GeoJSON
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<title>Leaflet WebMap</title> | |
<link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/leaflet.css" | |
integrity="sha512-xwE/Az9zrjBIphAcBb3F6JVqxf46+CDLwfLMHloNu6KEQCAWi6HcDUbeOfBIptF7tcCzusKFjFw2yuvEpDL9wQ==" | |
crossorigin="" /> | |
<!-- Make sure you put this AFTER Leaflet's CSS --> | |
<script src="https://unpkg.com/[email protected]/dist/leaflet.js" | |
integrity="sha512-GffPMF3RvMeYyc1LWMHtK8EbPv0iNZ8/oTtHPx9/cc2ILxQ+u905qIwdpULaqDkyBKgOaB57QTMg7ztg8Jm2Og==" | |
crossorigin=""></script> | |
<!-- jquery --> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script> | |
<style> | |
#map{ height: 100% } | |
</style> | |
</head> | |
<body> | |
<div id="map"></div> | |
<script> | |
// initialize the map | |
var map = L.map('map').setView([16, 100], 3); | |
// load a tile layer | |
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', { | |
maxZoom: 19, | |
attribution: '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors' | |
}).addTo(map); | |
// load GeoJSON from an external file | |
// $.getJSON("countries.geojson",function(data){ | |
// // add GeoJSON layer to the map once the file is loaded | |
// L.geoJson(data).addTo(map); | |
// }); | |
function addDataToMap(data, map) { | |
var dataLayer = L.geoJson(data, { | |
onEachFeature: function (feature, layer) { | |
var popupText = "NAME: " + feature.properties.NAME | |
+ "<br>REGION: " + feature.properties.REGION_UN; | |
//+ "<br><a href='" + feature.properties.url + "'>More info</a>"; | |
layer.bindPopup(popupText); | |
} | |
}); | |
dataLayer.addTo(map); | |
} | |
$.getJSON("countries.geojson", function (data) { addDataToMap(data, map); }); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment