Skip to content

Instantly share code, notes, and snippets.

@xeoncross
Created February 18, 2014 18:22
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 xeoncross/9076679 to your computer and use it in GitHub Desktop.
Save xeoncross/9076679 to your computer and use it in GitHub Desktop.
#!/usr/bin/php -q
<?php
/**
* Returns tab-delimited latitude,longitude,formatted address for given address from Google Maps API
* @author Warren Stevens (warbaby67@gmail.com)
**/
$base = 'http://maps.googleapis.com/maps/api/geocode/json?';
if (count($argv) < 2)
{
print "usage: ".__FILE__." \"<city,state>\"\n";
exit(64); # Command line usage error
}
$result = json_decode(file_get_contents($base.'address='.urlencode($argv[1]).'&sensor=false'),true);
if ($result['status'] == 'ZERO_RESULTS' || count($result['results']) == 0)
{
print 'No results for "'.$argv[1].'"'."\n";
exit(65); # Data format error
}
$lat = $result['results'][0]['geometry']['location']['lat'];
$lng = $result['results'][0]['geometry']['location']['lat'];
$add = $result['results'][0]['formatted_address'];
print sprintf("%s\t%s\t%s\n", $lat,$lng,$add);
exit(0);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment