8 changed files with 183 additions and 15 deletions
@ -0,0 +1,103 @@ |
|||||
|
<?php |
||||
|
/** |
||||
|
* RX - Main API to process in coming request for closest location. |
||||
|
* 2019-09-25 |
||||
|
* Rick Hays |
||||
|
* |
||||
|
*/ |
||||
|
|
||||
|
include_once 'classes/geocode.php'; |
||||
|
|
||||
|
// Test Key before allowing access - No encryption, simple key test. |
||||
|
$key = GetParam('K'); |
||||
|
if ($key !== 'RLH1234') |
||||
|
{ |
||||
|
header("HTTP/1.0 405 Method Not Allowed"); |
||||
|
exit; |
||||
|
} |
||||
|
|
||||
|
$request_method = strtoupper($_SERVER["REQUEST_METHOD"]); |
||||
|
switch($request_method) |
||||
|
{ |
||||
|
case 'GET': |
||||
|
|
||||
|
$Lat = GetParam('Latitude'); |
||||
|
$Lon = GetParam('Longitude'); |
||||
|
try |
||||
|
{ |
||||
|
$db = new PDO('sqlite:db/RX.db'); |
||||
|
$geo = new geocode(); |
||||
|
$shortest = 999999; // <-- Sets to a very high value to find shortest distance below. |
||||
|
|
||||
|
$result = $db->query('SELECT rowid,* FROM pharmacies'); |
||||
|
foreach ($result as $row) { |
||||
|
$distance = $geo->straight_line_distance($Lat, $Lon, $row['Latitude'], $row['Longitude'], 'N'); |
||||
|
if ($distance < $shortest) |
||||
|
{ |
||||
|
$pharmacy = $row['Pharmacy']; |
||||
|
$address = $row['Address']; |
||||
|
$city = $row['City']; |
||||
|
$state = $row['State']; |
||||
|
$zip = $row['Zip']; |
||||
|
$latitude = $row['Latitude']; |
||||
|
$longitude = $row['Longitude']; |
||||
|
$shortest = $distance; |
||||
|
} |
||||
|
} |
||||
|
$db = NULL; |
||||
|
} |
||||
|
catch(PDOException $e) |
||||
|
{ |
||||
|
echo 'PDO Exception: ' . $e-> getMessage(); |
||||
|
exit; |
||||
|
} |
||||
|
break; |
||||
|
// case 'POST': |
||||
|
// break; |
||||
|
// case 'PUT': |
||||
|
// break; |
||||
|
// case 'DELETE': |
||||
|
// break; |
||||
|
default: |
||||
|
// Invalid Request Method |
||||
|
header("HTTP/1.0 405 Method Not Allowed"); |
||||
|
exit; |
||||
|
} |
||||
|
|
||||
|
// Output JSON back to HTML file. |
||||
|
echo 'Closest Pharmacy To you is: <br/><br/>'; |
||||
|
echo $pharmacy . '<br/>'; |
||||
|
echo $address . '<br/>'; |
||||
|
echo $city . '<br/>'; |
||||
|
echo $state . '<br/>'; |
||||
|
echo $zip . '<br/>'; |
||||
|
echo '<br/>'; |
||||
|
// echo $latitude . '<br/>'; |
||||
|
// echo $longitude . '<br/>'; |
||||
|
echo 'Distance of - ' . round($shortest, 1) . ' mile(s) away.<br/>'; |
||||
|
exit; |
||||
|
|
||||
|
// ##### END OF PROGRAM ################################################################################################ |
||||
|
|
||||
|
/** |
||||
|
* GetParam - Returns the GET or POST values, and allows for defaults if not present. |
||||
|
* @param $param_name |
||||
|
* @param null $default |
||||
|
* |
||||
|
* @return string|null |
||||
|
*/ |
||||
|
function GetParam($param_name, $default=NULL) |
||||
|
{ |
||||
|
global $_POST; |
||||
|
global $_GET; |
||||
|
$param_value = ""; |
||||
|
|
||||
|
if(isset($_POST[$param_name])) |
||||
|
$param_value = $_POST[$param_name]; |
||||
|
else if(isset($_GET[$param_name])) |
||||
|
$param_value = $_GET[$param_name]; |
||||
|
else if($param_value === '') |
||||
|
$param_value = $default; |
||||
|
|
||||
|
return $param_value; |
||||
|
} |
@ -0,0 +1,35 @@ |
|||||
|
<?php |
||||
|
|
||||
|
class geocode |
||||
|
{ |
||||
|
/** |
||||
|
* straight_line_distance - Returns Straight Line distance between two sets of (LAT/LONGS) |
||||
|
* @param real $lat1 - Latitude of Location #1 |
||||
|
* @param real $lon1 - Longitude of Location #1 |
||||
|
* @param real $lat2 - Latitude of Location #2 |
||||
|
* @param real $lon2 - Longitude of Location #2 |
||||
|
* @param string $unit - Unit of measure [DEFAULT = statute miles] ('K'=kilometers, 'N'=nautical miles) |
||||
|
* @return numeric - returns the distance in the desired unit of measure. |
||||
|
*/ |
||||
|
public function straight_line_distance($lat1, $lon1, $lat2, $lon2, $unit='') |
||||
|
{ |
||||
|
$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; |
||||
|
$unit = strtoupper($unit); |
||||
|
if ($unit == "K") |
||||
|
{ |
||||
|
return ($miles * 1.609344); |
||||
|
} |
||||
|
else if ($unit == "N") |
||||
|
{ |
||||
|
return ($miles * 0.8684); |
||||
|
} |
||||
|
else |
||||
|
{ |
||||
|
return $miles; |
||||
|
} |
||||
|
} |
||||
|
} |
@ -1 +1 @@ |
|||||
.clear{clear:both;float:none}body{background-color:lightgray;width:100%}body header div{margin:0 auto;width:80%}body header div p{text-align:left}body section form div#latitude_error{color:red;margin:0 auto;text-align:left;width:80%}body section form div#latitute_input{margin:10px auto;text-align:left;width:80%}body section form div#latitute_input div#latitute_input_label{float:left;text-align:right;width:80px}body section form div#latitute_input div#latitute_input_input{float:left;text-align:left;width:100px}body section form div#longitude_error{color:red;margin:0 auto;text-align:left;width:80%}body section form div#longitude_input{margin:10px auto;width:80%}body section form div#longitude_input div#longitude_input_label{float:left;text-align:right;width:80px}body section form div#longitude_input div#longitude_input_text{float:left;text-align:left;width:100px}body section form div#submit_button{padding:20px 0 20px 180px;width:80%}body section div#result_header{margin:0 auto;text-align:left;width:80%}body section div#results{background-color:aqua;margin:0 auto;text-align:left;width:80%}body footer p{margin:20px auto;text-align:left;width:80%} |
.clear{clear:both;float:none}body{background-color:lightgray;width:100%}body header div{margin:0 auto;width:80%}body header div p{text-align:left}body section div#request{margin:0 auto;width:80%}body section div#request p{text-align:left}body section form div#latitude_error{color:red;margin:0 auto;text-align:left;width:80%}body section form div#latitute_input{margin:10px auto;text-align:left;width:80%}body section form div#latitute_input div#latitute_input_label{float:left;text-align:right;width:80px}body section form div#latitute_input div#latitute_input_input{float:left;text-align:left;width:100px}body section form div#longitude_error{color:red;margin:0 auto;text-align:left;width:80%}body section form div#longitude_input{margin:10px auto;width:80%}body section form div#longitude_input div#longitude_input_label{float:left;text-align:right;width:80px}body section form div#longitude_input div#longitude_input_text{float:left;text-align:left;width:100px}body section form div#submit_button{padding:20px 0 20px 180px;width:80%}body section div#result_header{margin:0 auto;text-align:left;width:80%}body section div#results{background-color:white;margin:0 auto;text-align:left;width:80%}body footer p{margin:20px auto;text-align:left;width:80%} |
Binary file not shown.
Loading…
Reference in new issue