Mapping The IP Address to Latitude and Longitude In Google Maps
Posted by Bagesh Singh on April 7th, 2010
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.
05 |
Get the code for Real IP of the site from roshanbh.com.np |
06 |
Mapping The IP Address to Latitude and Longitude. |
08 |
function IPtoLatLng($ip) |
11 |
$dom = new DOMDocument(); |
12 |
$ipcheck = ip2long($ip); |
14 |
<span id="more-63"></span> |
16 |
if($ipcheck == -1 || $ipcheck === false){ |
17 |
echo "ERROR: INVALID IP"; |
25 |
$temp=explode(",",$coordinates); |
26 |
$latlngValue['LNG']=$temp[0]; |
27 |
$latlngValue['LAT']=$temp[1]; |
28 |
$latlngValue['NAME']=$name; |
32 |
//calling the functions and setting on array $latlng |
33 |
$IP=getRealIpAddr(); //from roshanbh.com.np |
34 |
$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.
04 |
<meta http-equiv="content-type" content="text/html; charset=utf-8"/> |
05 |
<title>Google Maps Plotting User By Refrence Of IP Address | Subesh.com.np</title> |
08 |
type="text/javascript"></script> |
09 |
<script type="text/javascript"> |
12 |
var iconBlue = new GIcon(); |
15 |
iconBlue.iconSize = new GSize(12, 20); |
16 |
iconBlue.shadowSize = new GSize(22, 20); |
17 |
iconBlue.iconAnchor = new GPoint(6, 20); |
18 |
iconBlue.infoWindowAnchor = new GPoint(5, 1); |
20 |
var point = new GLatLng(parseFloat('<?=$latlng['LAT']?>'),parseFloat('<?=$latlng['LNG']?>')); |
23 |
if (GBrowserIsCompatible()) { |
24 |
var map = new GMap2(document.getElementById("map")); |
25 |
map.setCenter(new GLatLng(parseFloat('<?=$latlng['LAT']?>'),parseFloat('<?=$latlng['LNG']?>')),3); |
26 |
var marker = createMarker(point); |
27 |
map.addOverlay(marker); |
31 |
function createMarker(point) { |
32 |
var marker = new GMarker(point, iconBlue); |
33 |
var html = "<h1>Gothcha! HA HA </h1> <br />You Are Here !<br /><b><?=$latlng['NAME']?></b> "; |
34 |
GEvent.addListener(marker, 'mouseover', function() { |
35 |
marker.openInfoWindowHtml(html); |
43 |
<body onload="load()" onunload="GUnload()"> |
44 |
<h1>Subesh.com.np: Maps Example</h1> |
45 |
<div id="map" style="width: 500px; height: 300px"></div> |
Both comments and pings are currently closed.