Link to home
Start Free TrialLog in
Avatar of Jim West
Jim West

asked on

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
Avatar of noci
noci

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.).
Avatar of Jim West

ASKER

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
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
ASKER CERTIFIED SOLUTION
Avatar of Molnár István
Molnár István
Flag of Romania image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
@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 ?

Thanks,
Jim