File Coverage

blib/lib/Google/GeoCoder/Smart.pm
Criterion Covered Total %
statement 6 37 16.2
branch 0 10 0.0
condition 0 2 0.0
subroutine 2 4 50.0
pod 2 2 100.0
total 10 55 18.1


line stmt bran cond sub pod time code
1              
2              
3             package Google::GeoCoder::Smart;
4              
5             require Exporter;
6              
7 1     1   15682 use LWP::Simple qw(!head);
  1         60991  
  1         8  
8              
9 1     1   996 use JSON;
  1         10073  
  1         12  
10              
11             our @ISA = qw(Exporter);
12              
13             our @EXPORT = qw(geocode parse);
14              
15             our $VERSION = 1.17;
16              
17             =head1 NAME
18              
19             Smart - Google Maps Api HTTP geocoder
20              
21             =head1 SYNOPSIS
22              
23             use Google::GeoCoder::Smart;
24            
25             $geo = Google::GeoCoder::Smart->new();
26              
27             my ($resultnum, $error, @results, $returncontent) = $geo->geocode("address" => "your address here");
28              
29             $resultnum--;
30              
31             for $num(0 .. $resultnum) {
32              
33             $lat = $results[$num]{geometry}{location}{lat};
34              
35             $lng = $results[$num]{geometry}{location}{lng};
36              
37             };
38              
39             =head1 DESCRIPTION
40              
41             This module provides a simple and "Smart" interface to the Google Maps geocoding API.
42              
43             It is compatible with the google maps http geocoder v3.
44              
45             If Google changes their format, it might stop working.
46              
47             This module only depends on LWP::Simple and JSON.
48              
49             This version removes the depriciated homemade xml parsing and goes completely with the JSON format.
50              
51             If you need the old xml version, the older module versions still have it, but it does have a few problems.
52              
53             #################################################
54              
55             MAKE SURE TO READ GOOGLE's TERMS OF USE
56              
57             they can be found at http://code.google.com/apis/maps/terms.html#section_10_12
58              
59             #################################################
60              
61             If you find any bugs, please let me know.
62              
63             =head1 METHODS
64              
65             =head2 new
66              
67             $geo = Google::GeoCoder::Smart->new("key" => "your api key here", "host" => "host here");
68              
69             the new function normally is called with no parameters.
70              
71             however, If you would like to, you can pass it your Google Maps api key and a host name.
72              
73             the api key parameter is useful for the api premium service.
74              
75             the host paramater is only necessary if you use a different google host than googleapis.com,
76              
77             such as google.com.eu or something like that.http://code.google.com/apis/maps/terms.html#section_10_12
78              
79             =head2 geocode
80              
81             my ($num, $error, @results, $returntext) = $geo->geocode(
82              
83             "address" => "address *or street number and name* here",
84              
85             "city" => "city here",
86              
87             "state" => "state here",
88              
89             "zip" => "zipcode here"
90              
91             );
92              
93             This function brings back the number of results found for the address and
94              
95             the results in an array. This is the case because Google will sometimes return
96              
97             many different results for one address.
98              
99             It also returns the result text for debugging purposes.
100              
101             The geocode method will work if you pass the whole address as the "address" tag.
102            
103             However, it also supports breaking it down into parts.
104              
105             It will return one of the following error messages if an error is encountered
106              
107             connection #something went wrong with the download
108              
109             OVER_QUERY_LIMIT #the google query limit has been exceeded. Try again 24 hours from when you started geocoding
110              
111             ZERO_RESULTS #no results were found for the address entered
112              
113             If no errors were encountered it returns the value "OK"
114              
115             You can get the returned parameters easily through refferences.
116              
117             $lat = $results[0]{geometry}{location}{lat};
118              
119             $lng = $results[0]{geometry}{location}{lng};
120              
121             It is helpful to know the format of the json returns of the api.
122              
123             A good example can be found at http://www.googleapis.com/maps/apis/geocode/json?address=1600+Amphitheatre+Parkway+Mountain+View,+CA+94043&sensor=false
124              
125             =head1 AUTHOR
126              
127             TTG, ttg@cpan.org
128              
129             =head1 COPYRIGHT AND LICENSE
130              
131             Copyright (C) 2014 by TTG
132              
133             This library is free software; you can redistribute it and/or modify
134              
135             it under the same terms as Perl itself, either Perl version 5.10.0 or,
136              
137             at your option, any later version of Perl 5 you may have available.
138              
139              
140             =cut
141              
142             sub new {
143              
144 0     0 1   my ($junk, %params) = @_;
145              
146 0   0       my $host = delete $params{host} || "maps.googleapis.com";
147              
148 0           my $key = delete $params{key};
149              
150 0           bless {"key" => $key, "host" => $host};
151              
152             }
153              
154             sub geocode {
155              
156 0     0 1   my ($self, %params) = @_;
157              
158 0           $addr = delete $params{'address'};
159              
160 0           $CITY = delete $params{'city'};
161              
162 0           $STATE = delete $params{'state'};
163              
164 0           $ZIP = delete $params{'zip'};
165              
166 0           my $keyVar = "";
167              
168 0 0         if($self->{key}) {
169              
170 0           $keyVar = "&key=$self->{key}";
171              
172             }
173              
174 0           my $content = get("http://$self->{host}/maps/api/geocode/json?address=$addr $CITY $STATE $ZIP&sensor=false");
175              
176 0           undef $err;
177              
178 0           undef $error;
179              
180 0 0         if($content =~ m/ZERO_RESULTS/) {
181              
182 0           $error = "ZERO_RESULTS";
183              
184             };
185              
186 0 0         if($content =~ m/OVER_QUERY_LIMIT/) {
187              
188 0           $error = "OVER_QUERY_LIMIT";
189              
190             };
191              
192 0 0         unless(defined $content) {
193              
194 0           $error = "connection";
195              
196             };
197              
198 0 0         unless(defined $error) {
199              
200 0           $error = "OK";
201              
202             };
203              
204 0           undef @results;
205              
206              
207              
208              
209 0           $results_json = decode_json $content;
210              
211             #$error = $results_json->{results}[0];
212              
213             #@results = \$results_json->{results};
214              
215 0           $error = $results_json->{status};
216              
217 0           foreach $res($results_json->{results}[0]) {
218              
219 0           @push = ($res);
220              
221              
222              
223 0           push @results, @push;
224              
225              
226             };
227              
228              
229              
230 0           my $length = @results;
231              
232 0           return $length, $error, @results, $content;
233              
234             }
235              
236              
237              
238              
239             1;
240              
241