Skip to content

Instantly share code, notes, and snippets.

@adamb0mb
Last active January 21, 2024 18:51
Show Gist options
  • Save adamb0mb/11131861 to your computer and use it in GitHub Desktop.
Save adamb0mb/11131861 to your computer and use it in GitHub Desktop.
Google Apps Script for fetching WalkScore
/****************
* 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;
}
@PhilMiller
Copy link

This was helpful, thanks. I was using some Google Maps bits already anyway, so I tweaked it to automatically query lat/long from that

@joshjacobson
Copy link

joshjacobson commented Jul 4, 2022

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