Tool or PHP code to convert IP address into lat/lng coordinates

15,062

Solution 1

For one of my sites I made use of maxmind's free geolite country database which can be downloaded here: http://www.maxmind.com/app/geolitecountry

They also provide a city level version which includes the long/lat: http://www.maxmind.com/app/geolitecity

but note that the accuracy on the free version is a lot lower than the paid version.

Solution 2

I suggest using Google Analytics to your problem, but if you want to try yourself here is a starting point:

Take a look at this link http://www.geoplugin.net/php.gp, you get a full list of details about that ip address position, including latitude and longitude.

It is not that acurate but it works, and I use it. Here is a php script actually in use :

<?php
$ip_addr = $_SERVER['REMOTE_ADDR'];
$geoplugin = unserialize( file_get_contents('http://www.geoplugin.net/php.gp?ip='.$ip_addr) );

if ( is_numeric($geoplugin['geoplugin_latitude']) && is_numeric($geoplugin['geoplugin_longitude']) ) {

$lat = $geoplugin['geoplugin_latitude'];
$long = $geoplugin['geoplugin_longitude'];
}
echo $ip_addr.';'.$lat.';'.$long;
?>

You can see it working at http://whateverhappens.org/ip-addr/

And that a look at the Geoplugin website examples.

Share:
15,062
Peter Craig
Author by

Peter Craig

I'm a unicycling acrobat, artist, collector and recreational programmer. Dabbling in web, plugin and app development, framework tinkering, UX design, with a lean toward WordPress plugins and Google APIs (Google Maps, Analytics, AdWords). A little obsessed with statistics, marketing, behaviour and predictive data.

Updated on June 14, 2022

Comments

  • Peter Craig
    Peter Craig almost 2 years

    I have thousands of IP addresses of visitors to my site, what tools can I use to convert these into lat/lng coordinates? I will then be able visualise the data on a map with filters for further demographics gathered.