|
<!DOCTYPE html> |
|
<html> |
|
<head> |
|
<title>Example of using Census Reporter GeoJSON tiles on your own maps.</title> |
|
</head> |
|
<body> |
|
<select id="sumlev-picker"> |
|
<option> -- pick a summary level -- </option> |
|
</select> |
|
<div id="map" style="width: 100%; height: 100%;"></div> |
|
<link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet-0.7.3/leaflet.css" /> |
|
<script src="//cdn.jsdelivr.net/g/[email protected],[email protected],underscorejs"></script> |
|
<script src="http://censusreporter.org/static/js/TileLayer.GeoJSON.js"></script> |
|
<script type="text/javascript"> |
|
// make it big |
|
|
|
var sumlevs = [ |
|
{ 'level': '020', 'name': 'region', zoom: 4 }, |
|
{ 'level': '030', 'name': 'division', zoom: 4 }, |
|
{ 'level': '040', 'name': 'state', zoom: 4 }, |
|
{ 'level': '050', 'name': 'county', zoom: 7 }, |
|
{ 'level': '060', 'name': 'county subdivision', zoom: 7 }, |
|
{ 'level': '140', 'name': 'census tract', zoom: 10 }, |
|
{ 'level': '150', 'name': 'block group', zoom: 15 }, |
|
{ 'level': '160', 'name': 'place', zoom: 9 }, |
|
{ 'level': '170', 'name': 'consolidated city' }, |
|
{ 'level': '230', 'name': 'Alaska native regional corporation' }, |
|
{ 'level': '250', 'name': 'native area', zoom: 4 }, |
|
{ 'level': '251', 'name': 'tribal subdivision', zoom: 4 }, |
|
{ 'level': '256', 'name': 'tribal tract', zoom: 4 }, |
|
{ 'level': '310', 'name': 'metro (CBSA) area', zoom: 7 }, |
|
{ 'level': '314', 'name': 'metropolitan division', zoom: 4 }, |
|
{ 'level': '330', 'name': 'combined statistical area', zoom: 4 }, |
|
{ 'level': '335', 'name': 'combined NECTA', zoom: 4 }, |
|
{ 'level': '350', 'name': 'NECTA', zoom: 4 }, |
|
{ 'level': '364', 'name': 'NECTA division', zoom: 4 }, |
|
{ 'level': '400', 'name': 'urban area', zoom: 7 }, |
|
{ 'level': '500', 'name': 'congressional district', zoom: 7 }, |
|
{ 'level': '610', 'name': 'state house (upper)', zoom: 7 }, |
|
{ 'level': '620', 'name': 'state house (lower)', zoom: 7 }, |
|
{ 'level': '795', 'name': 'PUMA', zoom: 8 }, |
|
{ 'level': '860', 'name': 'ZIP code', zoom: 12 }, |
|
{ 'level': '950', 'name': 'school district (elementary)', zoom: 10 }, |
|
{ 'level': '960', 'name': 'school district (secondary)', zoom: 10 }, |
|
{ 'level': '970', 'name': 'school district (unified)', zoom: 10 } |
|
] |
|
|
|
var div = document.getElementById("map"); |
|
div.style.height = (window.innerHeight - 100) + "px"; |
|
|
|
var baseLayer = function() { |
|
return L.tileLayer('http://a.tile.openstreetmap.org/{z}/{x}/{y}.png', { |
|
attribution: { 'name': 'Map data © <a href="http://openstreetmap.org">OpenStreetMap</a> contributors, <a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, Imagery © <a href="http://mapbox.com">Mapbox</a>' }, |
|
maxZoom: 18 |
|
}); |
|
} |
|
|
|
// pretty basic Leaflet here... |
|
var map = L.map("map") |
|
map.setView([39.833333, -98.583333], 5); |
|
map.addLayer(baseLayer()); |
|
|
|
|
|
var defaultStyle = { |
|
"clickable": true, |
|
"color": "#00d", |
|
"fillColor": "#ccc", |
|
"weight": 1.0, |
|
"opacity": 0.3, |
|
"fillOpacity": 0.3, |
|
}; |
|
|
|
|
|
// more about sumlevs at http://censusreporter.org/glossary/#term-summary-level |
|
var defaultSumlev = '040'; |
|
var makeLayer = function(sumlev) { |
|
var tilesGeoAPI = 'http://embed.censusreporter.org/1.0/geo/tiger2013/tiles/' + sumlev + '/{z}/{x}/{y}.geojson'; |
|
|
|
var geojsonTileLayer = new L.TileLayer.GeoJSON(tilesGeoAPI, { |
|
clipTiles: true, |
|
unique: function(feature) { |
|
return feature.properties.geoid; |
|
} |
|
}, { |
|
style: defaultStyle, |
|
onEachFeature: function(feature, layer) { |
|
// you can wire behavior to each "feature", or place outline. |
|
var profileURL = 'http://censusreporter.org/profiles/' + feature.properties.geoid; |
|
layer.bindPopup("<a href='" + profileURL + "'>" + feature.properties.name + "</a>"); |
|
layer.on('mouseover', function() { |
|
layer.setStyle({ |
|
"fillOpacity": 0.5, |
|
}); |
|
}); |
|
layer.on('mouseout', function() { |
|
layer.setStyle(defaultStyle); |
|
}); |
|
} |
|
}); |
|
|
|
return geojsonTileLayer; |
|
} |
|
|
|
_.each(sumlevs,function(l) { |
|
$('<option>').val(l.level).text(l.level + " - " + l.name).appendTo('#sumlev-picker'); |
|
}); |
|
|
|
|
|
$("#sumlev-picker").change(function(e) { |
|
var sumlev = _.findWhere(sumlevs,{level: $(e.target).val()}) |
|
if (sumlev) { |
|
if (sumlev.zoom) { |
|
map.setZoom(sumlev.zoom); |
|
} |
|
if (typeof sumlev.layer == 'undefined') { |
|
sumlev.layer = makeLayer(sumlev.level); |
|
} |
|
_.each(sumlevs,function(sl) { |
|
if (sl.layer && map.hasLayer(sl.layer)) { |
|
map.removeLayer(sl.layer); |
|
} |
|
}) |
|
map.addLayer(sumlev.layer); |
|
} |
|
}); |
|
|
|
|
|
|
|
</script> |
|
</body> |
|
</html> |