-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
90 lines (73 loc) · 2.66 KB
/
Copy pathscript.js
File metadata and controls
90 lines (73 loc) · 2.66 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
// Load Favorite Routes
function loadFavorites() {
const favorites = document.getElementById("favoritesList");
favorites.innerHTML = "";
const routes = JSON.parse(localStorage.getItem("routes") || "[]");
routes.forEach(route => {
const li = document.createElement("li");
li.className = "list-group-item d-flex justify-content-between align-items-center";
li.textContent = route;
const btn = document.createElement("button");
btn.className = "btn btn-sm btn-outline-secondary";
btn.textContent = "Use";
btn.onclick = () => {
const [start, end] = route.split(" → ");
document.getElementById("start").value = start;
document.getElementById("end").value = end;
checkTraffic();
};
li.appendChild(btn);
favorites.appendChild(li);
});
}
let map;
function initMap() {
const defaultLocation = { lat: 18.5204, lng: 73.8567 };
map = new google.maps.Map(document.getElementById("map"), {
center: defaultLocation,
zoom: 12,
});
const trafficLayer = new google.maps.TrafficLayer();
trafficLayer.setMap(map);
}
// 🚦 Combined Function
function checkTraffic() {
const startLocation = document.getElementById("start").value;
const endLocation = document.getElementById("end").value;
if (!startLocation || !endLocation) {
showAlert("Please enter both start and destination locations.", "warning");
return;
}
// Convert start address
fetch(`https://maps.googleapis.com/maps/api/geocode/json?address=${startLocation}&key=AIzaSyCxwiQ1jpSsJnd7B7mLvqScnoZYfbKkq-k`)
.then(response => response.json())
.then(startData => {
const startCoords = startData.results[0].geometry.location;
// Convert end address
return fetch(`https://maps.googleapis.com/maps/api/geocode/json?address=${endLocation}&key=AIzaSyCxwiQ1jpSsJnd7B7mLvqScnoZYfbKkq-k`)
.then(response => response.json())
.then(endData => {
const endCoords = endData.results[0].geometry.location;
// Show route on map
showRouteOnMap(startCoords, endCoords);
});
})
.catch(error => console.error("Error getting location data:", error));
}
function showRouteOnMap(startCoords, endCoords) {
const directionsService = new google.maps.DirectionsService();
const directionsRenderer = new google.maps.DirectionsRenderer();
directionsRenderer.setMap(map);
const request = {
origin: startCoords,
destination: endCoords,
travelMode: "DRIVING",
};
directionsService.route(request, (result, status) => {
if (status === "OK") {
directionsRenderer.setDirections(result);
} else {
alert("Could not find route: " + status);
}
});
}