Json从Google API距离值

时间:2017-06-05 10:45:15

标签: json laravel

我想从json Google API距离响应中仅获取变量中的距离值。有关信息,请使用包https://github.com/alexpechkarev/google-maps

这是我的功能:

Route :: get('/ test',function(){

$origin = 'Paris';
$destination = 'Toulouse';

$response = \GoogleMaps::load('directions')
    ->setParam([
        'origin'          => $origin,
        'destination'     => $destination,
        'mode' => 'driving' ,
        'language' => 'fr',

    ])->get();

dd($response);

});

这是我的Json响应的一部分

"routes": [\n
        {\n
            "bounds": {\n
                "northeast": {\n
                    "lat": 43.605236,\n
                    "lng": 2.3535075\n
                },\n
                "southwest": {\n
                    "lat": 43.1955599,\n
                    "lng": 1.4444285\n
                }\n
            },\n
            "copyrights": "Donn\u00e9es cartographiques \u00a92017 Google",\n
            "legs": [\n
                {\n
                    "distance": {\n
                        "text": "93,2 km",\n
                        "value": 93199\n
                    },\n

我想从距离=“93199”获取值,有人知道我怎么能访问该值?

提前多多感谢

1 个答案:

答案 0 :(得分:1)

您可以通过

访问值距离
Route::get('/test', function () {

$origin = 'Paris';
$destination = 'Toulouse';

$response = \GoogleMaps::load('directions')
    ->setParam([
        'origin'          => $origin,
        'destination'     => $destination,
        'mode' => 'driving' ,
        'language' => 'fr',

    ])->get();

$res = json_decode($response);;
foreach($res->routes as $resp) {
    foreach($resp->legs as $dist) {
        dd($dist->distance);
    }
}

});
  

随意操纵我提供的代码。

希望它有所帮助。