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

9

我希望通过Google Maps API(V3?)获取经纬度所在的国家、地区和城市名称,并使用PHP进行翻译。但我无法为每个县获取到正确的结果,虽然我已经编写了以下代码,但其对于其他县不起作用,因为返回的结果不同(唯一正确的信息是国家,但即使是这个信息也是以本地语言而非英语返回的):

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

这个方法可以很好地获取我国的正确数据,但对其他国家不适用...


哪些坐标对你来说不起作用? - cloakedninjas
它对我来说完美无缺。然而,我的应用程序只在印度使用它。 - srbhbarot
你应该提供更多关于你尝试过的坐标的信息。 - srbhbarot
我已经尝试过,它给出的地址是:Burgenlandstraße 14-16,4020林茨,奥地利。 - srbhbarot
你好,你解决问题了吗? - srbhbarot
显示剩余2条评论
6个回答

25

你可以像Thomas建议的那样使用第二个API:

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

这个可以运行,但数据不是我需要的。我可以从这里获取国家和地区(但地区也包含邮政编码),但无法获取城市 - 只有地址。我该如何获取国家、城市/村庄名称和地区(不带邮政编码)? - Martha Smithers

8
这是我的解决方案。
$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;

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

如果你需要查找城市名称,那么只需将代码中的 in_array("country", $cn) 替换为 in_array("locality", $cn)。如果你需要查找邮政编码,则将 in_array("country", $cn) 替换为 in_array("postal_code", $cn) 即可。这是一个简单的代码。 - Harsh Patel

1

你能帮我用PHP从中获取国家、城市和地区吗? - Martha Smithers

0

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

将城市与GPS X和Y坐标放入文本文件中并从中提取。所有这些都可以在线获取。


0

如果你使用 Laravel,那么它对你会很有用。

    $latitude=26.12312;
    $longitude=83.77823;
    $geolocation = $latitude.','.$longitude;
    $response = \Http::get('https://maps.googleapis.com/maps/api/geocode/json', [
        'latlng' => $geolocation,
        'key' => 'api key',
        'sensor'    =>  false
    ]);
    $json_decode = json_decode($response);
    if(isset($json_decode->results[0])) {
        $response = array();
        foreach($json_decode->results[0]->address_components as $addressComponet) {
            $response[] = $addressComponet->long_name;
        }
        dd($response);
    }

请使用您自己的 API 密钥替换此处的密钥。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接