2

我有一个纬度和一个经度,我需要获取国家/地区。

我正在使用这个:

$geocode_stats = file_get_contents("http://maps.googleapis.com/maps/api/geocode/json?latlng=".$deal_lat.",".$deal_long . "&sensor=false");
$output_deals = json_decode($geocode_stats);
$country = $output_deals->results[2]->address_components[4]->long_name;

有时它给出一个正确的国家名称,但有时它给出空白值,有时它返回一个城市名称。

有人可以帮忙吗?

4

6 回答 6

6

我写了一个函数来简化它。只需传入您的地理地址,该地址可以是完整地址、邮政编码,或者在您的情况下是纬度和经度。然后它将在地址组件数组中搜索国家。如果无法找到县,它将简单地返回 null,如果需要,您可以将其更改为空字符串 ("")。

function getGeoCounty($geoAddress) {
    $url = 'http://maps.google.com/maps/api/geocode/json?address=' . $geoAddress .'&sensor=false'; 
    $get     = file_get_contents($url);
    $geoData = json_decode($get);
    if (json_last_error() !== JSON_ERROR_NONE) {
        throw new \InvalidArgumentException('Invalid geocoding results');
    }

    if(isset($geoData->results[0])) {
        foreach($geoData->results[0]->address_components as $addressComponent) {
            if(in_array('administrative_area_level_2', $addressComponent->types)) {
                return $addressComponent->long_name; 
            }
        }
    }
    return null; 
}
于 2013-03-11T16:14:36.130 回答
3

从纬度和经度获取完整的地址。

$url = 'http://maps.googleapis.com/maps/api/geocode/json?latlng='.trim($lat).','.trim($lng).'&sensor=false';
$json = @file_get_contents($url);$data=json_decode($json);
echo $data->results[0]->formatted_address;
于 2014-11-26T08:25:51.633 回答
2
$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;
于 2015-08-05T10:38:37.003 回答
1
$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;
于 2015-08-05T10:26:28.013 回答
0

I did take the code of matwonk as base.

function getGeoCounty($geoAddress) {
    $url = 'http://maps.google.com/maps/api/geocode/json?address=' . urlencode($geoAddress) .'&sensor=false'; 
    $get     = file_get_contents($url);
    $geoData = json_decode($get);
    if(isset($geoData->results[0])) {
        $return = array();
        foreach($geoData->results[0]->address_components as $addressComponet) {
            if(in_array('political', $addressComponet->types)) {
                if($addressComponet->short_name != $addressComponet->long_name)
                    $return[] = $addressComponet->short_name. " - " . $addressComponet->long_name; 
                else
                    $return[] = $addressComponet->long_name; 
            }
        }
        return implode(", ",$return);
    }
    return null;
}

The code return in format: neighborhood, City, State, Country

if detect a shortname (as country code or state code) appear in format CODE - NAME.

于 2013-05-10T21:26:22.827 回答
0
        $latlng = '22.7314,75.88243';
        $request_url = "http://maps.googleapis.com/maps/api/geocode /xml?latlng=".$latlng."&sensor=true";
        $xml = simplexml_load_file($request_url);

        if($xml->status == "OK") {
        $address = $xml->result->formatted_address;
          foreach ($xml->result->address_component as $address) {
            如果(“国家”==修剪($地址->类型)){
              $country = $address->short_name;
            }
          }
        }

于 2017-08-23T13:51:36.767 回答