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 ?

Thanks,
Jim West
Comment
Watch Question

nociSoftware Engineer
CERTIFIED EXPERT
Distinguished Expert 2019

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

https://metacpan.org/search?q=geolocation

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.).

Author

Commented:
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.
Thanks,
Jim West
Molnár IstvánHelpDesk / Programmer
CERTIFIED EXPERT

Commented:
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'));
$geocoder_many->set_picker_callback('max_precision');

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
CERTIFIED EXPERT
Commented:
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
etc.
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
jimtwest3Retired

Commented:
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
CERTIFIED EXPERT
Distinguished Expert 2019

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

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

Thanks,
Jim
Molnár IstvánHelpDesk / Programmer
CERTIFIED EXPERT

Commented: