php - Google map - 从经度和纬度获取国家、地区和城市名称(PHP)

标签 php json google-maps

我想通过 Google map 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;
?>

这对于从我的国家/地区获取正确的数据非常有效,但对其他国家/地区则不然...

最佳答案

您可以像这样使用第二个 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/>';
            }

关于php - Google map - 从经度和纬度获取国家、地区和城市名称(PHP),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9991224/

相关文章:

PHPMailer 根本不工作

php - key PRIMARY 的重复条目 IMG002

php - wp_get_current_user null 如果我直接在某些 PHP 文件中访问它

json - BigQuery json 导出使用什么格式?

javascript - iOS 混合应用程序 - Google map API 错误 : RefererNotAllowedMapError

地址具有哈希时的R地理编码查询错误

php 单例每个对象具有不同的标识符

Python:帮助将循环 a 中凌乱的 if-elif 语句阶梯转换为字典 switch 案例以创建 grpc ssl 证书

c# - MAC 导致 AESGCM 解密失败

android - 如何在折线上显示持续时间弹出窗口?