谷歌地图 - 从经度和纬度获取国家,地区和城市名称(PHP)

时间:2012-04-03 10:14:04

标签: php json google-maps

我想通过Google地图API(V3?)从经度和纬度获取国家,地区和城市名称。我只是无法为每个县获得它,我设法得到这个代码,但它不适用于其他县,因为返回的结果是不同的(唯一正确的信息是国家,但即使是以本地名称返回而不是英文名称):

<?
    $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;
?>

这非常适合从我的国家获取正确的数据,但不适用于其他人......

5 个答案:

答案 0 :(得分:25)

您可以使用第二个api(Thomas建议):

$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;

试试这个..

编辑:::::

$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/>';
            }

答案 1 :(得分:7)

以下是我解决问题的方法

$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;

答案 2 :(得分:6)

<?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;
?>

答案 3 :(得分:1)

您是否尝试过使用GeoCoding API?

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

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

编辑:我在许多针对印度的应用中使用过它并且效果非常好。

答案 4 :(得分:0)

总是依赖谷歌真的很愚蠢。

将城市放入带有 GPS x 和 Y 坐标的文本文件中,然后从那里提取。所有这些都可以在线获得

相关问题