| 1 | package Geo::Coder::Many::Google; | ||
| 2 | |||
| 3 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 1 | ▶ | use Carp; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 0 | ▶ | use Geo::Coder::Many::Util; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 7 | use base 'Geo::Coder::Many::Generic'; | ||
| 8 | |||
| 9 | =head1 NAME | ||
| 10 | |||
| 11 | Geo::Coder::Many::Google - Plugin for the google maps geocoder | ||
| 12 | |||
| 13 | =head1 VERSION | ||
| 14 | |||
| 15 | Version 0.02 | ||
| 16 | |||
| 17 | =cut | ||
| 18 | |||
| 19 | our $VERSION = '0.02'; | ||
| 20 | |||
| 21 | # Requires Geo::Coder::Google 0.06 or above | ||
| 22 | sub _MIN_MODULE_VERSION { return '0.06'; } | ||
| 23 | |||
| 24 | |||
| 25 | =head1 SYNOPSIS | ||
| 26 | |||
| 27 | This class wraps Geo::Coder::Google such that it can be used in | ||
| 28 | Geo::Coder::Many, by converting the results to a standard form. | ||
| 29 | |||
| 30 | Note: Geo::Coder::Google uses the deprecated version 2 of the Google | ||
| 31 | geocoder. There is a newer Geo::Coder::Googlev3 (also supported by | ||
| 32 | Geo::Coder::Many). | ||
| 33 | |||
| 34 | =head1 METHODS | ||
| 35 | |||
| 36 | =head2 geocode | ||
| 37 | |||
| 38 | Takes a location string, geocodes it using Geo::Coder::Google, and returns the | ||
| 39 | result in a form understandable to Geo::Coder::Many | ||
| 40 | |||
| 41 | =cut | ||
| 42 | |||
| 43 | # see details of google's response format here: | ||
| 44 | # v2: http://code.google.com/apis/maps/documentation/javascript/v2/services.html#Geocoding | ||
| 45 | # v3: http://code.google.com/apis/maps/documentation/geocoding/ | ||
| 46 | |||
| 47 | sub geocode { | ||
| 48 | my $self = shift; | ||
| 49 | my $location = shift; | ||
| 50 | defined $location or croak "Geo::Coder::Many::Google::geocode | ||
| 51 | method must be given a location."; | ||
| 52 | |||
| 53 | my @raw_replies = $self->{GeoCoder}->geocode( $location ); | ||
| 54 | |||
| 55 | my $Response = Geo::Coder::Many::Response->new( { location => $location } ); | ||
| 56 | |||
| 57 | foreach my $raw_reply ( @raw_replies ) { | ||
| 58 | |||
| 59 | my $precision = 0; # unknown | ||
| 60 | |||
| 61 | if (defined($raw_reply->{ExtendedData}) | ||
| 62 | && defined($raw_reply->{ExtendedData}{LatLonBox}) ){ | ||
| 63 | |||
| 64 | my $box = $raw_reply->{ExtendedData}{LatLonBox}; | ||
| 65 | # lng and lat in decimal degree format | ||
| 66 | |||
| 67 | $precision = | ||
| 68 | Geo::Coder::Many::Util::determine_precision_from_bbox({ | ||
| 69 | 'lon1' => $box->{south}, | ||
| 70 | 'lat1' => $box->{west}, | ||
| 71 | 'lon2' => $box->{north}, | ||
| 72 | 'lat2' => $box->{east}, | ||
| 73 | }); | ||
| 74 | } | ||
| 75 | |||
| 76 | my $tmp = { | ||
| 77 | address => $raw_reply->{address}, | ||
| 78 | country => $raw_reply->{AddressDetails}{Country}{CountryNameCode}, | ||
| 79 | latitude => $raw_reply->{Point}{coordinates}[1], | ||
| 80 | longitude => $raw_reply->{Point}{coordinates}[0], | ||
| 81 | precision => $precision, | ||
| 82 | }; | ||
| 83 | $Response->add_response( $tmp, $self->get_name()); | ||
| 84 | } | ||
| 85 | return $Response; | ||
| 86 | } | ||
| 87 | |||
| 88 | =head2 get_name | ||
| 89 | |||
| 90 | The short name by which Geo::Coder::Many can refer to this geocoder. | ||
| 91 | |||
| 92 | =cut | ||
| 93 | |||
| 94 | sub get_name { my $self = shift; return 'google ' . $self->{GeoCoder}->VERSION; } | ||
| 95 | |||
| 96 | 1; | ||