-
Notifications
You must be signed in to change notification settings - Fork 0
/
hospital.html
211 lines (187 loc) · 9.01 KB
/
hospital.html
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>NEARBY HOSPITALS</title>
<script src="https://js.api.here.com/v3/3.1/mapsjs-core.js"></script>
<script src="https://js.api.here.com/v3/3.1/mapsjs-service.js"></script>
<script src="https://js.api.here.com/v3/3.1/mapsjs-mapevents.js"></script>
<script src="https://js.api.here.com/v3/3.1/mapsjs-ui.js"></script>
<link rel="stylesheet" href="https://js.api.here.com/v3/3.1/mapsjs-ui.css">
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
background-color: #f8f9fa;
}
#map-container {
height: 70vh;
width: 100%;
}
#emergencyButton {
display: flex;
flex-direction: column;
align-items: center;
margin-top: 50px;
background-color: red;
}
</style>
</head>
<body>
<div id="map-container"></div>
<button id="emergencyButton">Emergency</button> <!-- Button for emergency -->
<script>
var platform = new H.service.Platform({
'apikey': 'hereapikey'
});
var defaultLayers = platform.createDefaultLayers();
var map = new H.Map(
document.getElementById('map-container'), // Use the map container element
defaultLayers.vector.normal.map,
{
zoom: 15
});
var behavior = new H.mapevents.Behavior(new H.mapevents.MapEvents(map));
var service = platform.getSearchService();
var userMarker = null; // Marker for user
var hospitalMarker = null; // Marker for nearest hospital
// Initialize the UI component
var ui = H.ui.UI.createDefault(map, defaultLayers);
// Get user's current location
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition((pos) => {
var userLatLng = {
lat: pos.coords.latitude,
lng: pos.coords.longitude
};
// Center the map at the user's current location
map.setCenter(userLatLng);
// Create a marker at the user's current location with a home icon
userMarker = new H.map.Marker(userLatLng, {
icon: new H.map.Icon('https://img.icons8.com/fluent/48/000000/home.png') // Home icon
});
map.addObject(userMarker);
// Perform reverse geocoding to get user's current address
reverseGeocode(userLatLng, (address) => {
// Search for nearby hospitals
service.browse({
at: userLatLng.lat + ',' + userLatLng.lng,
limit: 5,
categories: '800-8000-0159', // Category ID for hospitals
}, (result) => {
// Initialize variables for the hospital with minimum duration
var minDuration = Infinity;
var closestHospitalLatLng = null;
var closestHospitalName = null;
// Iterate through each hospital
result.items.forEach((item) => {
var hospitalLatLng = item.position;
var hospitalName = item.title;
// Calculate route and determine duration
calculateRouteFromAtoB(platform, userLatLng, hospitalLatLng, (duration) => {
// If this hospital has a shorter duration, update closest hospital
if (duration < minDuration) {
minDuration = duration;
closestHospitalLatLng = hospitalLatLng;
closestHospitalName = hospitalName;
// Remove previously added hospitalMarker if any
if (hospitalMarker) {
map.removeObject(hospitalMarker);
}
// Create hospitalMarker here, after closest hospital is determined
hospitalMarker = new H.map.Marker(closestHospitalLatLng);
hospitalMarker.setData({ name: closestHospitalName, duration: duration }); // Store hospital information in marker data
hospitalMarker.addEventListener('tap', function (evt) {
var data = evt.target.getData();
var hospitalName = data.name;
var duration = data.duration;
alert("Closest Hospital: " + hospitalName + "\nDuration: " + duration + " seconds");
});
map.addObject(hospitalMarker);
}
});
});
// Event listener for emergency button
document.getElementById("emergencyButton").addEventListener("click", function() {
// Sending message with user's current location and address
sendMessage(userLatLng, address);
});
});
});
});
} else {
alert('Geolocation is not supported by this browser.');
}
// Reverse geocode function
function reverseGeocode(userLatLng, callback) {
var reverseGeocodingUrl = `https://revgeocode.search.hereapi.com/v1/revgeocode?at=${userLatLng.lat},${userLatLng.lng}&lang=en-US&apikey=hereapikey`;
fetch(reverseGeocodingUrl)
.then(response => response.json())
.then(data => {
var address = data.items[0].address.label;
alert("Your current address is: " + address);
callback(address);
})
.catch(error => {
console.error('Error fetching reverse geocode data:', error);
});
}
function calculateRouteFromAtoB(platform, userLatLng, hospitalLatLng, callback) {
var router = platform.getRoutingService(null, 8),
routeRequestParams = {
routingMode: 'fast',
transportMode: 'car',
origin: userLatLng.lat + ',' + userLatLng.lng,
destination: hospitalLatLng.lat + ',' + hospitalLatLng.lng,
return: 'polyline,turnByTurnActions,actions,instructions,travelSummary'
};
router.calculateRoute(
routeRequestParams,
(result) => {
var route = result.routes[0];
var duration = 0;
route.sections.forEach((section) => {
duration += section.travelSummary.duration;
});
// Call the callback function with the duration
callback(duration);
},
onError
);
}
function onError(error) {
alert('Can\'t reach the remote server');
}
// Function to send message with user's current location and address
function sendMessage(userLocation, address) {
const accountSid = '';
const authToken = '';
const fromNumber = '+14793516220'; // Your Twilio phone number
const toNumber = ''; // Recipient's phone number
const messageBody = 'Emergency Location: ' + address;
fetch('https://api.twilio.com/2010-04-01/Accounts/' + accountSid + '/Messages.json', {
method: 'POST',
headers: {
'Authorization': 'Basic ' + btoa(accountSid + ':' + authToken),
'Content-Type': 'application/x-www-form-urlencoded'
},
body: new URLSearchParams({
'To': toNumber,
'From': fromNumber,
'Body': messageBody
})
})
.then(response => {
if (response.ok) {
console.log('Message sent successfully.');
} else {
console.error('Failed to send message.');
}
})
.catch(err => console.error('Error sending message:', err));
}
</script>
</body>
</html>