Last active
April 4, 2017 22:14
-
-
Save bencooper222/9aaf856847801581d6ab6b4a6eb8f103 to your computer and use it in GitHub Desktop.
Google Sheets Geocoding Custom Function
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
// returns latlng from Google API | |
function GEOCODE(input){ | |
var data = JSON.parse(queryGoogle(input)); | |
var location = data.results[0].geometry.location; | |
return location.lat + ", " + location.lng; | |
} | |
function queryGoogle(address){ | |
var options = | |
{ | |
"method" : "GET", | |
"followRedirects" : true, | |
"muteHttpExceptions": true | |
}; | |
// wouldn't it be great if we had a real getRequest method in Google Apps Script | |
// you'll have to get your own API key from https://developers.google.com/maps/documentation/geocoding/start | |
var queryString = "?address="+address+"&key="+"YOUR API KEY"; | |
var request = UrlFetchApp.fetch("https://maps.googleapis.com/maps/api/geocode/json"+queryString, options) | |
return request.getContentText(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment