#!/usr/bin/perl # geo.pl: very simple JSON/XML interface to Geo::Coder::US # Matt Croydon - http://postneo.com - 6/29/2006 # License: BSD use Geo::Coder::US; use JSON; use CGI; use XML::Simple; Geo::Coder::US->set_db( "geocoder.db" ); my $cgi = new CGI(); my $json = new JSON; my $xs = new XML::Simple(); my $address = $cgi->param('address'); my $output = $cgi->param('output'); $address =~ s/"+"/" "/g; my ($geo) = Geo::Coder::US->geocode($address); sub json { my $geo = shift; print "Content-type: text/javascript\n\n"; print $json->objToJson($geo, {pretty => 1, ident => 2}); print "\n"; } sub xml { my $geo = shift; print "Content-type: text/xml\n\n"; print "\n\n"; print $xs->XMLout($geo, NoAttr => 1, RootName => "location"); print "\n"; } if ($output eq 'json') { json($geo); } elsif ($output eq 'xml') { xml($geo); } else { xml($geo); }