Geo-Coder-GooglePlaces

 view release on metacpan or  search on metacpan

lib/Geo/Coder/GooglePlaces/V3.pm  view on Meta::CPAN

use warnings;

use Carp;
use Encode;
use JSON::MaybeXS;
use HTTP::Request;
use LWP::UserAgent;
use URI;

my @ALLOWED_FILTERS = qw/route locality administrative_area postal_code country/;

=head1 NAME

Geo::Coder::GooglePlaces::V3 - Google Places Geocoding API V3

=head1 VERSION

Version 0.06

=cut

our $VERSION = '0.06';

=head1 SYNOPSIS

    use Geo::Coder::GooglePlaces;

    my $geocoder = Geo::Coder::GooglePlaces->new();
    my $location = $geocoder->geocode(location => 'Hollywood and Highland, Los Angeles, CA');

=head1 DESCRIPTION

Geo::Coder::GooglePlaces::V3 provides a geocoding functionality using Google Places API V3.

=head1 SUBROUTINES/METHODS

=head2 new

  $geocoder = Geo::Coder::GooglePlaces->new();
  $geocoder = Geo::Coder::GooglePlaces->new(language => 'ru');
  $geocoder = Geo::Coder::GooglePlaces->new(gl => 'ca');
  $geocoder = Geo::Coder::GooglePlaces->new(oe => 'latin1');

To specify the language of Google's response add C<language> parameter
with a two-letter value. Note that adding that parameter does not
guarantee that every request returns translated data.

You can also set C<gl> parameter to set country code (e.g. I<ca> for Canada).

You can ask for a character encoding other than utf-8 by setting the I<oe>
parameter, but this is not recommended.

You can optionally use your Places Premier Client ID, by passing your client
code as the C<client> parameter and your private key as the C<key> parameter.
The URL signing for Premier Client IDs requires the I<Digest::HMAC_SHA1>
and I<MIME::Base64> modules. To test your client, set the environment
variables GMAP_CLIENT and GMAP_KEY before running v3_live.t

  GMAP_CLIENT=your_id GMAP_KEY='your_key' make test

You can get a key from L<https://console.developers.google.com/apis/credentials>.

=cut

sub new {
    my($class, %args) = @_;

	if(!defined($class)) {
		# Geo::Coder::GooglePlaces::new() used rather than Geo::Coder::GooglePlaces::new()
		$class = __PACKAGE__;
	} elsif(ref($class)) {
		# clone the given object
		return bless { %{$class}, %args }, ref($class);
	}

    my $ua       = delete $args{ua}       || LWP::UserAgent->new(agent => __PACKAGE__ . "/$VERSION");
    my $host     = delete $args{host}     || 'maps.googleapis.com';

    my $language = delete $args{language} || delete $args{hl};
    my $region   = delete $args{region}   || delete $args{gl};
    my $oe       = delete $args{oe}       || 'utf8';
    my $sensor   = delete $args{sensor}   || 0;
    my $client   = delete $args{client}   || '';
    my $key      = delete $args{key}      || '';
    my $components = delete $args{components};

    return bless {
        ua => $ua, host => $host, language => $language,
        region => $region, oe => $oe, sensor => $sensor,
        client => $client, key => $key,
        components => $components,
    }, $class;
}

=head2 geocode

  $location = $geocoder->geocode(location => $location);
  @location = $geocoder->geocode(location => $location);

Queries I<$location> to Google Places geocoding API and returns hash
reference returned back from API server.
When you call the method in
an array context, it returns all the candidates got back, while it
returns the 1st one in a scalar context.

When you'd like to pass non-ASCII string as a location, you should
pass it as either UTF-8 bytes or Unicode flagged string.

=cut

sub geocode {
    my $self = shift;

    my %param;
    if (@_ % 2 == 0) {
        %param = @_;
    } else {
        $param{location} = shift;
    }

    my $location = $param{location}



( run in 0.851 second using v1.01-cache-2.11-cpan-cdf2f3d4e48 )