Skip to content

Instantly share code, notes, and snippets.

@joyrexus
Last active August 29, 2015 13:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joyrexus/8691163 to your computer and use it in GitHub Desktop.
Save joyrexus/8691163 to your computer and use it in GitHub Desktop.
Geocoding CLI

Quick demo of how to use Google's Geocoding API.

Pass in an address/location as argument and the location coordinates are returned.

For example:

geocode.coffee '973 E 61st Street, Chicago, IL'

... returns:

{ lat: 41.7840748, lng: -87.6021859 }

See also Google's Geocoding Service.

#!/usr/bin/env coffee
'''
Geocoding demo - find location coords for an address
'''
req = require 'request'
argv = require('optimist').argv
coords = (data) ->
if data.results.length
console.log data.results[0].geometry.location
else
console.log "Could not identify location:", data.status
find = (address, callback) ->
url = 'https://maps.googleapis.com/maps/api/geocode/json?' \
+ "address=#{address}&sensor=false"
req url, (err, res, body) -> callback JSON.parse(body)
address = if argv._.length
argv._
else
"27 Canterbury Way, Sewanee, TN 37375"
find address, coords
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment