Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google maps - get country, region and city name from longitude and latitude (PHP)

I would like to get country, region and city name from longitude and latitude over Google maps API (V3?) - with PHP. I just can't manage to get it for every county, I managed to get this code but it doesn't work on other counties because the returned result is different (the only correct information is country but even that is returned in local name not English name):

<?
    $lok1 = $_REQUEST['lok1'];
    $lok2 = $_REQUEST['lok2'];

    $url = 'http://maps.google.com/maps/geo?q='.$lok2.','.$lok1.'&output=json';
    $data = @file_get_contents($url);
    $jsondata = json_decode($data,true);

    if(is_array($jsondata )&& $jsondata ['Status']['code']==200)
    {
          $addr = $jsondata ['Placemark'][0]['AddressDetails']['Country']['CountryName'];
          $addr2 = $jsondata ['Placemark'][0]['AddressDetails']['Country']['Locality']['LocalityName'];
          $addr3 = $jsondata ['Placemark'][0]['AddressDetails']['Country']['Locality']['DependentLocality']['DependentLocalityName'];
    }
    echo "Country: " . $addr . " | Region: " . $addr2 . " | City: " . $addr3;
?>

This works great to get correct data from my country, but not for others...

like image 539
Martha Smithers Avatar asked Apr 03 '12 10:04

Martha Smithers


People also ask

How do I get my city name in Google Places API?

2) Make another web-service call to https://maps.googleapis.com/maps/api/place/details/json?key=API_KEY&placeid=place_id_retrieved_in_step_1. This will return a JSON which contains address_components . Looping through the types to find locality and postal_code can give you the city name and postal code.


4 Answers

You can use second api(suggested by Thomas) like this:

$geocode=file_get_contents('http://maps.googleapis.com/maps/api/geocode/json?latlng=48.283273,14.295041&sensor=false');

        $output= json_decode($geocode);

echo $output->results[0]->formatted_address;

Try this..

Edit:::::

$geocode=file_get_contents('http://maps.googleapis.com/maps/api/geocode/json?latlng=48.283273,14.295041&sensor=false');

        $output= json_decode($geocode);

    for($j=0;$j<count($output->results[0]->address_components);$j++){
                echo '<b>'.$output->results[0]->address_components[$j]->types[0].': </b>  '.$output->results[0]->address_components[$j]->long_name.'<br/>';
            }
like image 139
srbhbarot Avatar answered Oct 01 '22 00:10

srbhbarot


Here is my approach to the problem

$lat = "22.719569";
$lng = "75.857726";
$data = file_get_contents("http://maps.google.com/maps/api/geocode/json?latlng=$lat,$lng&sensor=false");
$data = json_decode($data);
$add_array  = $data->results;
$add_array = $add_array[0];
$add_array = $add_array->address_components;
$country = "Not found";
$state = "Not found"; 
$city = "Not found";
foreach ($add_array as $key) {
  if($key->types[0] == 'administrative_area_level_2')
  {
    $city = $key->long_name;
  }
  if($key->types[0] == 'administrative_area_level_1')
  {
    $state = $key->long_name;
  }
  if($key->types[0] == 'country')
  {
    $country = $key->long_name;
  }
}
echo "Country : ".$country." ,State : ".$state." ,City : ".$city;
like image 29
Chandrika Shah Avatar answered Oct 01 '22 01:10

Chandrika Shah


<?php
$deal_lat=30.469301;
$deal_long=70.969324;
$geocode=file_get_contents('http://maps.googleapis.com/maps/api/geocode/json?latlng='.$deal_lat.','.$deal_long.'&sensor=false');

        $output= json_decode($geocode);

    for($j=0;$j<count($output->results[0]->address_components);$j++){
               $cn=array($output->results[0]->address_components[$j]->types[0]);
           if(in_array("country", $cn))
           {
            $country= $output->results[0]->address_components[$j]->long_name;
           }
            }
            echo $country;
?>
like image 29
Harsh Patel Avatar answered Oct 01 '22 01:10

Harsh Patel


Have you tried using their GeoCoding API?

https://developers.google.com/maps/documentation/geocoding/#ReverseGeocoding

Example: http://maps.googleapis.com/maps/api/geocode/json?latlng=40.714224,-73.961452&sensor=false

Edit: I have used it in many apps targeting India and it has worked pretty well.

like image 33
Thomas Antony Avatar answered Sep 30 '22 23:09

Thomas Antony