Created
March 1, 2019 23:13
-
-
Save ericrobskyhuntley/f999414064d06f8bd01a1467c37cf358 to your computer and use it in GitHub Desktop.
Linking Leaflet Layers by ID
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
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="utf-8"> | |
<title>Linking Leaflet Layers by ID</title> | |
<style> | |
#map {height: 850px;} | |
</style> | |
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.4.0/dist/leaflet.css" /> | |
<!-- Must come after leaflet.css --> | |
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> | |
<script src="https://unpkg.com/leaflet@1.4.0/dist/leaflet.js" type="text/javascript"></script> | |
</head> | |
<body> | |
<div id="map"></div> | |
<script src="map.js" type="text/javascript"></script> | |
</body> | |
</html> |
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 animalMap = L.map('map').setView( | |
center = [42.360724, -71.068711], | |
zoom = 16); | |
var tonerLite = L.tileLayer('https://stamen-tiles-{s}.a.ssl.fastly.net/toner-lite/{z}/{x}/{y}.{ext}', { | |
attribution: 'Map tiles by <a href="http://stamen.com">Stamen Design</a>, <a href="http://creativecommons.org/licenses/by/3.0">CC BY 3.0</a> — Map data © <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>', | |
subdomains: 'abcd', | |
minZoom: 0, | |
maxZoom: 20, | |
ext: 'png' | |
}); | |
tonerLite.addTo(animalMap); | |
var bufferGroup = L.layerGroup().addTo(animalMap); | |
$.getJSON("test_points.geojson", function(data) { | |
var point = L.geoJSON(data, { | |
onEachFeature: function(feature, layer) { | |
var props = feature.properties; | |
layer.on('click', function() { | |
addBuffer(props.CASE_ENQUIRY_ID); | |
}); | |
} | |
}).addTo(animalMap); | |
}); | |
function addBuffer(id) { | |
bufferGroup.clearLayers(); | |
$.getJSON("test_1p5km.geojson", function(data) { | |
L.geoJSON(data, { | |
filter: function(feature, layer) { | |
return Math.round(feature.properties.CASE_ENQUI) == id; | |
} | |
}).addTo(bufferGroup); | |
}) | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment