コード例 #1
0
 /**
  * Calculates the distance to travel from origin to destination using thier respective co-ordinates.
  * This function makes use of the Google Maps Distance Matrix API and requires a valid Server API key.
  *
  * @param  App\Location $origin			The origin, for this project it should be the co-ordinates of the event
  * @param  App\Location $destination	The destination, as above
  * @return int              			The distance returned by the Google Maps Distance Matrix API
  */
 public static function getMapsMatrixDistance($origin, $destination)
 {
     //Try to get Distance from Google Distance Matrix API
     $response = file_get_contents('https://maps.googleapis.com/maps/api/distancematrix/json?origins=' . $origin->longitude . ',' . $origin->latitude . '&destinations=' . $destination->longitude . ',' . $destination->latitude . '&key=' . env('GOOGLE_API_KEY'));
     $response = json_decode($response, true, 512);
     //WOO Debug code! Documenting my approach tbh
     //If we're all cool with the functional code, I'll get rid of these comments
     //
     //	var_dump($response);
     //	var_dump($response["rows"][0]["elements"][0]["distance"]["value"]);
     //echo ($response["rows"][0]["elements"][0]["distance"]["value"] == '' ? 'empty' : 'not empty');
     //if( isset($response["rows"][0]["elements"][0]["distance"]["value"]) ){echo 'not empty';} else {echo 'empty';}
     //If response contains zero results call getDistance instead
     //This will only happen (I think) when Google can't find a route between the two points
     //Or on an invalid request
     if (isset($response["rows"][0]["elements"][0]["distance"]["value"])) {
         return $response["rows"][0]["elements"][0]["distance"]["value"];
     } else {
         return LocationController::getDistance($origin, $destination);
     }
 }