Dodanie do notatki informacji na temat liczby traficarów w okolicy. #54

Merged
krzysiej merged 3 commits from issue-53 into master 2018-08-15 19:14:23 +02:00

View File

@@ -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) {