A Pen by Beau Cronin on CodePen.
Created
November 9, 2019 05:24
-
-
Save beaucronin/e91f9ff0a332a081e3e070d6a7da0563 to your computer and use it in GitHub Desktop.
Leaflet GeoJSON Prototype
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
<div id="mapid"></div> |
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
var mymap = L.map('mapid').setView([37.85, -122.25], 13); | |
mymap.on('moveend', function(ev) { | |
reloadLayers(); | |
}) | |
var baseLayer = L.tileLayer( | |
'https://api.tiles.mapbox.com/v4/{id}/{z}/{x}/{y}.png?access_token={accessToken}', | |
{ | |
attribution: 'Map data © <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>', | |
maxZoom: 18, | |
id: 'mapbox.dark', | |
accessToken: 'pk.eyJ1IjoiYmVhdWNyb25pbiIsImEiOiJjazJxcnkyZ3EwaTliM210b2l6dDE5MnppIn0.K9J-D-Z1UStC_BV75SyPNA' | |
}).addTo(mymap); | |
var baseLayers = { 'main': baseLayer }; | |
var layerNames = [ | |
'Airport', 'Building', 'Church', 'County', 'Hospital', 'Lake', 'Populated-Place', 'Post-Office', 'School' | |
] | |
var overLayers = {} | |
layerNames.forEach(function(layerName) { | |
overLayers[layerName] = L.geoJSON(); | |
}) | |
L.control.layers({}, overLayers).addTo(mymap); | |
var reloadLayers = function() { | |
var c = mymap.getCenter(); | |
layerNames.forEach(function(layerName) { | |
fetch( | |
'https://api.str8d8a.info/v1/gnis/'+layerName+'/around/'+c.lat.toString()+'/'+c.lng.toString()+'/4', | |
{ | |
mode: 'cors', | |
headers: { 'X-API-Key': 'Uui6dHqgqxauKmXyrk70p6AbmzTG9hM57Ys47dg1' } | |
} | |
) | |
.then(response => response.json()) | |
.then(function(data) { | |
console.log(data); | |
overLayers[layerName] | |
.clearLayers() | |
.addData(data) | |
.bindPopup(function(x) { | |
return x.feature.properties.name; | |
}); | |
}); | |
}); | |
} | |
reloadLayers(); |
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
#mapid { | |
height: 480px; | |
width: 1000px; | |
} | |
body { | |
color: black; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment