Nearest traficar inforations.
This commit is contained in:
60
app/Paper/Traficar.php
Normal file
60
app/Paper/Traficar.php
Normal file
@@ -0,0 +1,60 @@
|
||||
<?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 $staticMapsKey = 'AIzaSyBPb9wXT2BcK8EH_3FFWVc2MLuDprj7YXw';
|
||||
|
||||
/**
|
||||
* @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);
|
||||
}
|
||||
|
||||
|
||||
public function nearestCar()
|
||||
{
|
||||
$trafcarData = json_decode(file_get_contents('https://api.traficar.pl/eaw-rest-api/car?shapeId=5'), 1);
|
||||
$closestCar = [];
|
||||
$closestDistance = 1000;
|
||||
foreach ($trafcarData['cars'] as $car) {
|
||||
|
||||
$distance = $this->distance($this->homeCoords['lat'], $this->homeCoords['lng'], $car['latitude'], $car['longitude']);
|
||||
if ($distance < $closestDistance) {
|
||||
$closestDistance = $distance;
|
||||
$closestCar = $car;
|
||||
}
|
||||
}
|
||||
|
||||
// $imgNoKey = "https://maps.googleapis.com/maps/api/staticmap?center=" . $latHome . "," . $lngHome . "&size=300x300&maptype=roadmap&format=png&visual_refresh=true&markers=size:mid%7Ccolor:0x000000%7C" . $closestCar['latitude'] . "," . $closestCar['longitude'];
|
||||
// $img = "https://maps.googleapis.com/maps/api/staticmap?center=" . $this->homeCoords['lat'] . "," . $this->homeCoords['lng'] . "&size=300x300&maptype=roadmap&format=png&visual_refresh=true&markers=size:mid%7Ccolor:0x000000%7C" . $closestCar['latitude'] . "," . $closestCar['longitude'] . '&key=' . $this->staticMapsKey;
|
||||
// $tmpFile = storage_path() . '/traficar_map.png';
|
||||
// file_put_contents($tmpFile, file_get_contents($img));
|
||||
|
||||
|
||||
return ['car' => $closestCar, 'distance' => round($closestDistance, 2),];
|
||||
// 'imageName' => $tmpFile];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user