Skip to content

Instantly share code, notes, and snippets.

@morganherlocker
Created April 24, 2015 20:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save morganherlocker/5ed4473c12c3d4f02670 to your computer and use it in GitHub Desktop.
Save morganherlocker/5ed4473c12c3d4f02670 to your computer and use it in GitHub Desktop.
click filter
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>A simple map</title>
<meta name='viewport' content='initial-scale=1,maximum-scale=1,user-scalable=no' />
<script src='https://api.tiles.mapbox.com/mapbox.js/v2.1.8/mapbox.js'></script>
<script src='https://api.tiles.mapbox.com/mapbox.js/plugins/turf/v2.0.0/turf.min.js'></script>>
<link href='https://api.tiles.mapbox.com/mapbox.js/v2.1.8/mapbox.css' rel='stylesheet' />
<style>
body { margin:0; padding:0; }
#map { position:absolute; top:0; bottom:0; width:100%; }
</style>
</head>
<body>
<div id='map'></div>
<script>
L.mapbox.accessToken = 'pk.eyJ1IjoibW9yZ2FuaGVybG9ja2VyIiwiYSI6Ii1zLU4xOWMifQ.FubD68OEerk74AYCLduMZQ';
var map = L.mapbox.map('map', 'examples.map-i86nkdio')
.setView([34.7363,-119.7777], 9);
var polys = {
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {
"name": "Building A"
},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
-119.94873046875,
35.110921809704756
],
[
-120.26458740234375,
35.043489514314686
],
[
-120.03662109374999,
34.93323032184195
],
[
-119.94873046875,
35.110921809704756
]
]
]
}
},
{
"type": "Feature",
"properties": {
"name": "Building B"
},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
-119.30603027343749,
34.89494244739732
],
[
-119.5037841796875,
34.78222760653013
],
[
-119.2236328125,
34.70549341022547
],
[
-119.30603027343749,
34.89494244739732
]
]
]
}
},
{
"type": "Feature",
"properties": {
"name": "Building C"
},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
-120.003662109375,
34.646766246519114
],
[
-120.22613525390624,
34.52692430140103
],
[
-119.91577148437499,
34.334364487026306
],
[
-120.003662109375,
34.646766246519114
]
]
]
}
}
]
}
var pts = {
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {
"building": "Building A",
"marker-color": "#f00"
},
"geometry": {
"type": "Point",
"coordinates": [
-120.06134033203125,
35.081707990840705
]
}
},
{
"type": "Feature",
"properties": {
"building": "Building B",
"marker-color": "#f00"
},
"geometry": {
"type": "Point",
"coordinates": [
-119.15496826171875,
34.62868797377061
]
}
},
{
"type": "Feature",
"properties": {
"building": "Building A",
"marker-color": "#f00"
},
"geometry": {
"type": "Point",
"coordinates": [
-120.17669677734374,
35.023248960913385
]
}
},
{
"type": "Feature",
"properties": {
"building": "Building C",
"marker-color": "#f00"
},
"geometry": {
"type": "Point",
"coordinates": [
-120.10391235351564,
34.475995767558665
]
}
},
{
"type": "Feature",
"properties": {
"building": "Building B",
"marker-color": "#f00"
},
"geometry": {
"type": "Point",
"coordinates": [
-119.35546875000001,
34.829586636768205
]
}
},
{
"type": "Feature",
"properties": {
"building": "Building C",
"marker-color": "#f00"
},
"geometry": {
"type": "Point",
"coordinates": [
-119.93499755859375,
34.46354183877718
]
}
}
]
}
var polyLayer = L.geoJson(polys).addTo(map);
var ptsLayer = L.mapbox.featureLayer().addTo(map);
polyLayer.on('click', function(e) {
var matchingPoints = turf.featurecollection([])
matchingPoints.features = pts.features.filter(function(pt) {
if(pt.properties.building === e.layer.feature.properties.name) return true
})
ptsLayer.setGeoJSON(matchingPoints);
});
function getPoints(building, pts) {
var matchingPoints = turf.featurecollection([])
matchingPoints.features = pts.features.filter(function(pt) {
if(pt.properties.building === building.properties.name) return true
})
return matchingPoints
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment