I recently wanted to try my hand at writing a little iPhone app for helping students find University grant funding. It turns out to be a bit more difficult than I’d expected, but part of the app was to be a listing of all the available universities near the student. This, of course, would involve actually having a list of Universities. To make a long story short, once I got the list (Wikipedia rocks), I needed to get their locations so I could do a proximity search from the user’s coordinates. Since I didn’t want to spend too much time entering every one of the several hundred University names into Google, I decided to whip up a simple little script in Perl to do this for me.
#!/usr/bin/perl use strict; use warnings; use Geo::Coder::Google; our $apikey = 'Your-API-Key'; my $geocoder = Geo::Coder::Google->new(apikey => $apikey); while (<>) { chomp $_; my $location = $geocoder->geocode( location => $_ ); print "$_: "; if (ref($location) eq 'HASH') { print "n"; print " Address: $location->{address}n"; print " Latitude: $location->{Point}{coordinates}[1]n"; print " Longitude: $location->{Point}{coordinates}[0]n"; } else { print "UNKNOWNn"; } }
If it wasn’t for Perl, this would be fiendishly complicated. I just threw that in my ~/bin directory and passed a list of addresses to it on STDIN. Or you could give it a list of files that contain addresses, one per line, and it will give you a YAML output of the results. For instance:
nachbaur$ echo "University California San Diego" | ~/bin/str2geo.pl University California San Diego: Address: University City, San Diego, CA, USA Latitude: 32.854672 Longitude: -117.204533
I hope you have fun with it. It was easy to build, but writing little pipe commands like this makes using a Mac with Perl a blast.