| 1 | package Geo::Coder::All::Bing; | ||
| 2 | use Moose; | ||
| 3 | use namespace::autoclean; | ||
| 4 | use Carp; | ||
| 5 | use Locale::Codes::Country; | ||
| 6 | use Data::Dumper; | ||
| 7 | use Geo::Coder::Bing; | ||
| 8 | with 'Geo::Coder::Role::Geocode'; | ||
| 9 | has 'Bing' =>( | ||
| 10 | is => 'rw', | ||
| 11 | isa => 'Geo::Coder::Bing', | ||
| 12 | writer => 'set_bing_geocoder' | ||
| 13 | ); | ||
| 14 | sub geocode_local { | ||
| 15 | my ($self,$rh_args)= @_; | ||
| 16 | croak "API key needed" unless ($rh_args->{key}); | ||
| 17 | croak "Location string needed" unless ($rh_args->{location}); | ||
| 18 | my $rh_data; | ||
| 19 | $self->set_bing_geocoder(Geo::Coder::Bing->new( | ||
| 20 | key => $rh_args->{key}, | ||
| 21 | )); | ||
| 22 | my $rh_response = $self->Bing->geocode(location => $rh_args->{location}); | ||
| 23 | print STDERR Dumper($rh_response) if($rh_args->{DEBUG}); | ||
| 24 | return $self->_process_response($rh_response); | ||
| 25 | } | ||
| 26 | sub reverse_geocode_local{ | ||
| 27 | my ($self,$rh_args) = @_; | ||
| 28 | return 'Reverse Geocode is not available for Bing'; | ||
| 29 | } | ||
| 30 | sub _process_response{ | ||
| 31 | my ($self,$rh_response ) = @_; | ||
| 32 | return undef unless($rh_response); | ||
| 33 | my $rh_data; | ||
| 34 | $rh_data->{geocoder} = 'Bing'; | ||
| 35 | return $rh_data unless($rh_response->{address}); | ||
| 36 | $rh_data->{address} = $rh_response->{address}{formattedAddress} ; | ||
| 37 | $rh_data->{country} = $rh_response->{address}{countryRegion} ; | ||
| 38 | $rh_data->{country_code} = uc(country2code($rh_data->{country})) if($rh_data->{country}); | ||
| 39 | $rh_data->{country_code_alpha_3} = uc(country2code($rh_data->{country},'alpha-3')) ; | ||
| 40 | if( $rh_response->{point}{type} eq 'Point' && @{$rh_response->{point}{coordinates}} == 2){ | ||
| 41 | $rh_data->{coordinates}{lat} = $rh_response->{point}{coordinates}[0]; | ||
| 42 | $rh_data->{coordinates}{lon} = $rh_response->{point}{coordinates}->[1]; | ||
| 43 | } | ||
| 44 | return $rh_data; | ||
| 45 | } | ||
| 46 | __PACKAGE__->meta->make_immutable; | ||
| 47 | 1; |