/** * Create Route. * * Creates a Route object from an array of comma separated coordinates, * e.g. ['52.54628,13.30841', '51.476780,0.000479', ...]. * * @param string[] $arrayOfCommaSeparatedCoordinates * @return Route */ public function createRoute($arrayOfCommaSeparatedCoordinates) { $coordinates = []; foreach ($arrayOfCommaSeparatedCoordinates as $item) { $valueArray = explode(',', $item); if (2 != count($valueArray)) { if (!is_null($this->logger)) { $this->logger->error(sprintf('"%s" are not valid coordinates.', $item)); } continue; } try { Assertion::allNumeric($valueArray); } catch (AssertionFailedException $e) { if (!is_null($this->logger)) { $this->logger->error(sprintf('Given coordinates "%s" are invalid. %s', $item, $e->getMessage())); continue; } } $lat = (double) $valueArray[0]; $long = (double) $valueArray[1]; try { $coordinate = new Coordinate($lat, $long); } catch (\DomainException $e) { if (!is_null($this->logger)) { $this->logger->error(sprintf('Given coordinates "%s" are invalid. %s', $item, $e->getMessage())); } continue; } $coordinates[] = $coordinate; } $route = new Route(); $route->setInputRoute($coordinates); return $route; }
/** * @param Route $route * @return Uri */ private function createUri(Route $route) { $cnt = 1; $max = self::MAX_WAYPOINTS_LIMIT + 1; $start = null; $waypoints = []; $end = null; $start = $route->getCurrentCoordinate(); while (($coordinate = $route->getNextCoordinate()) && $cnt <= $max) { $waypoints[] = $coordinate; $cnt++; } $end = array_pop($waypoints); $origLat = $start->getLatitude(); $origLng = $start->getLongitude(); $destLat = $end->getLatitude(); $destLng = $end->getLongitude(); $uriString = $this->serviceEndpoint . "origin={$origLat},{$origLng}" . "&destination={$destLat},{$destLng}"; $uriString .= $this->addWaypoints($waypoints); $uriString .= "&key={$this->apiKey}"; return new Uri($uriString); }