Commit 05d5efd2 authored by Hasith Yoman's avatar Hasith Yoman

station list with queue count

parent 2a241981
......@@ -31,6 +31,34 @@ class StationController extends Controller
}
return response()->json(['respond'=>$respond,'count'=>$queue]);
}
public function getNearestFuelStations(Request $req) {
$fuelStations = Station::join('users','users.id','=','stations.user_id')->orderby('id','DESC')
->get(['stations.id','stations.name','stations.location','stations.availability','users.name as uname','users.email','users.phone']);
$nearestStations = []; // initialize empty array to hold nearest stations
foreach ($fuelStations as $station) {
$queue = Queue::where('station_id',$station->id)->where('status','0')->count();
$distance = $this->calculateDistance(explode(":",$station->location)[0], explode(":",$station->location)[1], $req->lat, $req->lng);
$point1 = explode(":",$station->location)[0].",".explode(":",$station->location)[1];
$point2 = $req->lat.",".$req->lng;
$api_distance = $this->getAPIDistance($point1, $point2, $req->lat, $req->lng);
$station->distance = $distance;
$station->api_distance = $api_distance;
$station->queue = $queue;
$nearestStations[] = $station;
}
usort($nearestStations, function($a, $b) {
if ($a->distance == $b->distance) {
return $a->queue - $b->queue;
}
return strcmp($a->distance , $b->distance );
//return $a->distance <=> $b->distance;
});
return response()->json(['respond'=>$nearestStations]); ;
}
public function getAPIDistance($point1,$point2){
$key = "AIzaSyAqCHZnyToOOkw4fiumXxC5oTEaEVAIISA";
$response = Http::get('https://maps.googleapis.com/maps/api/directions/json?origin='.$point1.'&destination='.$point2.'&key='.$key);
......@@ -42,6 +70,8 @@ class StationController extends Controller
}
}
// calculate distance
public function calculateDistance($lat1, $lon1, $lat2, $lon2) {
$theta = $lon1 - $lon2;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment