简体   繁体   中英

Json reponse from Google API Distance Value

I would like to get only the distance value in a variable from a json Google API Distance Response. For info i use the package https://github.com/alexpechkarev/google-maps .

Here my function :

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

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

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

    ])->get();

dd($response);

});

Here a part of my Json reponse

"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

I would like to get the value from distance = "93199" someone knows how i could access to the value ?

Thanks a lot in advance

You can access the value distance by

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

});

Feel free to manipulate the codes that I gave.

Hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM