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

How to get formatted address from reverse geocoding JSON result?

这是 JSON 输出:我只想获取 formatted_address 值 & long_name & short_name。我提到了人们已经问过的问题,但我没弄对。 参考:

get city from geocoder results?

{
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "Jalan Sungai Chua",
               "short_name" : "B11",
               "types" : [ "route" ]
            },
            {
               "long_name" : "Sungai Chua",
               "short_name" : "Sungai Chua",
               "types" : [ "sublocality_level_1", "sublocality", "political" ]
            },
            {
               "long_name" : "Kajang",
               "short_name" : "Kajang",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Selangor",
               "short_name" : "Selangor",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "Malaysia",
               "short_name" : "MY",
               "types" : [ "country", "political" ]
            },
            {
               "long_name" : "43000",
               "short_name" : "43000",
               "types" : [ "postal_code" ]
            }
         ],
         "formatted_address" : "Jalan Sungai Chua, Sungai Chua, 43000 Kajang, Selangor, Malaysia",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 2.9932816,
                  "lng" : 101.787477
               },
               "southwest" : {
                  "lat" : 2.9932814,
                  "lng" : 101.7873144
               }
            },
            "location" : {
               "lat" : 2.9932815,
               "lng" : 101.7873957
            },
            "location_type" : "GEOMETRIC_CENTER",
            "viewport" : {
               "northeast" : {
                  "lat" : 2.994630480291502,
                  "lng" : 101.7887446802915
               },
               "southwest" : {
                  "lat" : 2.991932519708498,
                  "lng" : 101.7860467197085
               }
            }
         },
         "place_id" : "ChIJcSMkCHjLzTERU6wXGzYy14I",
         "types" : [ "route" ]
      },
      {
         "address_components" : [
            {
               "long_name" : "Petronas Jalan Enggang",
               "short_name" : "Petronas Jalan Enggang",
               "types" : [ "point_of_interest", "establishment" ]
            },
            {
               "long_name" : "Kajang",
               "short_name" : "Kajang",
               "types" : [ "sublocality_level_1", "sublocality", "political" ]
            },
            {
               "long_name" : "Kajang",
               "short_name" : "Kajang",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Selangor",
               "short_name" : "Selangor",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "Malaysia",
               "short_name" : "MY",
               "types" : [ "country", "political" ]
            },
            {
               "long_name" : "43000",
               "short_name" : "43000",
               "types" : [ "postal_code" ]
            }
         ],
         "formatted_address" : "Petronas Jalan Enggang, Kajang, 43000 Kajang, Selangor, Malaysia",
         "geometry" : {
            "location" : {
               "lat" : 2.994144,
               "lng" : 101.78781
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 2.995492980291502,
                  "lng" : 101.7891589802915
               },
               "southwest" : {
                  "lat" : 2.992795019708498,
                  "lng" : 101.7864610197085
               }
            }
         },
         "place_id" : "ChIJN5uCE3jLzTERpHUSvJFj5To",
         "types" : [
            "bus_station",
            "transit_station",
            "point_of_interest",
            "establishment"
         ]
      },
      {
         "address_components" : [
            {
               "long_name" : "Kajang",
               "short_name" : "Kajang",
               "types" : [ "sublocality_level_1", "sublocality", "political" ]
            },
            {
               "long_name" : "Kajang",
               "short_name" : "Kajang",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Kajang",
               "short_name" : "Kajang",
               "types" : [ "administrative_area_level_4", "political" ]
            },
            {
               "long_name" : "Hulu Langat",
               "short_name" : "Hulu Langat",
               "types" : [ "administrative_area_level_3", "political" ]
            },
            {
               "long_name" : "Selangor",
               "short_name" : "Selangor",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "Malaysia",
               "short_name" : "MY",
               "types" : [ "country", "political" ]
            },
            {
               "long_name" : "43000",
               "short_name" : "43000",
               "types" : [ "postal_code" ]
            }
         ],
         "formatted_address" : "Kajang, 43000 Kajang, Selangor, Malaysia",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 2.9980711,
                  "lng" : 101.79819
               },
               "southwest" : {
                  "lat" : 2.986342,
                  "lng" : 101.7843368
               }
            },
            "location" : {
               "lat" : 2.9919972,
               "lng" : 101.7908387
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 2.9980711,
                  "lng" : 101.79819
               },
               "southwest" : {
                  "lat" : 2.986342,
                  "lng" : 101.7843368
               }
            }
         },
         "place_id" : "ChIJsfCmp3jLzTERJuiZjvHEHSA",
         "types" : [ "sublocality_level_1", "sublocality", "political" ]
      },
      {
         "address_components" : [
            {
               "long_name" : "Kajang",
               "short_name" : "Kajang",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Hulu Langat",
               "short_name" : "Hulu Langat",
               "types" : [ "administrative_area_level_3", "political" ]
            },
            {
               "long_name" : "Selangor",
               "short_name" : "Selangor",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "Malaysia",
               "short_name" : "MY",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "Kajang, Selangor, Malaysia",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 3.069515,
                  "lng" : 101.8336388
               },
               "southwest" : {
                  "lat" : 2.8684469,
                  "lng" : 101.69742
               }
            },
            "location" : {
               "lat" : 2.993518,
               "lng" : 101.7874058
            },

MY PHP 函数获取给定纬度和经度的反向地理编码

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

但是如何定位long_name、格式化地址等等?

以下示例演示如何解析和访问 Geocoding Service 结果 long_name & short_name PHP 中的属性:

$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);
    }

}

PhpFiddle

首先,您可以在此处阅读有关地理编码器结果类型的所有信息 -> https://developers.google.com/maps/documentation/geocoding/intro

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

$location = json_decode($location);

然后像这样解析它:

$geoResults = [];
foreach($location->results as $result){
    $geoResult = [];    
    foreach ($result->address_components as $address) {
        if ($address->types[0] == 'country') {
            $geoResult['country'] = $address->long_name;
        }
        if ($address->types[0] == 'administrative_area_level_1') {
            $geoResult['state'] = $address->long_name;
        }
        if ($address->types[0] == 'administrative_area_level_2') {
            $geoResult['county'] = $address->long_name;
        }
        if ($address->types[0] == 'locality') {
            $geoResult['city'] = $address->long_name;
        }
        if ($address->types[0] == 'postal_code') {
            $geoResult['postal_code'] = $address->long_name;
        }       
        if ($address->types[0] == 'route') {
            $geoResult['route'] = $address->long_name;
        }       
    }
    $geoResults[] = $geoResult;
}

现在您有一个 $geoResults 数组,其命名属性为:

Array
(
    [0] => Array
        (
            [route] => Jalan Sungai Chua
            [city] => Kajang
            [state] => Selangor
            [country] => Malaysia
            [postal_code] => 43000
        )

    [1] => Array
        (
            [city] => Kajang
            [state] => Selangor
            [country] => Malaysia
            [postal_code] => 43000
        )

    [2] => Array
        (
            [city] => Kajang
            [state] => Selangor
            [country] => Malaysia
            [postal_code] => 43000
        )

    [3] => Array
        (
            [city] => Kajang
            [state] => Selangor
            [country] => Malaysia
        )

    [4] => Array
        (
            [state] => Selangor
            [country] => Malaysia
        )

    [5] => Array
        (
            [postal_code] => 43000
            [state] => Selangor
            [country] => Malaysia
        )

    [6] => Array
        (
            [state] => Selangor
            [country] => Malaysia
        )

    [7] => Array
        (
            [state] => Selangor
            [country] => Malaysia
        )

    [8] => Array
        (
            [country] => Malaysia
        )

)

您可以像这样遍历所有找到的城市:

foreach($geoResults as $result) {
    echo isset($result['city']) ? $result['city'] : 'N/A';
    echo '<br>';
}