Example #1
0
 /**
  * The 'viaroute' implementation of the OSRM server API.
  * https://github.com/DennisOSRM/Project-OSRM/wiki/Server-api
  * 
  * @param \Osrm\Coordinate $coordinate an undefined argument list of
  * coordinates. Two arguments (from/to) must be provided. 
  * @return type
  */
 public function getRoute()
 {
     $this->prepareServerUrl();
     $requestUrl = $this->server . 'viaroute?';
     if (func_num_args() < 2) {
         throw new OsrmException('A minimum of two arguments must be provided.', 2);
     }
     for ($j = 0; $j < func_num_args(); $j++) {
         $coord = func_get_arg($j);
         if ($coord instanceof Coordinate) {
             if ($j != 0) {
                 $requestUrl = $requestUrl . '&';
             }
             $requestUrl = $requestUrl . $coord;
         }
     }
     $curl = curl_init();
     curl_setopt_array($curl, array(CURLOPT_RETURNTRANSFER => 1, CURLOPT_URL => $requestUrl));
     $resp = curl_exec($curl);
     curl_close($curl);
     $json = json_decode($resp);
     if ($json->status === 0) {
         $route = new Route();
         $route->setEndPoint($json->route_summary->end_point);
         $route->setStartPoint($json->route_summary->start_point);
         $route->setTotalTime($json->route_summary->total_time);
         $route->setTotalDistance($json->route_summary->total_distance);
         $route->setRouteGeometry($json->route_geometry);
         $instructionsJson = $json->route_instructions;
         $instructions = array();
         foreach ($instructionsJson as $instrObj) {
             $instruction = new DrivingInstruction();
             $instruction->setTurnInstruction($instrObj[0]);
             $instruction->setWayName($instrObj[1]);
             $instruction->setLength($instrObj[2]);
             $instruction->setPosition($instrObj[3]);
             $instruction->setTime($instrObj[4]);
             $instruction->setLengthUnit($instrObj[5]);
             $instruction->setDirection($instrObj[6]);
             $instruction->setAzimuth($instrObj[7]);
             array_push($instructions, $instruction);
         }
         $route->setRouteInstructions($instructions);
         return $route;
     } else {
         throw new OsrmException("Osrm status error", $json->{'status'});
         return null;
     }
 }