bkerr has asked for the wisdom of the Perl Monks concerning the following question:

Hi there, been struggling with getting the coordinates out of the Geo::Coder::Google V3. When I dump the call results I get:
$VAR1 = { 'formatted_address' => 'Huoben 2, 6023 Rothenburg, Schweiz', 'types' => [ 'street_address' ], 'address_components' => [ { 'types' => [ 'street_number' ], 'short_name' => '2', 'long_name' => '2' }, { 'types' => [ 'route' ], 'short_name' => 'Huoben', 'long_name' => 'Huoben' }, { 'types' => [ 'locality', 'political' ], 'short_name' => 'Rothenburg', 'long_name' => 'Rothenburg' }, { 'types' => [ 'administrative_area_level_2', 'polit +ical' ], 'short_name' => 'Hochdorf', 'long_name' => 'Hochdorf' }, { 'types' => [ 'administrative_area_level_1', 'political' ], 'short_name' => 'LU', 'long_name' => 'Luzern' }, { 'types' => [ 'country', 'political' ], 'short_name' => 'CH', 'long_name' => 'Schweiz' }, { 'types' => [ 'postal_code' ], 'short_name' => '6023', 'long_name' => '6023' } ], 'geometry' => { 'viewport' => { 'southwest' => { 'lat' => '47.0860081197085', 'lng' => '8.2563233197085' }, 'northeast' => { 'lat' => '47.0887060802915', 'lng' => '8.2590212802915' } }, 'location' => { 'lat' => '47.0873571', 'lng' => '8.2576723' }, 'location_type' => 'ROOFTOP' } };
I am trying to extract the latitude and longitude with:
my $long = $location->{'geometry'}{'viewport'}{'location'}{'lng'}; my $lat = $location->{'geometry'}{'viewport'}{'location'}{'lat'};
Which is giving me no results. Can anyone help me with this please? Many Thanks!

Replies are listed 'Best First'.
Re: Google decoder
by hdb (Monsignor) on Sep 10, 2013 at 07:18 UTC

    'location' is under 'geometry', not under 'viewport'. So the following does it:

    my $long = $location->{'geometry'}{'location'}{'lng'}; my $lat = $location->{'geometry'}{'location'}{'lat'};
      Yes Great that worked. Thanks for the replies!
Re: Google decoder
by McA (Priest) on Sep 10, 2013 at 06:59 UTC

    The following should do it:

    my $lat = $location->{'geometry'}->{'location'}->{'lat'};

    UPDATE: I hit the same error as you. The follwoing should make it clear after reformatting:

    'geometry' => { 'viewport' => { 'southwest' => { 'lat' => '47.0860081197085', 'lng' => '8.2563233197085' }, 'northeast' => { 'lat' => '47.0887060802915', 'lng' => '8.2590212802915' } }, 'location' => { 'lat' => '47.0873571', 'lng' => '8.2576723' }, 'location_type' => 'ROOFTOP' }

    McA

Re: Google decoder (how to access hash values, perl dereferencing string xpaths data dumper )
by Anonymous Monk on Sep 10, 2013 at 08:25 UTC

    code outputs perl code like data::dumper only more verbose (ready for copy/paste to access ) ex

    $location->{address_components}[0]{types}[0] = "street_number"; #d3 $location->{address_components}[5]{short_name} = "CH"; #d2 $location->{geometry}{location_type} = "ROOFTOP"; #d1