Eli Kern
Eli Kern

Reputation: 319

Leaflet choropleth map - polygon colors linked to spreadsheet?

I'm new to using Leaflet after having made some interactive choropleth maps using Mapbox's TileMill. If I add a custom polygon layer through using geoJSON, is there any way for me to symbolize (i.e. color) polygons based on values in a linked spreadsheet? What I'm trying to do is find a way to create an online interactive choropleth that can be automatically updated with current data just by making changes to a spreadsheet (i.e. Google doc) rather than adding a new geoJSON layer. Thank you for your help!

Upvotes: 2

Views: 678

Answers (2)

geraldarthur
geraldarthur

Reputation: 1524

You can request your Google Spreadsheet data as JSON and then parse the data on the client-side, updating the data inside your geoJSON. Here's some more documentation via Google. Basically you'd call the spreadsheet, pass the spreadsheet data as JSON to a callback function, then have that function update your geoJSON with the new values. The basic API looks like this, where feed is the type of feed (either list or cell) you'd want and key is your Google Spreadsheet's key.

http://spreadsheets.google.com/feeds/feed/key/worksheet/public/basic?alt=json-in-script&callback=myFunc

Upvotes: 1

Bill Morris
Bill Morris

Reputation: 589

It seems like you might have two different Qs there, and since I can't comment to ask for clarification, here are answers to both:

  1. If you're referring to updating colors in Tilemill, then (the title of your question shouldn't refer to Leaflet, and) there's a pretty good workflow here for using a google spreadsheet as a Tilemill data source. Unfortunately though, fill colors are not data-driven properties, so you'll still need to assign colors conditionally in CartoCSS.
  2. If you're referring to updating a thematic layer in the Leaflet client, then there are two options:
    • Write a script to populate this function and this line with values brought in from your google spreadsheet endpoint.
    • Use CartoDB. It does pretty much all of what your referring to, with live changes on a leaflet map as you update the table and CartoCSS styles. You can also synchronize a CartoDB table with a google spreadsheet.

Upvotes: 0

Related Questions