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.

01 <?php
02 /*
03 Subesh Pokhrel
04 subesh.com.np
05 Get the code for Real IP of the site from roshanbh.com.np
06 Mapping The IP Address to Latitude and Longitude.
07 */
08 function IPtoLatLng($ip)
09 {
10 $latlngValue=array();
11 $dom = new DOMDocument();
12 $ipcheck = ip2long($ip);
13
14 <span id="more-63"></span>
15
16 if($ipcheck == -1 || $ipcheck === false){
17 echo "ERROR: INVALID IP";
18 exit;
19 }
20 else
21 $uri = "http://api.hostip.info/?ip=$ip&position=true";
22 $dom->load($uri);
23 $name=$dom->getElementsByTagNameNS('http://www.opengis.net/gml','name')->item(1)->nodeValue;
24 $coordinates=$dom->getElementsByTagNameNS('http://www.opengis.net/gml','coordinates')->item(0)->nodeValue;
25 $temp=explode(",",$coordinates);
26 $latlngValue['LNG']=$temp[0];
27 $latlngValue['LAT']=$temp[1];
28 $latlngValue['NAME']=$name;
29 return $latlngValue;
30
31 }
32 //calling the functions and setting on array $latlng
33 $IP=getRealIpAddr(); //from roshanbh.com.np
34 $latlng=IPtoLatLng($IP);
35 ?>

[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.

01 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
02 <html xmlns="http://www.w3.org/1999/xhtml">
03 <head>
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>
06
08 type="text/javascript"></script>
09 <script type="text/javascript">
10 //<![CDATA[
11
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);
19
20 var point = new GLatLng(parseFloat('<?=$latlng['LAT']?>'),parseFloat('<?=$latlng['LNG']?>'));
21
22 function load() {
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);
28 }
29 }
30
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);
36 });
37 return marker;
38 }
39 //]]>
40
41 </script>
42 </head>
43 <body onload="load()" onunload="GUnload()">
44 <h1>Subesh.com.np: Maps Example</h1>
45 <div id="map" style="width: 500px; height: 300px"></div>
46 </body>
47 </html>
Both comments and pings are currently closed.

Comments are closed.

Powered by WordPress | iCellPhoneDeals.com Offers Free Wireless Deals. | Thanks to Bestincellphones.com Verizon Cell Phones, Best CD Rates Online and Fat Burning Furnace Review
Php Programmer Bagesh Singh