blib/lib/Geo/Google/StaticMaps.pm untested

filter: Statementstmt 0.0% 0 / 157 Branchbran 0.0% 0 / 72 Conditioncond 0.0% 0 / 18 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 0.0% 0 / 19 Podpod 0.0% 0 / 15 totaltotal 0.0% 0 / 291 CC 37 SCAR 35.9
CC
37
Cov
0%
_make_url
110.0
_normalize_opts
42.0
_normalize_marker
42.0
CRAP 36.3
1  
2 =head1 NAME
3  
4 Geo::Google::StaticMaps - API for generating URLs for static Google Maps
5  
6 =head1 SYNOPSIS
7  
8 use Geo::Google::StaticMaps;
9
10 my $url = Geo::Google::StaticMaps->url(
11 key => "your Google Maps API key",
12 size => [ 400, 300 ],
13 center => [ 51.855970, 0.958499 ],
14 zoom => 13,
15 );
16  
17 =cut
18  
19 package Geo::Google::StaticMaps;
20  
21 use strict;
22 use warnings;
23 use Carp;
24 use vars qw($VERSION);
25  
26 $VERSION = 0.10;
27  
28 sub new {
29 my ($class, %opts) = @_;
30  
31 my $self = { %opts };
32  
33 return bless $self, $class;
34 }
35  
36 sub url {
37 my ($self, %opts) = @_;
38  
39 if (ref $self) {
40 Carp::croak("Can't pass options when url is used as an instance method") if %opts;
41 %opts = %$self;
42 }
43  
44 return _make_url(_normalize_opts(%opts));
45 }
46  
47 sub _normalize_opts {
48 my %opts = @_;
49  
50 my $format = delete($opts{format}) || "png";
51 my $maptype = delete($opts{maptype});
52 my $size = _normalize_size(delete($opts{size}));
53 my $markers = _normalize_markers(delete($opts{markers}));
54 my $paths = _normalize_paths(delete($opts{path}));
55 my $center = _normalize_point(delete($opts{center}));
56 my $zoom = delete($opts{zoom});
57 my $span = _normalize_point(delete($opts{span}));
58 my $frame = delete($opts{frame});
59 $frame = ($frame ? 1 : 0) if defined $frame;
60 my $hl = delete($opts{hl});
61 my $key = delete($opts{key});
62  
63 Carp::croak("Must supply a Google Maps API Key") unless $key;
64 Carp::croak("Must give the map size") unless $size;
65 Carp::croak("Unsupported options: ".join(', ', keys %opts)) if %opts;
66  
67 return (
68 format => $format,
69 maptype => $maptype,
70 size => $size,
71 markers => $markers,
72 paths => $paths,
73 center => $center,
74 zoom => $zoom,
75 span => $span,
76 frame => $frame,
77 hl => $hl,
78 key => $key,
79 );
80  
81 }
82  
83 sub _normalize_markers {
84 my $markers = shift;
85  
86 return undef unless defined $markers;
87  
88 if (ref $markers ne 'ARRAY') {
89 return [ _normalize_marker($markers) ];
90 }
91 else {
92 return [ map { _normalize_marker($_) } @$markers ];
93 }
94 }
95  
96 sub _normalize_points {
97 my $points = shift;
98  
99 return undef unless defined $points;
100  
101 if (ref $points ne 'ARRAY') {
102 return [ _normalize_point($points) ];
103 }
104 else {
105 return [ map { _normalize_point($_) } @$points ];
106 }
107 }
108  
109 sub _normalize_paths {
110 my $paths = shift;
111  
112 return undef unless defined $paths;
113  
114 if (ref $paths ne 'ARRAY') {
115 return [ _normalize_path($paths) ];
116 }
117 else {
118 return [ map { _normalize_path($_) } @$paths ];
119 }
120 }
121  
122 sub _normalize_marker {
123 my $marker = shift;
124  
125 return undef unless defined $marker;
126  
127 if (ref $marker eq 'ARRAY') {
128 $marker = {
129 point => $marker,
130 };
131 }
132  
133 my %opts = %$marker;
134  
135 my $point = _normalize_point(delete($opts{point}));
136  
137 my $lat = delete($opts{lat});
138 my $lon = delete($opts{lon});
139  
140 if (defined($lat) || defined($lon)) {
141 Carp::croak("Must give both lat and lon") unless defined($lat) && defined($lon);
142 Carp::croak("Can't give both point and separate lat and lon") if defined($point);
143 $point = _normalize_point([ $lat, $lon ]);
144 }
145  
146 my $size = delete($opts{size}) || "normal";
147 my $color = delete($opts{color}) || "red";
148 my $letter = delete($opts{letter}) || "";
149  
150 return {
151 point => $point,
152 size => $size,
153 color => $color,
154 letter => $letter,
155 };
156 }
157  
158 sub _normalize_path {
159 my $path = shift;
160  
161 return undef unless defined $path;
162  
163 my %opts = %$path;
164  
165 my $points = _normalize_points(delete($opts{points}));
166 my $weight = delete($opts{weight}) || 5;
167 my $color = _normalize_color(delete($opts{color})) || "rgb:0x0000ff";
168  
169 return {
170 points => $points,
171 weight => $weight,
172 color => $color,
173 };
174  
175 }
176  
177 sub _normalize_point {
178 my $point = shift;
179  
180 return undef unless defined $point;
181  
182 my ($lat, $lon);
183  
184 if (ref $point eq 'ARRAY') {
185 ($lat, $lon) = @$point;
186 }
187 else {
188 $lat = $point->{lat};
189 $lon = $point->{lon};
190 }
191  
192 return [ $lat, $lon ];
193 }
194  
195 sub _normalize_size {
196 my $dimensions = shift;
197  
198 return undef unless defined $dimensions;
199  
200 my ($width, $height);
201  
202 if (ref $dimensions eq 'ARRAY') {
203 ($width, $height) = @$dimensions;
204 }
205 else {
206 $width = $dimensions->{width};
207 $height = $dimensions->{height};
208 }
209  
210 return [ $width, $height ];
211 }
212  
213 sub _normalize_color {
214 my $color = shift;
215  
216 return undef unless defined $color;
217  
218 if (ref $color eq '') {
219 # User passed it in as a string in a format suitable for direct use.
220 return $color;
221 }
222 else {
223 my ($r, $g, $b, $a);
224  
225 if (ref($color) eq 'ARRAY') {
226 ($r, $g, $b, $a) = @$color;
227 }
228 else {
229 ($r, $g, $b, $a) = map { $color->{$_} } qw(red green blue alpha);
230 }
231  
232 if (defined($a)) {
233 return sprintf("rgba:0x%02x%02x%02x%02x", $r, $g, $b, $a);
234 }
235 else {
236 return sprintf("rgb:0x%02x%02x%02x", $r, $g, $b);
237 }
238 }
239  
240 }
241  
242 sub _make_url {
243 my %opts = @_;
244  
245 my $prefix = "http://maps.google.com/staticmap?";
246  
247 my %args = ();
248  
249 $args{center} = _make_point_string($opts{center}) if $opts{center};
250 $args{zoom} = $opts{zoom} if $opts{zoom};
251 $args{size} = sprintf("%ix%i", @{$opts{size}});
252 $args{format} = $opts{format} if $opts{format};
253 $args{maptype} = $opts{maptype} if $opts{maptype};
254 $args{markers} = _make_markers_string($opts{markers}) if $opts{markers};
255 $args{span} = _make_point_string($opts{span}) if $opts{span};
256 $args{frame} = $opts{frame} if $opts{frame};
257 $args{hl} = $opts{hl} if $opts{hl};
258 $args{key} = $opts{key};
259  
260 my @args = map { _urlencode($_)."="._urlencode($args{$_}) } keys %args;
261 my $qs = join('&', @args);
262  
263 # We do the paths separately because there can be more than one so we can't
264 # represent it as a hash.
265 my $path_extras = "";
266 if ($opts{paths}) {
267 my @path_extras = ();
268 foreach my $path (@{$opts{paths}}) {
269 my @points_string = map { _make_point_string($_) } @{$path->{points}};
270 my $points_string = join('|', @points_string);
271 push @path_extras, "&path=".$path->{color}.",weight:".$path->{weight}.",".$points_string;
272 }
273 $path_extras = join('', @path_extras);
274 }
275  
276 return $prefix.$qs.$path_extras;
277 }
278  
279 sub _make_markers_string {
280 my $markers = shift;
281  
282 my @bits = map { sprintf("%s,%s%s%s", _make_point_string($_->{point}), $_->{size}, $_->{color}, $_->{letter}) } @$markers;
283  
284 return join("|", @bits);
285 }
286  
287 sub _make_point_string {
288 my $point = shift;
289  
290 return sprintf("%f,%f", @$point);
291 }
292  
293 sub _urlencode {
294 my $s = $_[0];
295 local $_;
296 $s =~ s/([^\w,\.\-])/"%".uc(sprintf("%2.2x", ord($1)))/eg;
297 return $s;
298 }
299  
300  
301 1;
302  
303 =head1 DESCRIPTION
304  
305 This module provides a simple wrapper around generating URLs for Google's Static Maps API.
306 You can find out more about the Static Maps API here: L<http://code.google.com/apis/maps/documentation/staticmaps/>.
307  
308 At the time of writing this module supports all features supported by the Static Maps API,
309 but arguments are provided as a data structure rather than as the (rather inconsistent) string
310 representations required by the API.
311  
312 There is a single public static method, called C<url>, which will return a string containing
313 a static map URL given a data structure of arguments. The various properties and their structures
314 are given below.
315  
316 =head1 DATA STRUCTURES
317  
318 The Static Maps API has several concepts that are represented by data structures. These
319 are described in the following sections, along with how they can be represented in this API.
320  
321 =head2 Maps
322  
323 The foremost entity is the map itself. It is given as a set of name-value pairs (i.e. a hash)
324 passed into the C<url> method. The properties supported are:
325  
326 =head3 format
327  
328 The image format that will be used for the resulting map.
329  
330 Takes a string containing either "png", "gif" or "jpeg". The default is "png".
331  
332 =head3 maptype
333  
334 The tileset used to generate the map.
335  
336 This can be either "roadmap", which is the standard Google Maps road map, or "mobile",
337 which is the variation used on Google Maps For Mobile. The default is "roadmap".
338  
339 =head3 size
340  
341 The size in pixels of the resulting map. This property is B<required>.
342  
343 Given either as a reference to a list containing a (width, height) pair, or
344 as a reference to a hash containing "width" and "height" members. No other
345 members can be present in the hash.
346  
347 =head3 markers
348  
349 A list of markers to include on the map.
350  
351 Given as a list of marker structures (described below), or as a single marker structure.
352  
353 Note that due to an ambiguity the shorthand whereby a marker is given simply as
354 a pair of coordinates in an array ref is not allowed when using the single marker
355 shorthand. Enclose that array ref in a further array ref to create a single-element
356 list.
357  
358 =head3 paths
359  
360 A list of paths (polygons) to draw on the map.
361  
362 Given as a list of path structures (described below) or as a single path structure.
363  
364 =head3 center
365  
366 The geographic coordinates which will be at the center of the resulting map.
367  
368 Given as a point structure (described below).
369  
370 =head3 zoom
371  
372 The zoom level of the resulting map.
373  
374 This is an integer between 0 and 19, where 0 is zoomed out enough to show the whole
375 world given an appropriate map size. Not all zoom levels are supported for the whole
376 world. See the Static Maps API documentation on zoom levels for more information.
377  
378 =head3 span
379  
380 The amount of the world that the map will show in the horizontal and vertical axes.
381  
382 Given as a point structure (described below) containing the number of degrees latitude
383 and longitide that will be shown on the map. Where the aspect ratio doesn't match
384 that of the map itself, additional map data may be shown.
385  
386 It doesn't make much sense to use zoom and span at the same time.
387  
388 =head3 frame
389  
390 Whether to draw a frame around the resulting map image.
391  
392 Given as a boolean. (Usually 1 for true, 0 for false. However, anything that can be
393 evaluated as a boolean is accepted.)
394  
395 =head3 hl
396  
397 A preferred language to use for the map captions.
398  
399 Given as a string containing a language code. This will not necessarily be supported for all parts of the world.
400  
401 =head3 key
402  
403 Your Google Maps API key. This is B<required>.
404  
405 Given as a string. You must sign up for an API key from the Maps API website before you can use this API.
406  
407 =head2 Points
408  
409 A point is a (latitude, longitude) pair.
410  
411 It can be given either as a reference to a list containing the pair (with latitude first) or a reference
412 to a hash containing "lat" and "lon" members, and no other members.
413  
414 Points are used as part of all of the other geographical structures.
415  
416 =head2 Markers
417  
418 A marker is an icon placed at a particular point on the map. Google provides for a few different
419 icon sizes and colors, and for some marker sizes allows the markers to be annotated with
420 a letter of the alphabet. See the Static Maps API documentation for more information.
421  
422 As a shorthand, wherever it is not ambiguous, a marker can be represented simply as a point.
423 In this case, all of the default display settings will be used for the marker.
424  
425 A marker is normally represented as a reference to a hash containing the following properties:
426  
427 =head3 point
428  
429 The point at which the marker will be placed on the map. These coordinates can alternatively be given
430 as separate "lat" and "lon" properties on the marker itself, if you wish to avoid creating a separate
431 point structure.
432  
433 =head3 size
434  
435 A keyword which determines what size marker will be used.
436  
437 This is a string containing either "normal", "tiny", "mid" or "small", where "normal" is the largest
438 and the default.
439  
440 =head3 color
441  
442 A keyword which determines what color marker will be used.
443  
444 This is a string containing one of the following color names:
445 "black", "brown", "green", "purple", "yellow", "blue", "gray", "orange", "red" or "white".
446  
447 =head3 letter
448  
449 If the marker is either "normal" or "mid", this gives a letter to be displayed within the marker.
450  
451 This is a string containing a single uppercase letter.
452  
453 =head2 Paths
454  
455 A path is essentially a polyline drawn on the map. A path is formed from a sequence of
456 joined points along with a color and a weight.
457  
458 A path is represented as a reference to a hash containing the following properties:
459  
460 =head3 points
461  
462 The vertices of the path. The visible lines of the path will connect these vertices.
463  
464 Given as a reference to a list of point structures, as described above.
465  
466 =head3 weight
467  
468 How wide the drawn lines will be, as an integer number of pixels.
469  
470 =head3 color
471  
472 The color of the lines making up the path.
473  
474 Given as a color structure, as described below.
475  
476 =head2 Colors
477  
478 A color is an RGB triple with an optional alpha channel.
479  
480 Colors are represented either as a list with three or four elements (the latter
481 being for an alpha channel) or as a reference to a hash containing "red", "green",
482 "blue" and "alpha" properties and no other properties. Each channel value is
483 an integer between 0 and 255 inclusive. For alpha, 0 is completely transparent
484 and 255 is complete opaque. If an alpha channel is not given, it will default to 255.
485  
486 Colors can also be represented as a simple string containing something that's valid
487 as per the Google Maps API, including the "rgb:" or "rgba:" prefix.
488  
489 =head1 CAVEATS
490  
491 Although this module will do some basic sanity checks to make sure that you have all of the required
492 arguments, it intentionally avoids itself checking for many of the limits currently imposed by the
493 Static Maps API, since those limits may be altered by Google in future.
494  
495 You should consult the Google Static Maps API documentation to discover the range of values supported
496 for each property.
497  
498 =head1 POSSIBLE FUTURE ENHANCEMENTS
499  
500 It was planned for this module to offer also an object-oriented API where the data structure
501 can be built up in stages by successive method calls. However, in the interests of keeping things
502 simple the author decided to support in this version only the static method which takes all arguments
503 in a single data structure.
504  
505 In future, particularly if the Static Maps API becomes more complicated, it may be desirable to
506 support an extended OO version of this API.
507  
508 =head1 AUTHOR
509  
510 Copyright 2008 Martin Atkins <mart@degeneration.co.uk>
511  
512 =head1 LICENCE
513  
514 This module may be distributed under the same terms as Perl itself.
515