Post your trick

How to get Latitude and Longitude from IP address

After searching for more than four to five hours i got the link which give latitude, longitude and other information such as address including city and country. So here is a small function which will give you latitude and longitude of the given ip address.

function getLatLng($ip){

//get the contents from the site by file_get_contents.
$sXML = @file_get_contents(’http://ipinfodb.com/ip_query.php?ip=’.$ip.’&timezone=false’);

//make the response in a xml object so we can parse it.
$oXML = @new SimpleXMLElement($sXML);

$array = array(
‘Latitude’ => (string) $oXML->Latitude[0],
‘Longitude’ => (string) $oXML->Longitude[0]
);
return $array;
}


2 Responses to “How to get Latitude and Longitude from IP address”
  • Nilesh February 4th, 2011 at 2:10 pm

    Please check this code its fack


  • admin February 19th, 2011 at 1:16 pm

    Hi Nilesh,

    The code is right but the site (www.ipinfodb.com) has changed the api to get the latitude and longitude use the following code now on

    $sXML = @file_get_contents(’http://api.ipinfodb.com/v2/ip_query.php?key=your_api_key&ip=’.$ip);

    for the above code you will have to register with the site. There you will get an api key. Replace ‘your_api_key’ with the api key you get.

    Hope it will work fine now. If you get any problem get back to me :) .


Leave a Reply

     

     

     

Post A Trick !

How does "Kodetricks" work?

We at kodeplay like to share knowledge. With Kodetricks, even you can join us. All you need to do is post a programming related trick if you have one or rate a trick if you like someone else's.



Technology

Contact Us