We help IT Professionals succeed at work.

Need a Perl script to return latitude and longitude for an address

Please recommend a Perl script that will return the latitude and and longitude for an address.
Please explain the pros and cons of signing up for a google api account to use their geo code api.
Are there any alternatives to google for obtaining a latitude and longitude for an address ?

Jim West
Watch Question

nociSoftware Engineer
Distinguished Expert 2019

When looking for Perl Solution try CPAN.org first.....
Search for GEOLOCATION yields:


Which all use different methode.
I am not sure which address you mean, postal address, visiting address, IP address etc.
Many might provide you with the address of a provider of services. (ISP for IP address f.e.).


I am looking a Geo Code Perl module that will return the latitude and longitude for a given address.
For example, my own address is "693 Hickory Ridge Drive, Ellijay, GA US".  
I would like a Perl script that takes Street Address and returns a latitude and longitude.
The Google Geo Coder modules now have a complicated licensing, so I would like an alternative to Google, if that is possible.
Jim West
Molnár IstvánHelpDesk / Programmer

i found some examples that can help :

1. This function/code uses Google API - gets an address and returns lat/lng:
use strict;
use LWP::Simple; # from CPAN
use JSON qw( decode_json ); # from CPAN

sub getLatLong($){
  my ($address) = @_;

  my $format = "json"; #can also to 'xml'

  my $geocodeapi = "https://maps.googleapis.com/maps/api/geocode/";

  my $url = $geocodeapi . $format . "?sensor=false&address=" . $address;

  my $json = get($url);

  my $d_json = decode_json( $json );

  my $lat = $d_json->{results}->[0]->{geometry}->{location}->{lat};
  my $lng = $d_json->{results}->[0]->{geometry}->{location}->{lng};

  return ($lat, $lng);

Open in new window

2. Geocode using OSM :
MetaCPan - as  "noci" suggested

3. OpenCage Perl geocoding library
You can use the following library: geocode-in-perl -  on the website it says that : 2,500 API requests per day. No credit card required.
4. You can use the following code to compare different services:
use Geo::Coder::Bing;
use Geo::Coder::Googlev3;
use Geo::Coder::Mapquest;
use Geo::Coder::OSM;
use Geo::Coder::Many;
use Geo::Coder::Many::Util qw( country_filter );

### Geo::Coder::Many object
my $geocoder_many = Geo::Coder::Many->new( );

$geocoder_many->add_geocoder({ geocoder => Geo::Coder::Googlev3->new });
$geocoder_many->add_geocoder({ geocoder => Geo::Coder::Bing->new( key => 'GET ONE' )});
$geocoder_many->add_geocoder({ geocoder => Geo::Coder::Mapquest->new( apikey => 'GET ONE' )});
$geocoder_many->add_geocoder({ geocoder => Geo::Coder::OSM->new( sources => 'mapquest' )});

$geocoder_many->set_filter_callback(country_filter('United States'));

for my $location (@locations) {
  my $result = $geocoder_many->geocode({ location => $location });

Open in new window

More info about code 1. and 4. : LINK
hope it helps
HelpDesk / Programmer
About Google API :

+ if you use Google API, you can get a $200 monthly credit Equivalent free usage:

Dynamic Maps (JavaScript API)    Up to 28,000 loads       
Directions       Up to 40,000 calls
Geocoding       Up to 40,000 calls
Geolocation       Up to 40,000 calls
Here is a table of prices for Google Services: LINK
To calculate / estimate the price, you can use the following price calculator from Google: LINK

+ if you have few requests and loads, that will not exceed the monthly credit, i would go with Google APIs

+ after registering, on Google Cloud Platform, you can enable / disable APIs and Services

+ on Google Cloud Platform you can restrict the API to :
                           None (Not restricted)
                           HTTP Referrers (Websites): you can add multiple website
                           IP Address
                           Android Apps
                           IOS Apps

+ in Google Could Platform you can also check the traffic for this APIs and services, that i think is a great feature

- for registering you will need a credit card

- if you exceed the mouthy 200$ free credit, you must pay

hope it helps

Hi Molnar,
Thank you for your message.
Your information on the Google API Account pricing and usage is exactly what I needed to understand.
Please Close My Ticket ( Question ).
Your email resolves my concerns and issue.
Thank you very much for your help!
Jim West
nociSoftware Engineer
Distinguished Expert 2019

@jimtwest3, you need to close it yourself. we cannot close and select the right solution for you.

I am new to this system.   Please tell me how to Close this Question and Mark it Answered and Solved ?

Molnár IstvánHelpDesk / Programmer