Finish project. Please read the readme.
Signed-off-by: Rick Hays <rhays@haysgang.com>
This commit is contained in:
103
RX.php
Normal file
103
RX.php
Normal file
@@ -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;
|
||||
}
|
||||
35
classes/geocode.php
Normal file
35
classes/geocode.php
Normal file
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
2
css/system-min.css
vendored
2
css/system-min.css
vendored
@@ -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%}
|
||||
@@ -10,6 +10,11 @@ body {
|
||||
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;
|
||||
@@ -51,7 +56,7 @@ body {
|
||||
text-align: left;
|
||||
width: 80%; }
|
||||
body section div#results {
|
||||
background-color: aqua;
|
||||
background-color: white;
|
||||
margin: 0 auto;
|
||||
text-align: left;
|
||||
width: 80%; }
|
||||
|
||||
@@ -1,8 +1,3 @@
|
||||
<?php
|
||||
opcache_reset();
|
||||
// apc_clear_cache();
|
||||
?>
|
||||
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
@@ -17,10 +12,13 @@
|
||||
<body>
|
||||
<header>
|
||||
<div>
|
||||
<p>Please enter a Latitude and Longitude</p>
|
||||
<h2>Find the closest Pharmacy to you</h2>
|
||||
</div>
|
||||
</header>
|
||||
<section>
|
||||
<div id="request">
|
||||
<p>Please enter a Latitude and Longitude</p>
|
||||
</div>
|
||||
<form action="" method="get">
|
||||
<div id="latitude_error"></div>
|
||||
<div id="latitute_input">
|
||||
@@ -41,7 +39,7 @@
|
||||
</div>
|
||||
</div>
|
||||
<div id="submit_button" class="clear">
|
||||
<input type="button" onClick="submitForm()" value=" Save ">
|
||||
<input type="button" onClick="submitForm()" value=" Find ">
|
||||
</div>
|
||||
</form>
|
||||
</section>
|
||||
27
js/system.js
27
js/system.js
@@ -1,7 +1,7 @@
|
||||
let value_error = false; // Flag if Lat/Lon values are out of range.
|
||||
let latitude_low = 38.0;
|
||||
let latitude_high = 40.0;
|
||||
let longitude_low = -96.0;
|
||||
let longitude_low = -97.0;
|
||||
let longitude_high = -94.0;
|
||||
|
||||
function submitForm()
|
||||
@@ -29,9 +29,26 @@ function submitForm()
|
||||
// if Lat or Lon values are out of range return to form.
|
||||
if (value_error) { return; }
|
||||
|
||||
// Call to API
|
||||
$.ajax(
|
||||
{
|
||||
url : 'https://rhays.us/Examples/RX_API_Call/RX.php',
|
||||
type : 'GET',
|
||||
dataType: 'html',
|
||||
data :
|
||||
{
|
||||
'K' : 'RLH1234',
|
||||
'Latitude' : nLatitude,
|
||||
'Longitude' : nLongitude
|
||||
},
|
||||
success : function(data)
|
||||
{
|
||||
$("#results").html(data);
|
||||
},
|
||||
error : function(request,error)
|
||||
{
|
||||
alert("Request: "+JSON.stringify(request));
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
alert('Submit Form');
|
||||
}
|
||||
|
||||
@@ -25,6 +25,16 @@ body
|
||||
|
||||
section
|
||||
{
|
||||
div#request
|
||||
{
|
||||
margin: 0 auto;
|
||||
width: 80%;
|
||||
|
||||
p
|
||||
{
|
||||
text-align: left;
|
||||
}
|
||||
}
|
||||
form
|
||||
{
|
||||
div#latitude_error
|
||||
@@ -99,7 +109,7 @@ body
|
||||
|
||||
div#results
|
||||
{
|
||||
background-color: aqua;
|
||||
background-color: white;
|
||||
margin: 0 auto;
|
||||
text-align: left;
|
||||
width: 80%;
|
||||
|
||||
Reference in New Issue
Block a user