Skip to content

Instantly share code, notes, and snippets.

@FluxCoder
Last active March 26, 2019 20:21
Show Gist options
  • Save FluxCoder/a5fde9e610bc673d521e66ef8f94fd3b to your computer and use it in GitHub Desktop.
Save FluxCoder/a5fde9e610bc673d521e66ef8f94fd3b to your computer and use it in GitHub Desktop.
Find the nearest Taxi to one point
<?php
/*
* Calculate the distance between 2 coordinates
* By James Roffey - https://jroffey.me
*/
if(isset($_GET["lat"]) && isset($_GET["lon"])){
$pickup = array($_GET["lat"], $_GET["lon"]);
$taxis = array(
0 => array ('id' => '1','driverid' => '1','lat' => '51.873675','lon' => '0.544403','online' => '1','number_plate' => 'FE07 RAB'),
1 => array ('id' => '2','driverid' => '2','lat' => '51.868801','lon' => '0.559987','online' => '1','number_plate' => 'FE07 RDY'),
2 => array ('id' => '3','driverid' => '3','lat' => '51.873233','lon' => '0.561296','online' => '1','number_plate' => 'FH05 ROS')
);
$nearest = null;
$thetaxi = 0;
foreach($taxis as $taxi){
$dist = distance(array($taxi["lat"], $taxi["lon"]), $pickup);
if($dist < $nearest || $nearest == null){
$nearest = $dist;
$thetaxi = $taxi;
}
}
echo "
Number Plate: {$thetaxi["number_plate"]} Taxi ID: {$thetaxi["id"]}<br />
<a href='https://www.google.com/maps/search/?api=1&query={$thetaxi["lat"]},{$thetaxi["lon"]}'>Taxi is here</a><br />";
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment