Last active
January 21, 2024 18:51
-
-
Save adamb0mb/11131861 to your computer and use it in GitHub Desktop.
Google Apps Script for fetching WalkScore
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/**************** | |
* This is an example of how to pull data from the WalkScore API from Google Spreadsheets | |
* Hastily Written by Adam Phillabaum <adam@phillabaum.us> | |
* | |
* An example invocation looks like: | |
* "=WalkScore(AE2,AF2,C2)" | |
* Where AE2 is latitude, AF2 is longitude, and C2 is the address | |
* | |
* Get your WalkScore API key here: http://www.walkscore.com/professional/api-sign-up.php | |
* Don't worry, it's free and instant... even though it looks like a leadgen form | |
* | |
* TODO: Remove depenecy on Lat/Lng, use the geocoder service to just use the address | |
*/ | |
var WSAPIKEY="aaaaaaaaaa"; | |
var WSAPIBASEURL="http://api.walkscore.com/score?format=json&wsapikey="+WSAPIKEY | |
/** | |
* Takes a URL, and appends the specified URL params onto it | |
*/ | |
function appendParamToUrl(url,key,value){ | |
key = encodeURIComponent(key); | |
value = encodeURIComponent(value); | |
if (url.indexOf("?")<0) | |
return url+"?"+key+"="+value; | |
else | |
return url+"&"+key+"="+value; | |
} | |
/** | |
* Takes an address, a latitude, and a longitude and returns the walkscore | |
*/ | |
function WalkScore(lat, lng, address) { | |
if (typeof(lat) != "number" || typeof(lng) != "number") { | |
throw "lat and lng must both be numbers"; | |
} | |
url = appendParamToUrl(WSAPIBASEURL,"lat",lat) | |
url = appendParamToUrl(url,"lon",lng) | |
url = appendParamToUrl(url,"address",address) | |
var response = UrlFetchApp.fetch(url); | |
var payload = JSON.parse(response.getContentText()) | |
if (payload.status != 1) { | |
throw "Error code from WalkScore: "+payload.status; | |
} | |
return payload.walkscore; | |
} |
This was helpful, thanks. I was using some Google Maps bits already anyway, so I tweaked it to automatically query lat/long from that
To get bike score add url = appendParamToUrl(url,"bike",1)
and then change the return to return payload.bike.score;
Do the same to get transit.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is exactly what I was looking for! I was also able to tweak it to fetch Bike and Transit scores too.