-
Notifications
You must be signed in to change notification settings - Fork 0
/
fire.html
211 lines (187 loc) · 8.97 KB
/
fire.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 fireS</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: #f8edf0;
}
#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> <!-- Place map container here -->
<button id="emergencyButton">Emergency</button> <!-- Button for emergency -->
<script>
var platform = new H.service.Platform({
'apikey': '{Your API KEY}'
});
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 fireMarker = null; // Marker for nearest fire
// 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 fires
service.browse({
at: userLatLng.lat + ',' + userLatLng.lng,
limit: 5,
categories: '700-7300-0113', // Category ID for fires
}, (result) => {
// Initialize variables for the fire with minimum duration
var minDuration = Infinity;
var closestfireLatLng = null;
var closestfireName = null;
// Iterate through each fire
result.items.forEach((item) => {
var fireLatLng = item.position;
var fireName = item.title;
// Calculate route and determine duration
calculateRouteFromAtoB(platform, userLatLng, fireLatLng, (duration) => {
// If this fire has a shorter duration, update closest fire
if (duration < minDuration) {
minDuration = duration;
closestfireLatLng = fireLatLng;
closestfireName = fireName;
// Remove previously added fireMarker if any
if (fireMarker) {
map.removeObject(fireMarker);
}
// Create fireMarker here, after closest fire is determined
fireMarker = new H.map.Marker(closestfireLatLng);
fireMarker.setData({ name: closestfireName, duration: duration }); // Store fire information in marker data
fireMarker.addEventListener('tap', function (evt) {
var data = evt.target.getData();
var fireName = data.name;
var duration = data.duration;
alert("Closest fire: " + fireName + "\nDuration: " + duration + " seconds");
});
map.addObject(fireMarker);
}
});
});
// 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=kvmRZD2v5zoIUmyBTPIFBGiUYNPw4BQfToxLSod10qM`;
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, fireLatLng, callback) {
var router = platform.getRoutingService(null, 8),
routeRequestParams = {
routingMode: 'fast',
transportMode: 'car',
origin: userLatLng.lat + ',' + userLatLng.lng,
destination: fireLatLng.lat + ',' + fireLatLng.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 = 'YOUR SID';
const authToken = 'YOUR AUTH TOKEN';
const fromNumber = '+19387661877'; // Your Twilio phone number
const toNumber = 'Your dummy number'; // 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>