How to get latitude and longitude from google map using PHP
For Pointing the exact location on Google Map it will always be a good idea to pass the latitude and longitude with Google Maps API. In this article, you’ll know how to get latitude and longitude from address using Google Maps API in PHP. Here we’ll combine all code into a PHP function and you only need to use this function to find latitude and longitude from an address. You may also like Pick up Location and Drop Location line draw in google map API using javascript and How to Get User Location Using IP Address In PHP.
Code
$address = 'surat'." ".'gujarat'." ".'india' ;
$Addr = str_replace(' ','+',$address);
$geocode = file_get_contents('http://maps.googleapis.com/maps/api/geocode/json?address='.$Addr.'&sensor=false');
$output = json_decode($geocode);
$data['latitude'] = $output->results[0]->geometry->location->lat;
$data['longitude'] = $output->results[0]->geometry->location->lng;
print_r($data);
die;
Output
Array ( [latitude] => 22.3038945 [longitude] => 70.8021599 )
Other Code
$address = 'surat'." ".'gujarat'." ".'india' ;
$Addr = str_replace(' ','+',$address);
$geocode = file_get_contents('http://maps.googleapis.com/maps/api/geocode/json?address='.$Addr.'&sensor=false');
$output = json_decode($geocode);
echo "<pre>";
print_r($output);
die;
Output
stdClass Object
(
[results] => Array
(
[0] => stdClass Object
(
[address_components] => Array
(
[0] => stdClass Object
(
[long_name] => Gujarat
[short_name] => GJ
[types] => Array
(
[0] => administrative_area_level_1
[1] => political
)
)
[1] => stdClass Object
(
[long_name] => India
[short_name] => IN
[types] => Array
(
[0] => country
[1] => political
)
)
)
[formatted_address] => Gujarat, India
[geometry] => stdClass Object
(
[bounds] => stdClass Object
(
[northeast] => stdClass Object
(
[lat] => 24.705709
[lng] => 74.4764881
)
[southwest] => stdClass Object
(
[lat] => 20.127954
[lng] => 68.162834
)
)
[location] => stdClass Object
(
[lat] => 22.258652
[lng] => 71.1923805
)
[location_type] => APPROXIMATE
[viewport] => stdClass Object
(
[northeast] => stdClass Object
(
[lat] => 24.705709
[lng] => 74.4764881
)
[southwest] => stdClass Object
(
[lat] => 20.127954
[lng] => 68.162834
)
)
)
[place_id] => ChIJlfcOXx8FWTkRLlJU7YfYG4Y
[types] => Array
(
[0] => administrative_area_level_1
[1] => political
)
)
)
[status] => OK
)
Leave a Reply