-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlocationtest.php
More file actions
98 lines (73 loc) · 2.33 KB
/
Copy pathlocationtest.php
File metadata and controls
98 lines (73 loc) · 2.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
<!DOCTYPE html>
<html>
<head>
<title>Nearest Centers</title>
<link rel="stylesheet" type="text/css" href="styles.css">
</head>
<body>
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
require_once 'database.php';
if ($_SERVER['REQUEST_METHOD']=== 'POST') {
$userLocation = $_POST['location'];
$bloodType = $_POST['bloodtype'];
if (empty($userLocation) || empty($bloodType)) {
echo "Please enter both location and blood type.";
exit;
}
$sql = "SELECT CENTER.CenterID, CENTER.CenterName, CENTER.Latitude, CENTER.Longitude
FROM CENTER
INNER JOIN INVENTORY ON CENTER.CenterID = INVENTORY.CenterID
WHERE INVENTORY.BloodType = :bloodType";
try {
$stmt = $conn->prepare($sql);
} catch (PDOException $e) {
echo "Error preparing statement: " . $e->getMessage();
exit;
}
$stmt->bindParam(':bloodType', $bloodType, PDO::PARAM_STR);
try {
$stmt->execute();
} catch (PDOException $e) {
echo "Error getting centers: " . $e->getMessage();
exit;
}
$nearestCenter = null;
$nearestDistance = PHP_INT_MAX;
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$centerLocation = array('lat' => $row['Latitude'], 'lng' => $row['Longitude']);
$distance = getDistance($userLocation, $centerLocation, 'AIzaSyCe5ZVazAnHUseeOhuUsXGFjVt2CKwaqYY');
if ($distance < $nearestDistance) {
$nearestDistance = $distance;
$nearestCenter = $row;
}
}
if ($nearestCenter) {
echo "<h2>Nearest Center:</h2>";
echo "<p>Name: " . htmlspecialchars($nearestCenter['CenterName']) . "</p>";
echo "<p>Distance: " . $nearestDistance . " km</p>";
} else {
echo "No centers found for the selected blood type.";
}
}
function getDistance($location1, $location2, $apiKey) {
$location2String = $location2['lat'] . ',' . $location2['lng'];
$url = "https://maps.googleapis.com/maps/api/distancematrix/json?units=metric&origins=" . urlencode($location1) . "&destinations=" . urlencode($location2String) . "&key=" . $apiKey;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$json = curl_exec($ch);
curl_close($ch);
$data = json_decode($json, true);
if ($data['status'] === 'OK' && !empty($data['rows'][0]['elements'][0]['distance'])) {
return $data['rows'][0]['elements'][0]['distance']['value'] / 1000;
} else {
error_log("Error getting distance: " . print_r($data, true));
return PHP_INT_MAX;
}
}
?>
</body>
</html>