2017-06-05 37 views
0

我想從json Google API距離響應中只獲取變量中的距離值。對於我使用包https://github.com/alexpechkarev/google-maps的信息。來自Google API的Json響應距離值

這裏我的功能:

路線::得到( '/測試',函數(){

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

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

    ])->get(); 

dd($response); 

});

這裏的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 

我想擺脫距離的值=「93199」有人知道我怎麼能獲得價值?

感謝很多提前

回答

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

}); 

隨意操縱,我給了代碼。

希望它有幫助。