harshdhane's picture
Upload 9 files
8b283be verified
Raw
History Blame Contribute Delete
9.52 kB
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Autonomous Traffic Management System</title>
<link rel="stylesheet" href="{{ url_for('static', filename='css/style.css') }}">
<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyA0S_ArVIWsGXkPwpgIhCy2g4y7HlSamX8&libraries=geometry,places&callback=initMap" async defer></script>
</head>
<body>
<header>
<img src="https://cdn-icons-png.flaticon.com/512/3448/3448339.png" alt="Traffic Management Logo" class="logo">
<h1>Autonomous Traffic Management Dashboard</h1>
<div class="nav-buttons">
<a href="{{ url_for('index') }}">
<button class="nav-btn">Search place</button>
</a>
<a href="{{ url_for('main_dashboard') }}">
<button class="nav-btn">Live Traffic & Find Route</button>
</a>
</div>
<button class="login-btn" onclick="window.location.href='{{ url_for('logout') if 'username' in session else url_for('login') }}'">
{{ "Hello, " + session['username'] if 'username' in session else "Hello, Login" }}
</button>
</header>
<main>
<div class="content-container">
<!-- Find Route Section -->
<section id="route-section">
<h2>Find Route</h2>
<form id="route-form">
<label for="source-input">Source:</label>
<input id="source-input" type="text" placeholder="Enter source location">
<label for="destination-input">Destination:</label>
<input id="destination-input" type="text" placeholder="Enter destination location">
<button type="submit">Find</button>
</form>
<div id="travel-info"></div>
<div id="route-options" style="margin-top: 20px;"></div>
</section>
<!-- Traffic Stats Section -->
<section id="stats-section">
<h2>Traffic Statistics</h2>
<canvas id="traffic-stats-chart"></canvas>
</section>
</div>
<!-- Map Section -->
<section id="map-section">
<h2>Traffic Visualization</h2>
<div>
<label for="traffic-toggle">Show Traffic Layer:</label>
<input id="traffic-toggle" type="checkbox" checked>
</div>
<div id="map" style="height: 400px; width: 100%; margin-top: 10px;"></div>
</section>
</main>
<footer>
<p>Autonomous Traffic Management System</p>
</footer>
<script>
let map;
let directionsService;
let directionsRenderer;
let trafficLayer;
function initMap() {
map = new google.maps.Map(document.getElementById('map'), {
center: { lat: 18.4889, lng: 74.0246 },
zoom: 13
});
trafficLayer = new google.maps.TrafficLayer();
trafficLayer.setMap(map);
directionsService = new google.maps.DirectionsService();
directionsRenderer = new google.maps.DirectionsRenderer({ map: map });
const sourceInput = document.getElementById('source-input');
const destinationInput = document.getElementById('destination-input');
new google.maps.places.Autocomplete(sourceInput);
new google.maps.places.Autocomplete(destinationInput);
// Add event listener to the traffic toggle checkbox
const trafficToggle = document.getElementById('traffic-toggle');
trafficToggle.addEventListener('change', () => {
if (trafficToggle.checked) {
trafficLayer.setMap(map);
} else {
trafficLayer.setMap(null);
}
});
}
async function calculateAndDisplayRoute(source, destination) {
try {
const response = await directionsService.route({
origin: source,
destination: destination,
travelMode: google.maps.TravelMode.DRIVING,
provideRouteAlternatives: true
});
directionsRenderer.setDirections(response);
const routes = response.routes;
displayAvailableRoutes(routes);
} catch (error) {
alert("Error calculating route: " + error.message);
}
}
function displayAvailableRoutes(routes) {
const routeOptionsDiv = document.getElementById('route-options');
routeOptionsDiv.innerHTML = '<h3>Available Routes:</h3>';
routes.forEach((route, index) => {
const distance = route.legs[0].distance.text;
const duration = route.legs[0].duration.text;
const routeButton = document.createElement('button');
routeButton.textContent = `Route ${index + 1}: ${distance}, ${duration}`;
routeButton.addEventListener('click', () => {
directionsRenderer.setRouteIndex(index);
checkLiveTrafficOnRoute(route);
});
routeOptionsDiv.appendChild(routeButton);
});
}
async function checkLiveTrafficOnRoute(route) {
const trafficDetails = [];
const path = route.overview_path;
for (let i = 0; i < path.length; i += 1) {
const point = path[i];
try {
const trafficData = await axios.get(`https://maps.googleapis.com/maps/api/distancematrix/json`, {
params: {
origins: `${point.lat()},${point.lng()}`,
destinations: `${point.lat()},${point.lng()}`,
departure_time: 'now',
key: 'AIzaSyA0S_ArVIWsGXkPwpgIhCy2g4y7HlSamX8'
}
});
const durationInTraffic = trafficData.data.rows[0].elements[0].duration_in_traffic;
if (durationInTraffic) {
trafficDetails.push({
lat: point.lat(),
lng: point.lng(),
duration: durationInTraffic.text
});
}
} catch (error) {
console.error("Error fetching traffic data: ", error);
}
}
if (trafficDetails.length > 0) {
displayTrafficAlert(trafficDetails);
} else {
alert("This route has no significant traffic. You can proceed.");
}
}
function displayTrafficAlert(trafficDetails) {
let message = "Heavy Traffic Detected:\n\n";
trafficDetails.forEach((detail, index) => {
message += `Traffic Point ${index + 1}: Latitude: ${detail.lat}, Longitude: ${detail.lng}, Duration in Traffic: ${detail.duration}\n`;
});
message += "\nThis route has heavy traffic. Consider choosing another route.";
alert(message);
}
document.getElementById('route-form').addEventListener('submit', (e) => {
e.preventDefault();
const sourceLocation = document.getElementById('source-input').value;
const destinationLocation = document.getElementById('destination-input').value;
calculateAndDisplayRoute(sourceLocation, destinationLocation);
});
const ctx = document.getElementById('traffic-stats-chart').getContext('2d');
new Chart(ctx, {
type: 'bar',
data: {
labels: ['Intersection 1', 'Intersection 2', 'Intersection 3'],
datasets: [{
label: 'Traffic Density',
data: [30, 45, 60],
backgroundColor: ['green', 'yellow', 'red']
}]
},
options: {
responsive: true,
plugins: {
legend: {
display: true,
position: 'top',
},
tooltip: {
enabled: true,
}
},
scales: {
x: {
title: {
display: true,
text: 'Intersections',
}
},
y: {
title: {
display: true,
text: 'Traffic Density',
},
min: 0,
max: 100
}
}
}
});
</script>
</body>
</html>