Whatever course you decide upon, there is always someone to tell you that you are wrong. There are always difficulties arising which tempt you to believe that your critics are right. To map out a course of action and follow it to an end requires courage.
-Ralph Waldo Emerson

IP 2 Location is a neat little tool that I’ve seen on a few sites but no “real” here’s how to make it happen guide.  It’s actually incredible to think that these are actually just large databases that are used to convert IP addresses to geographic locations.   I found such a database from Max Mind called GeoCityLite.
GeoCityDB
I’m using the city version but you can just as easily use the country version which is smaller and faster.  For this example either should work.    The following code assumes you know how to use LOAD DATA INFILE and are using the csv->mysql database information on their site.
Here is a sample I used:

1
mysql> LOAD DATA LOCAL INFILE "/<mypathtothefile>/GeoLiteCity-Location.csv" INTO TABLE geo_blocks FIELDS TERMINATED BY "," OPTIONALLY ENCLOSED BY '"' LINES TERMINATED BY "\n";

After the database is loaded the rest is just using examples from Google Maps API. I used the following code directly from the code given when applying for a Google Map API Key. I modified it to include both the function to use the database and the functions to use the JSON from Google. Check it out below.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<?php
//Traditional defines for later
$googlekey = 'YOUR GOOGLE KEY';
define(DB_HOST,'host');
define(DB_NAME,'name');
define(DB_USER,'user');
define(DB_PASS,'pass');
// Required to make the ip looking work converts address to numbers
function ipaddr2num($ip){
	$ip_parts = explode('.',$ip);
 
	return 16777216*$ip_parts[0] + 65536*$ip_parts[1] + 256*$ip_parts[2] + $ip_parts[3];  
}
$ipnum = ipaddr2num($_SERVER['REMOTE_ADDR']);
 
 
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if (!$link) {
    die('Not connected : ' . mysql_error());
}
$db_selected = mysql_select_db(DB_NAME, $link);
if (!$db_selected) {
    die ('Can\'t use foo : ' . mysql_error());
}
 
$sql = 'SELECT locid FROM ip_blocks WHERE '.$ipnum.' >= startblock AND
	'.$ipnum.' <= endblock';
 
$rs = mysql_query($sql);
$locid = mysql_result($rs,0);
$sql = 'SELECT lat,longitude FROM geo_blocks WHERE locid = '.$locid;
$rs = mysql_query($sql);
$line = mysql_fetch_array($rs);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
    <title>Google Maps JavaScript API Example</title>
    <script src="http://maps.google.com/maps?file=api&amp;v=2&amp;key=<?php echo $googlekey;?>"
      type="text/javascript"></script>
    <script type="text/javascript">
 
    //<![CDATA[
 
    function load() {
      if (GBrowserIsCompatible()) {
        var map = new GMap2(document.getElementById("map"));
        var point = new GLatLng(<?php echo $line[0] .','. $line[1];?>);
	map.setCenter(point, 13);
	map.addOverlay(new GMarker(point));
 
      }
    }
 
    //]]>
    </script>
  </head>
  <body onload="load()" onunload="GUnload()">
    <div id="map" style="width: 150px; height: 200px"></div>
  </body>
</html>

I hope you enjoy this bit of code and I will have a working example of in action soon. IP 2 Location doesn’t have to be hard and this example with PHP and Google maps proves it.

You want a lesson? I’ll give you a lesson. How about a geography lesson? My father’s from Puerto Rico. My mother’s from El Salvador. And neither one of those is Mexico.
-Jennifer Esposito