Skip to content

Instantly share code, notes, and snippets.

@milafrerichs
Created August 8, 2017 17:35
Show Gist options
  • Save milafrerichs/b2073e7f5a4b33d2af23ede7ab57af02 to your computer and use it in GitHub Desktop.
Save milafrerichs/b2073e7f5a4b33d2af23ede7ab57af02 to your computer and use it in GitHub Desktop.
JS Bin Map with changing detail via buttons // source http://jsbin.com/wicozeq
<!DOCTYPE html>
<html>
<head>
<meta name="description" content="Map with changing detail via buttons">
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.2.3/d3.min.js"></script>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
<style id="jsbin-css">
path
fill: #ccc
</style>
</head>
<body>
<div id="map" style="width: 600px; height: 600px;">
<svg width="600" height="600">
<g>
</g>
</svg>
<button id="show-block">Block</button>
<button id="show-neighborhood">Neighborhood</button>
</div>
<script id="jsbin-javascript">
var projection = d3.geoAlbers();
var geoGenerator = d3.geoPath()
.projection(projection);
var map = d3.select('#map g')
neighborhoods = 'https://raw.githubusercontent.com/codeforamerica/click_that_hood/master/public/data/boston.geojson';
blocks = 'https://gist.githubusercontent.com/milafrerichs/12fbfb500be19a41f39a581c8285ecb0/raw/a0734d28b47d48a06ca917529369cb35d232602b/residential_and_non_residential_building_.geojson';
function drawMap(data) {
projection
.fitExtent([[20, 20], [600, 600]], data)
u = map.selectAll('path')
.data(data.features);
u.enter()
.append('path')
.merge(u)
.attr('d', geoGenerator);
u.exit().remove()
}
var q = d3.queue();
q.defer(d3.json,neighborhoods);
q.defer(d3.json,blocks)
.awaitAll(function(error, results) {
blockData = results[1];
neighborhoodData = results[0];
drawMap(blockData)
d3.select('#show-block').on('click', function() { drawMap(blockData);})
d3.select('#show-neighborhood').on('click', function() { drawMap(neighborhoodData);})
})
</script>
<script id="jsbin-source-css" type="text/css">path
fill: #ccc</script>
<script id="jsbin-source-javascript" type="text/javascript">var projection = d3.geoAlbers();
var geoGenerator = d3.geoPath()
.projection(projection);
var map = d3.select('#map g')
neighborhoods = 'https://raw.githubusercontent.com/codeforamerica/click_that_hood/master/public/data/boston.geojson';
blocks = 'https://gist.githubusercontent.com/milafrerichs/12fbfb500be19a41f39a581c8285ecb0/raw/a0734d28b47d48a06ca917529369cb35d232602b/residential_and_non_residential_building_.geojson';
function drawMap(data) {
projection
.fitExtent([[20, 20], [600, 600]], data)
u = map.selectAll('path')
.data(data.features);
u.enter()
.append('path')
.merge(u)
.attr('d', geoGenerator);
u.exit().remove()
}
var q = d3.queue();
q.defer(d3.json,neighborhoods);
q.defer(d3.json,blocks)
.awaitAll(function(error, results) {
blockData = results[1];
neighborhoodData = results[0];
drawMap(blockData)
d3.select('#show-block').on('click', function() { drawMap(blockData);})
d3.select('#show-neighborhood').on('click', function() { drawMap(neighborhoodData);})
})</script></body>
</html>
var projection = d3.geoAlbers();
var geoGenerator = d3.geoPath()
.projection(projection);
var map = d3.select('#map g')
neighborhoods = 'https://raw.githubusercontent.com/codeforamerica/click_that_hood/master/public/data/boston.geojson';
blocks = 'https://gist.githubusercontent.com/milafrerichs/12fbfb500be19a41f39a581c8285ecb0/raw/a0734d28b47d48a06ca917529369cb35d232602b/residential_and_non_residential_building_.geojson';
function drawMap(data) {
projection
.fitExtent([[20, 20], [600, 600]], data)
u = map.selectAll('path')
.data(data.features);
u.enter()
.append('path')
.merge(u)
.attr('d', geoGenerator);
u.exit().remove()
}
var q = d3.queue();
q.defer(d3.json,neighborhoods);
q.defer(d3.json,blocks)
.awaitAll(function(error, results) {
blockData = results[1];
neighborhoodData = results[0];
drawMap(blockData)
d3.select('#show-block').on('click', function() { drawMap(blockData);})
d3.select('#show-neighborhood').on('click', function() { drawMap(neighborhoodData);})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment