-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
72 lines (58 loc) · 2.54 KB
/
script.js
File metadata and controls
72 lines (58 loc) · 2.54 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
mapboxgl.accessToken = 'pk.eyJ1IjoiYWRkcmFpbjEiLCJhIjoiY201c29qZGdkMGFjMDJpcG9iOGl0bnEzNCJ9.DZ9-xP5klHuPvECO3l2tEA';
navigator.geolocation.getCurrentPosition(successLocation, errorLocation, { enableHighAccuracy: true });
function successLocation(position) {
setupMap([position.coords.longitude, position.coords.latitude]);
}
function errorLocation() {
setupMap([-118.2437, 34.0522]); // Default to Los Angeles
}
function setupMap(center) {
const map = new mapboxgl.Map({
container: 'map',
style: 'mapbox://styles/mapbox/streets-v11',
center: center,
zoom: 15
});
map.addControl(new mapboxgl.NavigationControl());
const directions = new MapboxDirections({
accessToken: mapboxgl.accessToken,
unit: 'imperial',
profile: 'driving' // Travel mode (can be 'cycling', 'walking', 'driving')
});
map.addControl(directions, 'top-left');
directions.on('route', (event) => {
const routes = event.route; // Array of routes
let highestEcoScore = 0;
let bestRouteIndex = -1;
// Calculate eco score for all routes and find the highest score
routes.forEach((route, index) => {
const distance = route.distance / 1609.34; // Convert meters to miles
const duration = route.duration / 60; // Convert seconds to minutes
const ecoScore = calculateEcoScore(distance, duration);
if (ecoScore > highestEcoScore) {
highestEcoScore = ecoScore;
bestRouteIndex = index; // Track the index of the best route
}
// Update the eco score for the current route
if (index === 0) updateEcoScore(ecoScore);
});
// Show the message only if the selected route is the most eco-friendly one
if (bestRouteIndex === 0) {
showEcoMessage();
}
});
}
// Function to calculate eco score
function calculateEcoScore(distance, duration) {
const maxEcoScore = 100;
const ecoFactor = 30; // Adjust factor for influence
const score = maxEcoScore - (ecoFactor * (distance / duration));
return Math.max(10, score); // Ensure a minimum score of 10%
}
// Function to update the eco score in the green box
function updateEcoScore(score) {
const ecoScoreBox = document.getElementById('eco-score-box');
const ecoScoreValue = document.getElementById('eco-score-value');
ecoScoreValue.textContent = score.toFixed(1); // Update the displayed score
ecoScoreBox.style.display = 'block'; // Show the eco score box
}