Importing data from the US Census API

The US Census Bureau provides an API for querying data in a number of ways. This post will not describe the Census API, other than to say that the data is returned in JSON format. We use the state ID, provided in the 2nd column, to look up the existing state data (using the lookupId method of google.maps.Data) and update with the census data (using the setProperty method of google.maps.Data)





Styling the data

Data can be styled through the use of a Data.StyleOptions object or through a function that returns a Data.StyleOptions object. Here we create a choropleth map by applying a gradient to each polygon in the dataset based on the value in the census data.





In addition to the coloring, we've created an interactive element by adding events that respond to mouse activity. When you hover your mouse cursor (or finger) over a region with data, the border becomes heavier and the data card is updated with the selected value.





We’ve also used a custom basemap style in this example to provide some contrast to the colorful data.





Check out Google Maps Engine if you need somewhere to store your geospatial data in the cloud, as we’ve done here. If you have any questions on using these features, check out the docs for the data layer and the Maps Engine API or head over to Stack Overflow and ask there. You can also check out this article’s permanent home, where the interactive version lives.



Posted by Mark McDonald, Google Developer Relations Team