Perl Geocoding Tutorial

Install Perl geocoding library

# install with cpanm
$ cpanm Geo::Coder::OpenCage

Geocode an address

use Geo::Coder::OpenCage;

my $Geocoder = Geo::Coder::OpenCage->new(
    api_key => 'YOUR-API-KEY',
);

my $result = $Geocoder->geocode(
    location => "Псковская улица, Санкт-Петербург, Россия",
    language => "ru",
    countrycode => "ru",
);

Batch geocode addresses

my @places = ('London', 'Paris', 'Berlin');
my %results;

# loop through the list, geocoding each individually
foreach my $place (@places){
    my $result = $Geocoder->geocode(
        location => $place
    );
    my $msg = $result->{status}->{message};
    if ($msg eq 'OK'){
        $results{$place} = $result;
    } else {
        warn "failed to geocode $place : $msg";
    }
}

Geocode coordinates

use Geo::Coder::OpenCage;

my $Geocoder = Geo::Coder::OpenCage->new(
    api_key => 'YOUR-API-KEY',
);

my $result = $Geocoder->reverse_geocode(lat => -22.6792, lng => 14.5272);