Jul
Mapping The IP Address to Latitude and Longitude In Google Maps
Filed Under (Google Maps, PHP) by Subesh Pokhrel on 11-07-2008
Tagged Under : Google Maps, ip, latitude, longitude, PHP
At last I found hostip.info provides a service for getting the latitude and longitude of a place on the basis of the user’s ip address. Its light weight and free, rather than downloading bulky database and getting paid services, for getting latitude and longitude on the basis of IP.
Here is the simplest way to do it. Requested to http://api.hostip.info/?ip=$ip&position=true it responds XML formatted output. So we need to parse XML code using DOM class of PHP.(#19-#23). Remember, the position=true should be set to get the latitude and longitude otherwise it will not show on default.
Here is the code and I will soon upload the working demo.
<?php
/*
Subesh Pokhrel
subesh.com.np
Get the code for Real IP of the site from roshanbh.com.np
Mapping The IP Address to Latitude and Longitude.
*/
function IPtoLatLng($ip)
{
$latlngValue=array();
$dom = new DOMDocument();
$ipcheck = ip2long($ip);<span id="more-63"></span>
if($ipcheck == -1 || $ipcheck === false){
echo "ERROR: INVALID IP";
exit;
}
else
$uri = "http://api.hostip.info/?ip=$ip&position=true";
$dom->load($uri);
$name=$dom->getElementsByTagNameNS('http://www.opengis.net/gml','name')->item(1)->nodeValue;
$coordinates=$dom->getElementsByTagNameNS('http://www.opengis.net/gml','coordinates')->item(0)->nodeValue;
$temp=explode(",",$coordinates);
$latlngValue['LNG']=$temp[0];
$latlngValue['LAT']=$temp[1];
$latlngValue['NAME']=$name;
return $latlngValue;
}
//calling the functions and setting on array $latlng
$IP=getRealIpAddr(); //from roshanbh.com.np
$latlng=IPtoLatLng($IP);
?>
[UPDATE] [DOWNLOAD]
Due to high request of the visitors I have posted the whole source code here.. just copy the PHP part above anywhere the script part and get the IP code from http://roshanbh.com.np/2007/12/getting-real-ip-address-in-php.html and run. Remember you need PHP5, because the DOM used is of PHP5.
<!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 Plotting User By Refrence Of IP Address | Subesh.com.np</title>
<script src="http://maps.google.com/maps?file=api&v=2&key=ABQIAAAAPHLcOOGHX2-uLk3K8q1nMRSFS3Iba-Yq79Uuy07kI14Q-o9MOhQTADZYj-oIAYvmRBVx8i9NqeBSjg"
type="text/javascript"></script>
<script type="text/javascript">
//<![CDATA[
var iconBlue = new GIcon();
iconBlue.image = 'http://labs.google.com/ridefinder/images/mm_20_blue.png';
iconBlue.shadow = 'http://labs.google.com/ridefinder/images/mm_20_shadow.png';
iconBlue.iconSize = new GSize(12, 20);
iconBlue.shadowSize = new GSize(22, 20);
iconBlue.iconAnchor = new GPoint(6, 20);
iconBlue.infoWindowAnchor = new GPoint(5, 1);
var point = new GLatLng(parseFloat('<?=$latlng['LAT']?>'),parseFloat('<?=$latlng['LNG']?>'));
function load() {
if (GBrowserIsCompatible()) {
var map = new GMap2(document.getElementById("map"));
map.setCenter(new GLatLng(parseFloat('<?=$latlng['LAT']?>'),parseFloat('<?=$latlng['LNG']?>')),3);
var marker = createMarker(point);
map.addOverlay(marker);
}
}
function createMarker(point) {
var marker = new GMarker(point, iconBlue);
var html = "<h1>Gothcha! HA HA </h1> <br>You Are Here !<br><b><?=$latlng['NAME']?></b> ";
GEvent.addListener(marker, 'mouseover', function() {
marker.openInfoWindowHtml(html);
});
return marker;
}
//]]>
</script>
</head>
<body onload="load()" onunload="GUnload()">
<h1>Subesh.com.np: Maps Example</h1>
<div id="map" style="width: 500px; height: 300px"></div>
</body>
</html>
![[del.icio.us]](http://subesh.com.np/wp-content/plugins/bookmarkify/delicious.png)
![[Digg]](http://subesh.com.np/wp-content/plugins/bookmarkify/digg.png)
![[Facebook]](http://subesh.com.np/wp-content/plugins/bookmarkify/facebook.png)
![[Google]](http://subesh.com.np/wp-content/plugins/bookmarkify/google.png)
![[MySpace]](http://subesh.com.np/wp-content/plugins/bookmarkify/myspace.png)
![[StumbleUpon]](http://subesh.com.np/wp-content/plugins/bookmarkify/stumbleupon.png)
![[Technorati]](http://subesh.com.np/wp-content/plugins/bookmarkify/technorati.png)
![[Windows Live]](http://subesh.com.np/wp-content/plugins/bookmarkify/windowslive.png)
![[Yahoo!]](http://subesh.com.np/wp-content/plugins/bookmarkify/yahoo.png)


This is a very handy way of adding extra security to your web applications. I’m going to implement this into my website just for that extra security. Although, an IP can be spoofed, this is a start.
Thanks for the article.
- Dwayne Charrington.
great job….but I feel their api is a bit unreliable, it didn’t work for the IP address 202.79.35.151 - which is kathmandu …. Kathmandu seems to be unknown city from them
May be.. but this is the easiest and weightless way to get the latitude and longitude and it depends upon the service provided by hostip.info. If you want professional service there are other ways available.
great job ! i will try this . thanks.
I sure could use this, to build the “entertainment” part of our school’s website.
A working demo would be really great.
Thank you for your work.
Subesh,
Excellent find. This definitely a easy and light weight solution. But, I don’t think we can expect this to locate every city as Roshan has mentioned.
very useful information shared.. keep it up subesh !!!