Added information about available traficars in a given area to printed info.
This commit is contained in:
@@ -14,6 +14,7 @@ class Traficar
|
||||
|
||||
private $homeCoords = ['lat' => 54.417475, 'lng' => 18.481913];
|
||||
|
||||
private $traficarJson;
|
||||
//dystans w kilometrach w którym auta brane są pod uwagę
|
||||
const CLOSEST_DISTANCE = 2;
|
||||
|
||||
@@ -52,15 +53,46 @@ class Traficar
|
||||
'Numer boczny: ' . $info['car']['orderNumber'] . "\n" .
|
||||
'Rejestracja: ' . $info['car']['regNumber'] . "\n" .
|
||||
'Lokalizacja: ' . $info['car']['location'] . "\n" .
|
||||
'Odległość: ' . $info['distance'] . "km\n";
|
||||
'Odległość: ' . $info['distance'] . "km\n\n" .
|
||||
'Traficarów w promieniu ' . self::CLOSEST_DISTANCE . 'km: ' . $this->getTotalCarsInArea() . "\n";
|
||||
}
|
||||
return $formatedInfo;
|
||||
}
|
||||
|
||||
/**
|
||||
* pobiera informacje na temat aut traficatowych i zapisuje do pamięci
|
||||
* @return mixed
|
||||
*/
|
||||
private function getTraficarJson()
|
||||
{
|
||||
if (is_null($this->traficarJson)) {
|
||||
$this->traficarJson = json_decode(file_get_contents('https://api.traficar.pl/eaw-rest-api/car?shapeId=5'), 1);
|
||||
}
|
||||
return $this->traficarJson;
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* zwraca ilość aut w podanym promieniu
|
||||
* @return int
|
||||
*/
|
||||
private function getTotalCarsInArea()
|
||||
{
|
||||
$trafcarData = $this->getTraficarJson();
|
||||
$numberOfCarsInArea = 0;
|
||||
foreach ($trafcarData['cars'] as $car) {
|
||||
$distance = $this->distance($this->homeCoords['lat'], $this->homeCoords['lng'], $car['latitude'], $car['longitude']);
|
||||
if ($distance < self::CLOSEST_DISTANCE) {
|
||||
$numberOfCarsInArea++;
|
||||
}
|
||||
}
|
||||
return $numberOfCarsInArea;
|
||||
}
|
||||
|
||||
public function nearestCarInfo()
|
||||
{
|
||||
$trafcarData = json_decode(file_get_contents('https://api.traficar.pl/eaw-rest-api/car?shapeId=5'), 1);
|
||||
$trafcarData = $this->getTraficarJson();
|
||||
$closestCar = [];
|
||||
$closestDistance = 1000;
|
||||
foreach ($trafcarData['cars'] as $car) {
|
||||
|
||||
Reference in New Issue
Block a user