Mapping the IP Address to Latitude and Longitude in Google Maps
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.
[sourcecode=”php”]
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);
?>
[/sourcecode]
[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.
[sourcecode=”html”]
Google Maps Plotting User By Refrence Of IP Address | Subesh.com.np