ANIL KUMAR PANIGRAHI 's Blog

We get the address by latitude and longitude -using php code

Posted by: Anil Kumar Panigrahi in: ● November 24, 2009

How did we get the complete address with using of longitude and latitude  using google map API and php code.

The following is may useful script to detect the address.

1) Signup for API key Google Map.

2) Follow the below php code:

<?php

if(isset($_GET['latitude']))
{
$latitude=$_GET['latitude'];
}
if(isset($_GET['longitude']))
{
$longitude=$_GET['longitude'];
}

$key= “Your API Key”;

Call the below function to get the address:

$data=translateLatLngtoAddress($latitude,$longitude,$key);

echo “<pre>”;
var_dump($data);
echo “</pre>”;

Function for get the data :

function translateLatLngtoAddress($lat,$long,$key)
{
$lat=mb_convert_encoding($lat, ‘UTF-8′,mb_detect_encoding($lat, ‘UTF-8, ISO-8859-1′, true));
$long=mb_convert_encoding($long, ‘UTF-8′,mb_detect_encoding($long, ‘UTF-8, ISO-8859-1′, true));

$url=”http://maps.google.com/maps/geo?q=$lat,$long&output=csv&sensor=false&key=”.$key;
$response = getGeolocation($url);

if (substr($response, 0, 3) === ‘200′) {
return $geo = explode(‘,’, $response);
}
return false;
}

Function is for get the location is getGeolocation:

function getGeolocation($url)
{

$init = curl_init();
curl_setopt($init, CURLOPT_URL, $url);
curl_setopt($init, CURLOPT_HEADER,0);
curl_setopt($init, CURLOPT_USERAGENT, $_SERVER["HTTP_USER_AGENT"]);
curl_setopt($init, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($init);
curl_close($init);
return $response;
}

Hope that  it may be useful.

Thank you,

 

Share on Twitter

5 Comments to "We get the address by latitude and longitude -using php code"

2 | Hashim

2 de December de 2009 to ● 10:35 am

Hi this code is not work

Warning: Wrong parameter count for mb_detect_encoding() in /opt/lampp/htdocs/map/map3.php on line 22

Warning: mb_convert_encoding() [function.mb-convert-encoding]: Unknown encoding “-8″ in /opt/lampp/htdocs/map/map3.php on line 22

Warning: Wrong parameter count for mb_detect_encoding() in /opt/lampp/htdocs/map/map3.php on line 23

Warning: mb_convert_encoding() [function.mb-convert-encoding]: Unknown encoding “-8″ in /opt/lampp/htdocs/map/map3.php on line 23

bool(false)

3 | Anil Kumar Panigrahi

8 de December de 2009 to ● 7:23 am

Hi Hashim,

Its working yaar, i place demo following url, we can find the details.

http://labs.anil2u.info/latlong.php?latitude=16.9662011&longitude=82.2243712

Thank you.

4 | Dharmveer Motyar

16 de December de 2009 to ● 12:51 pm

Useful but please write a post How to detect user’s country?
Thanx for your help anil.

5 | Joomla Developers

18 de January de 2010 to ● 6:18 am

The code was really helpful and easy to understand. Thanks Anil

Hi Dharmveer,

You can get the code for “How to detect user’s country” from:

http://www.phpclasses.org/browse/package/2363.html

Write Comment

About Me

  Anil Kumar Panigrahi
  Software Engineer (PHP)
  Blogger & Founder of Anil Labs
  Baruva, India

You can share with me . . .

My Own Contributions

1) First contribution for codeigniter framework ( multi language support using google translate API ). we can get files from http://github.com/nyros/codeigniter_multilanguage


2) Audio and Video Streaming using FFMpeg and PHP -complete doc file : http://www.docstoc.com/docs/8300349/Audio-and-Video-Streaming

Twitter Updates

Follow me @anil2u
  • http://www.google.co.uk/ logo is changing with mouse move . . . - posted on 07/09/2010 10:57:33
  • True enemies are real friends . . . - posted on 06/09/2010 10:15:29
  • Just watched nice movie " Madrasapattinam ". - posted on 05/09/2010 16:20:48
  • We will always be thankful to our teacher for all the hard work and efforts they have put in, for educating us.- Happy teachers Day . . . :) - posted on 05/09/2010 05:39:13
  • Working on shipping cost functionality (UPS and USPS) for oscommerce shopping cart - posted on 01/09/2010 09:40:03