108 lines
3.3 KiB
PHP
108 lines
3.3 KiB
PHP
<?php
|
|
/**
|
|
* Created by PhpStorm.
|
|
* User: k
|
|
* Date: 30.06.2018
|
|
* Time: 18:29
|
|
*/
|
|
|
|
namespace App\Paper;
|
|
|
|
|
|
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.5;
|
|
|
|
/**
|
|
* @param $lat1
|
|
* @param $lon1
|
|
* @param $lat2
|
|
* @param $lon2
|
|
* @return float zwracana jest odległość w kilometrach
|
|
*/
|
|
private function distance($lat1, $lon1, $lat2, $lon2)
|
|
{
|
|
|
|
$theta = $lon1 - $lon2;
|
|
$dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta));
|
|
$dist = acos($dist);
|
|
$dist = rad2deg($dist);
|
|
$miles = $dist * 60 * 1.1515;
|
|
return ($miles * 1.609344);
|
|
}
|
|
|
|
|
|
/**
|
|
* pobiera informacje o najbliższym trafficarze względem punktu podanego jako domowy i zwraca Sformatowane
|
|
* informacje na jego temat lub jesli nie ma auta to podaje informację że nie ma dostępnego.
|
|
*
|
|
* @return string
|
|
*/
|
|
public function nearestCarText()
|
|
{
|
|
$info = $this->nearestCarInfo();
|
|
$formatedInfo = 'Brak aut w promieniu ' . self::CLOSEST_DISTANCE . 'km od domu.';
|
|
if ($info['car'] != []) {
|
|
$formatedInfo = "Najbliższy traficar:\n" .
|
|
'Model: ' . $info['car']['model'] . "\n" .
|
|
'Numer boczny: ' . $info['car']['orderNumber'] . "\n" .
|
|
'Rejestracja: ' . $info['car']['regNumber'] . "\n" .
|
|
'Lokalizacja: ' . $info['car']['location'] . "\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 = $this->getTraficarJson();
|
|
$closestCar = [];
|
|
$closestDistance = 1000;
|
|
foreach ($trafcarData['cars'] as $car) {
|
|
|
|
$distance = $this->distance($this->homeCoords['lat'], $this->homeCoords['lng'], $car['latitude'], $car['longitude']);
|
|
if ($distance < self::CLOSEST_DISTANCE && $distance < $closestDistance) {
|
|
$closestDistance = $distance;
|
|
$closestCar = $car;
|
|
}
|
|
}
|
|
return ['car' => $closestCar, 'distance' => round($closestDistance, 2),];
|
|
}
|
|
} |