如何从json值获取位置值?

时间:2018-04-04 15:26:25

标签: php json

首先,我使用此链接从城市名称

获取经度和长度
https://maps.googleapis.com/maps/api/geocode/json?address=usa,califonia,Los%20Angeles&key={API-KEY}&language=EN

............................................... .................................................. .................................................. ........

加载此网址时,我会获得此json格式。

{
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "Los Angeles",
               "short_name" : "Los Angeles",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Los Angeles County",
               "short_name" : "Los Angeles County",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "California",
               "short_name" : "CA",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "United States",
               "short_name" : "US",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "Los Angeles, CA, USA",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 34.3373061,
                  "lng" : -118.1552891
               },
               "southwest" : {
                  "lat" : 33.7036519,
                  "lng" : -118.6681759
               }
            },
            "location" : {
               "lat" : 34.0522342,
               "lng" : -118.2436849
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 34.3373061,
                  "lng" : -118.1552891
               },
               "southwest" : {
                  "lat" : 33.7036519,
                  "lng" : -118.6681759
               }
            }
         },
         "place_id" : "ChIJE9on3F3HwoAR9AhGJW_fL-I",
         "types" : [ "locality", "political" ]
      }
   ],
   "status" : "OK"
}

我想从json格式获取location ==> lat = 34.0522342和location ==> lng = -118.2436849。

然后我使用此代码。

<?php
$url = 'https://maps.googleapis.com/maps/api/geocode/json?address=usa,califonia,Los%20Angeles&key={API-KEY}&language=EN';
$json = file_get_contents($url);
$json_data = json_decode($json, true);

$location_lat =  $json_data['results']['location']['lat'];
$location_lng =  $json_data['results']['location']['lng'];

echo $location_lat;
echo $location_lng;
?>

但我没有得到任何数据?我该怎么办?

2 个答案:

答案 0 :(得分:1)

如果您获得正确的数据层,那么您应该使用...

$location_lat =  $json_data['results'][0]['geometry']['location']['lat'];
$location_lng =  $json_data['results'][0]['geometry']['location']['lng'];

echo $location_lat.PHP_EOL;
echo $location_lng;

哪些输出......

34.0522342
-118.2436849

答案 1 :(得分:0)

你可以编辑到

$location_lat =  $json_data['results'][0]['geometry']['location']['lat'];
$location_lng =  $json_data['results'][0]['geometry']['location']['lng'];