PHP - map a users IP address with Google Maps

May 12th 2011

So, some time ago I found a script online that would allow you to map a users IP address. Well, I corrected it to allow for the new request from ipinfodb.

The original post is located:


(that one no longer works by the way).

I've just finished the new version of this script to allow for a more fully interactive function.

With this new function you have complete control over the size of the input variables as well as the height and width of the resulting map.

Here it is, in all its glory.

PHP Code:
 * Function written by Roger E Thomas (
 * 2011

$apiKey=""// your IP Info API key
$googleMapsAPIKey=""// your Google Maps API Key
$ipAddress $_SERVER['REMOTE_ADDR'];
$mapResult mapUserIPWithGoogleMaps($ipAddress,"400px","400px",$apiKey,$googleMapsAPIKey);

mapUserIPWithGoogleMaps($ipAddress,$mapHeight,$mapWidth,$apiKey,$googleMapsAPIKey) {
$xml simplexml_load_file(''.$apiKey.'&ip='.$ipAddress.'&timezone=true');
$result['ip'] = $xml->Ip;
$result['status'] = $xml->Status;
$result['country'] = $xml->CountryName;
$result['region'] = $xml->RegionName;
$result['city'] = $xml->City;
$result['latitude'] = $xml->Latitude;
$result['longitude'] = $xml->Longitude;
$result['timezone'] = $xml->TimezoneName;
$result['jsLink'] = "<script src=\"".$googleMapsAPIKey."\" type=\"text/javascript\"></script>";
$result['mapDiv'] = "
        <div id=\"map\" style=\"width: "
.$mapWidth."; height: ".$mapHeight."; margin-top:20px;\"></div>
            <script type=\"text/javascript\">
            var map = new GMap(document.getElementById(\"map\"));
            var point = new GPoint("
            map.centerAndZoom(point, 3);
            var marker = new GMarker(point);

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "">
<html xmlns="">
echo $mapResult['jsLink'];
<body style="text-align:center;">
<div style="margin-left:auto;margin-right:auto;width:600px;text-align:left;">
    <b>IP Address:</b> <?php echo $mapResult['ip']; ?><br />
    <b>Country:</b> <?php echo $mapResult['country']; ?><br />
    <b>Region:</b> <?php echo $mapResult['region']; ?><br />
    <b>City:</b> <?php echo $mapResult['city']; ?><br />
    <b>Timezone:</b> <?php echo $mapResult['timezone']; ?><br />

    <?php echo $mapResult['mapDiv']; ?>



Now, before you jump into this head first, wait! You need to get an API Key first. The good news is, you can get one by signing up here:

You also need a Google Maps API Key, which you can get here:

Finally, if you like this script, please let me know by filling out a comment below!

(better yet, blog it). Credit is appreciated!

Visit me on Google+

Leave a comment


rich commented on Dec 29th 2011

Awesome code