-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
264 lines (212 loc) · 8.46 KB
/
script.js
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
var mapOptions = {};
var map;
var currMarker;
var desMarker;
var startIcon = {
url: 'curr-start-indicator.png', // Path to your custom icon image
scaledSize: new google.maps.Size(40, 40), // Size of the icon
}
var currentIcon = {
url: 'current-indicator.png', // Path to your custom icon image
scaledSize: new google.maps.Size(40, 40), // Size of the icon
}
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(success, error);
} else {
console.log("Geolocation is not supported by your browser.");
}
function success(position) {
const latitude = position.coords.latitude;
const longitude = position.coords.longitude;
console.log(`Your current location: Latitude: ${latitude}, Longitude: ${longitude}`);
// Define map options after getting the user's position
mapOptions = {
center: { lat: latitude, lng: longitude },
zoom: 17, // Adjust the zoom level as desired
mapTypeId: google.maps.MapTypeId.TERRAIN
};
// Create the map
map = new google.maps.Map(document.getElementById('googleMap'), mapOptions);
// Create a marker for the current location
currMarker = new google.maps.Marker({
position: { lat: latitude, lng: longitude },
map: map,
title: 'Your Location',
icon: currentIcon
});
// Add click listener to set destination on map click
google.maps.event.addListener(map, 'click', function (event) {
addPinMarker(event)
});
// Add listener for touch events (for mobile devices)
google.maps.event.addListener(map, 'touchend', function (event) {
addPinMarker(event)
});
}
function addPinMarker(event){
if(desMarker){
desMarker.setMap(null)
}
var endIcon = {
url: 'end-indicator.png', // Path to your custom icon image
scaledSize: new google.maps.Size(30, 40), // Size of the icon
}
desMarker = new google.maps.Marker({
position: event.latLng,
map: map,
title: 'Destination',
icon: endIcon
});
// Update destination input field with coordinates
document.getElementById("to").value = event.latLng.lat() + ", " + event.latLng.lng();
}
function error() {
console.log("Unable to retrieve your location");
}
// Load the map when the window is fully loaded
google.maps.event.addDomListener(window, 'load');
var directionsDisplay = new google.maps.DirectionsRenderer();
function calcRoute() {
//create map
document.getElementById('start-btn').style.display = 'none'
document.getElementById('stop-btn').style.display = 'block'
console.log(mapOptions)
//create a DirectionsService object to use the route method and get a result for our request
var directionsService = new google.maps.DirectionsService();
//create a DirectionsRenderer object which we will use to display the route
//bind the DirectionsRenderer to the map
if(!directionsDisplay.getMap()){
directionsDisplay.setMap(map);
}
//create request
directionsDisplay.setOptions({
polylineOptions: {
strokeColor: 'rgb(128, 69, 185)',
strokeWeight: 10 // Change this to the desired thickness // Change this to the desired color
}
});
navigator.geolocation.getCurrentPosition((position) => {
var request = {
origin: document.getElementById("from").value || {
lat: position.coords.latitude,
lng: position.coords.longitude,
},
destination: document.getElementById("to").value,
travelMode: google.maps.TravelMode.WALKING, //WALKING, BYCYCLING, TRANSIT
unitSystem: google.maps.UnitSystem.METRIC
}
console.log('origin:', request.origin);
//pass the request to the route method
directionsService.route(request, function (result, status) {
if (status == google.maps.DirectionsStatus.OK) {
//Get distance and time
const output = document.querySelector('#output');
output.innerHTML = "<div class='alert-info'>" + ".<br /> Walking distance <i class='fas fa-road'></i> : " + result.routes[0].legs[0].distance.text + ".<br />Duration <i class='fas fa-hourglass-start'></i> : " + result.routes[0].legs[0].duration.text + ".</div>";
directionsDisplay.setDirections({ routes: [] });
//display route
directionsDisplay.setDirections(result);
startLiveTracking(); // Start live tracking after route calculation
} else {
//delete route from map
directionsDisplay.setDirections({ routes: [] });
//center map in London
map.setCenter(myLatLng);
//show error message
output.innerHTML = "<div class='alert-danger'><i class='fas fa-exclamation-triangle'></i> Could not retrieve driving distance.</div>";
}
});
})
}
//create autocomplete objects for all inputs
var options = {
types: ['(cities)']
}
var input1 = document.getElementById("from");
var autocomplete1 = new google.maps.places.Autocomplete(input1, options);
var input2 = document.getElementById("to");
var autocomplete2 = new google.maps.places.Autocomplete(input2, options);
// Function to handle errors during positioning
function error(err) {
console.error(`Error obtaining your location: ${err.message}`);
}
var watchId; // Variable to store the watch position ID
// Function to start live tracking
function startLiveTracking() {
if (navigator.geolocation) {
watchId = navigator.geolocation.watchPosition(trackUser, error);
map.setZoom(47); // Adjust the zoom level as desired
} else {
console.log("Geolocation is not supported by your browser.");
}
}
// Function to track user's position
function trackUser(position) {
console.log('position:',position);
const latitude = position.coords.latitude;
const longitude = position.coords.longitude;
// Update user marker position
if (currMarker) {
currMarker.setPosition({ lat: latitude, lng: longitude });
} else {
// Create user marker if not already created
currMarker = new google.maps.Marker({
position: { lat: latitude, lng: longitude },
map: map,
title: 'Your Location',
icon: currentIcon
});
}
currMarker.setIcon(startIcon);
// Check if user has reached the destination
const destination = document.getElementById("to").value;
const distanceToDestination = google.maps.geometry.spherical.computeDistanceBetween(new google.maps.LatLng(latitude, longitude), destination);
if (distanceToDestination < ARRIVAL_THRESHOLD) {
// User has reached the destination, stop tracking
stopLiveTracking();
alert('reached');
console.log("You have reached your destination!");
}else{
// Recalculate route and update output
var request = {
origin: { lat: latitude, lng: longitude },
destination: destination,
travelMode: google.maps.TravelMode.WALKING,
unitSystem: google.maps.UnitSystem.METRIC
};
var directionsService = new google.maps.DirectionsService();
directionsService.route(request, function (result, status) {
if (status == google.maps.DirectionsStatus.OK) {
// Update distance and duration in the output
const output = document.querySelector('#output');
output.innerHTML = "<div class='alert-info'>" + "<br /> Walking distance <i class='fas fa-road'></i> : " + result.routes[0].legs[0].distance.text + ".<br />Duration <i class='fas fa-hourglass-start'></i> : " + result.routes[0].legs[0].duration.text + ".</div>";
directionsDisplay.setDirections({ routes: [] });
// Display updated route
directionsDisplay.setDirections(result);
} else {
// Show error message
const output = document.querySelector('#output');
output.innerHTML = "<div class='alert-danger'><i class='fas fa-exclamation-triangle'></i> Could not retrieve walking distance.</div>";
}
});
}
}
// Function to stop live tracking
function stopLiveTracking() {
if (watchId) {
navigator.geolocation.clearWatch(watchId);
watchId = undefined;
}
document.getElementById('start-btn').style.display = 'block'
document.getElementById('stop-btn').style.display = 'none'
currMarker.setIcon(currentIcon);
// Clear the directions displayed on the map
directionsDisplay.setDirections({ routes: [] });
// Remove the destination marker from the map
if (marker) {
desMarker.setMap(null);
desMarker = undefined; // Reset marker variable
}
}
// Constants
const ARRIVAL_THRESHOLD = 50; // Threshold distance (in meters) within which user is considered to have arrived
// Example usage: