More ways to use Census boundaries on your slippy map
A few weeks ago we released a simple feature to add Census boundaries to your “slippy maps.” Today we’re happy to share some more tricks of this nature.
The earlier feature only gave you all shapes of a given summary level. Now you can be more selective—introducing CensusReporter.GeoIDLayer
As before, this code depends upon LeafletJS.. In fact, we’ve renamed the javascript library to make that clear. The old one is still there, but if by chance you’re already using it, you should update the javascript src url.
To use this, first load LeafletJS and then our library on your page:
<script src="http://censusreporter.org/static/js/cr-leaflet.js"></script>Then, assuming your map is called map, this is all you need to do to add the boundary of Illinois to it:
new CensusReporter.GeoIDLayer('04000US17').addTo(map);
Under the hood, the GeoIDLayer uses our /geo/show JSON api, so you can pass any arguments that understands. Specifically, you can pass in a comma-separated string of geoids, and you can also use our “containment” syntax to get all geographies of a certain kind within a given parent. (See below for an example)
GeoIDLayer extends L.GeoJSON, so you can pass an options object as the second argument to control style and interaction. By default, clicking on layers added using this layer will open the appropriate Census Reporter “profile page” for the geography. You can disable this by setting the autoclick option to false.
Here’s an example which shows how to add all states in the Midwest division to the map, with a popup which shows their names:
var layer = new CensusReporter.GeoIDLayer('040|02000US2',{
autoclick: false,
onEachFeature: function(data,layer) {
layer.bindPopup(data.properties.name);
}
}).addTo(map);
Once you’ve created the layer, you can add and remove more geoids easily:
layer.addGeoID('04000US06'); // add California
var illinois = layer.removeGeoID('04000US17'); // remove Illinois
Because a L.GeoJSON object is really a layer group, calling removeGeoID can return the shape you’re removing as a Leaflet layer. You could add it back later with:
layer.addLayer(illinois); // restore Illinois
This isn’t something we actually use directly in CensusReporter, so we welcome your feedback about whether it’s useful, or what could make it better. Even more, we welcome your pull requests!












