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

时间:2015-10-18 17:20:44

标签: php json google-maps geocoding reverse-geocoding

这是JSON输出:我想只获取formatted_address值& long_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函数用于获取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);

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

2 个答案:

答案 0 :(得分:4)

以下示例演示了如何解析和访问Geocoding Service结果long_name& PHP中的short_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);
    }

}

PhpFiddle

答案 1 :(得分:2)

首先,您可以在此处阅读有关地理编码器结果类型的所有信息 - >的 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>';
}