Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何从反向地理编码JSON结果中获取格式化地址?_Php_Json_Google Maps_Geocoding_Reverse Geocoding - Fatal编程技术网

Php 如何从反向地理编码JSON结果中获取格式化地址?

Php 如何从反向地理编码JSON结果中获取格式化地址?,php,json,google-maps,geocoding,reverse-geocoding,Php,Json,Google Maps,Geocoding,Reverse Geocoding,这是JSON输出:我只想得到格式化的地址值&long\u name&short\u name。我提到了人们已经问过的问题,但我不明白。 参考: 我的PHP函数用于获取给定lat和lon的反向地理编码 public function get_location() { $this->location = $param; $location = file_get_contents('http://maps.googleapis.com/maps/api

这是JSON输出:我只想得到格式化的地址值&long\u name&short\u name。我提到了人们已经问过的问题,但我不明白。 参考:

我的PHP函数用于获取给定lat和lon的反向地理编码

public function get_location()
    {
        $this->location = $param;
         $location = file_get_contents('http://maps.googleapis.com/maps/api/geocode/json?latlng=2.993518,101.7874058&sensor=true');

         return $location;
    }
我试着这样解析:

$search = new search();
$data = $search->get_location();//caries the JSON data
$return['json']= json_encode($data);
$data = json_decode($return['json'], true);

但是如何以长名称、格式化地址等为目标?

以下示例演示如何在PHP中解析和访问结果
long\u name
&
short\u name
属性:

$json = file_get_contents('http://maps.googleapis.com/maps/api/geocode/json?latlng=2.993518,101.7874058&sensor=true');
$data = json_decode($json, true);


foreach ($data['results'] as $item) {
    if( !empty( $item['address_components'] ) ){
        $longName = $item['address_components'][0]['long_name']; 
        $shortName = $item['address_components'][0]['short_name']; 

        print_r('Long Name:' . $longName);
        print_r(' Short Name:' . $shortName);
    }

}

首先,您可以在此处阅读有关地理编码器结果类型的所有信息->

为了方便起见,我会将JSON转换为
stdClass

$location=json\u decode($location);
然后像这样解析它:

$geoResults=[];
foreach($location->results as$result){
$Georgesult=[];
foreach($result->address\u组件作为$address){
如果($address->types[0]=='country'){
$Georgesult['country']=$address->long_name;
}
如果($address->types[0]=='Administration\u area\u level\u 1'){
$geoResult['state']=$address->long\u name;
}
如果($address->types[0]==“管理区\级别\ 2”){
$Georgesult['county']=$address->long_name;
}
如果($address->types[0]=='locality'){
$Georgesult['city']=$address->long_name;
}
如果($address->types[0]=='邮政编码'){
$Georgesult['postal_code']=$address->long_name;
}       
如果($address->types[0]=='route'){
$geoResult['route']=$address->long\u name;
}       
}
$geoResults[]=$geoResult;
}
现在您有了一个带有命名属性的
$geoResults
数组:

数组
(
[0]=>阵列
(
[路线]=>蔡松凯
[城市]=>Kajang
[州]=>雪兰莪州
[国家]=>马来西亚
[邮政编码]=>43000
)
[1] =>阵列
(
[城市]=>Kajang
[州]=>雪兰莪州
[国家]=>马来西亚
[邮政编码]=>43000
)
[2] =>阵列
(
[城市]=>Kajang
[州]=>雪兰莪州
[国家]=>马来西亚
[邮政编码]=>43000
)
[3] =>阵列
(
[城市]=>Kajang
[州]=>雪兰莪州
[国家]=>马来西亚
)
[4] =>阵列
(
[州]=>雪兰莪州
[国家]=>马来西亚
)
[5] =>阵列
(
[邮政编码]=>43000
[州]=>雪兰莪州
[国家]=>马来西亚
)
[6] =>阵列
(
[州]=>雪兰莪州
[国家]=>马来西亚
)
[7] =>阵列
(
[州]=>雪兰莪州
[国家]=>马来西亚
)
[8] =>阵列
(
[国家]=>马来西亚
)
)
您可以像这样迭代所有找到的城市:

foreach($geoResults as$result){
echo isset($result['city'])?$result['city']:'N/A';
回声“
”; }
是解析JSON给您带来了问题吗?
$json = file_get_contents('http://maps.googleapis.com/maps/api/geocode/json?latlng=2.993518,101.7874058&sensor=true');
$data = json_decode($json, true);


foreach ($data['results'] as $item) {
    if( !empty( $item['address_components'] ) ){
        $longName = $item['address_components'][0]['long_name']; 
        $shortName = $item['address_components'][0]['short_name']; 

        print_r('Long Name:' . $longName);
        print_r(' Short Name:' . $shortName);
    }

}