Google地图-从经度和纬度(PHP)获取国家,地区和城市名称 [英] Google maps - get country, region and city name from longitude and latitude (PHP)

查看:94
本文介绍了Google地图-从经度和纬度(PHP)获取国家,地区和城市名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过PHP通过Google Maps API(V3?)从经度和纬度中获得国家,地区和城市的名称.我只是无法设法在每个县都得到它,我设法得到了这段代码,但是它不能在其他县工作,因为返回的结果是不同的(唯一正确的信息是国家/地区,但即使返回的也不是当地名称)英文名称):

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...

推荐答案

您可以像这样使用第二个api(由Thomas建议):

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;

尝试一下.

::::

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

这篇关于Google地图-从经度和纬度(PHP)获取国家,地区和城市名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆