Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Interactive Route with Traffic Info</title>
- <!-- Include Leaflet.js -->
- <link rel="stylesheet" href="https://unpkg.com/leaflet/dist/leaflet.css" />
- <script src="https://unpkg.com/leaflet/dist/leaflet.js"></script>
- <style>
- #map {
- width: 100%;
- height: 500px;
- }
- body {
- font-family: Arial, sans-serif;
- margin: 0;
- padding: 0;
- }
- #route-info {
- margin: 10px;
- padding: 10px;
- background: #f9f9f9;
- border: 1px solid #ccc;
- }
- </style>
- </head>
- <body>
- <h1 style="text-align: center;">Interactive Route with Traffic Info</h1>
- <div id="map"></div>
- <div id="route-info">Click on the map to set start and end points.</div>
- <script>
- const apiKey = 'Gixt05RAvLI57chTAGgMFzNaxcYAyigl'; // Your TomTom API Key
- // Initialize the map with OpenStreetMap as the base layer
- const map = L.map('map').setView([-7.25, 112.75], 12); // Centered on Surabaya
- // Add OpenStreetMap tiles
- L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
- attribution: '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
- }).addTo(map);
- let startPointMarker = null; // To store the start point marker
- let endPointMarker = null; // To store the end point marker
- let routeLine = null; // To store the route polyline
- // Define custom icons for start and end points
- const startIcon = L.icon({
- iconUrl: 'https://raw.githubusercontent.com/pointhi/leaflet-color-markers/master/img/marker-icon-green.png',
- iconSize: [25, 41],
- iconAnchor: [12, 41],
- popupAnchor: [1, -34]
- });
- const endIcon = L.icon({
- iconUrl: 'https://raw.githubusercontent.com/pointhi/leaflet-color-markers/master/img/marker-icon-red.png',
- iconSize: [25, 41],
- iconAnchor: [12, 41],
- popupAnchor: [1, -34]
- });
- // Function to handle map clicks (for setting start and end points)
- map.on('click', function (e) {
- const latLng = e.latlng; // Get the latitude and longitude of the click
- if (!startPointMarker) {
- // First click: Set the start point
- startPointMarker = L.marker(latLng, { draggable: true, icon: startIcon }).addTo(map);
- startPointMarker.bindPopup('Start Point').openPopup();
- startPointMarker.on('dragend', updateRoute); // Update route when marker is dragged
- document.getElementById('route-info').innerHTML = `Start Point: ${latLng.lat.toFixed(5)}, ${latLng.lng.toFixed(5)}`;
- } else if (!endPointMarker) {
- // Second click: Set the end point
- endPointMarker = L.marker(latLng, { draggable: true, icon: endIcon }).addTo(map);
- endPointMarker.bindPopup('End Point').openPopup();
- endPointMarker.on('dragend', updateRoute); // Update route when marker is dragged
- document.getElementById('route-info').innerHTML = `End Point: ${latLng.lat.toFixed(5)}, ${latLng.lng.toFixed(5)}`;
- // Fetch and display the route
- fetchRoute(startPointMarker.getLatLng(), endPointMarker.getLatLng());
- } else {
- // Reset if both points are already set
- resetMarkers();
- startPointMarker = L.marker(latLng, { draggable: true, icon: startIcon }).addTo(map);
- startPointMarker.bindPopup('Start Point').openPopup();
- startPointMarker.on('dragend', updateRoute);
- document.getElementById('route-info').innerHTML = `Start Point: ${latLng.lat.toFixed(5)}, ${latLng.lng.toFixed(5)}`;
- }
- });
- // Function to reset markers and route
- function resetMarkers() {
- if (startPointMarker) {
- map.removeLayer(startPointMarker);
- startPointMarker = null;
- }
- if (endPointMarker) {
- map.removeLayer(endPointMarker);
- endPointMarker = null;
- }
- if (routeLine) {
- map.removeLayer(routeLine);
- routeLine = null;
- }
- document.getElementById('route-info').innerHTML = 'Click on the map to set start and end points.';
- }
- // Function to update the route when markers are moved
- async function updateRoute() {
- if (startPointMarker && endPointMarker) {
- const startLatLng = startPointMarker.getLatLng();
- const endLatLng = endPointMarker.getLatLng();
- await fetchRoute(startLatLng, endLatLng);
- }
- }
- // Function to fetch the route using TomTom Routing API
- async function fetchRoute(start, end) {
- const url = `https://api.tomtom.com/routing/1/calculateRoute/${start.lat},${start.lng}:${end.lat},${end.lng}/json?key=${apiKey}&traffic=true`;
- //const url = `https://api.tomtom.com/routing/1/calculateRoute/${start.lat},${start.lng}:${end.lat},${end.lng}/json?key=${apiKey}&traffic=true`;
- try {
- const response = await fetch(url);
- if (!response.ok) {
- throw new Error(`Error fetching route: HTTP ${response.status}`);
- }
- const data = await response.json();
- console.log('Full API Response:', data); // Log the full response
- // Check if the response contains valid route data
- if (!data.routes || !data.routes[0] || !data.routes[0].legs || !data.routes[0].legs[0]) {
- throw new Error('Invalid route data received from the API.');
- }
- // Extract route information
- const route = data.routes[0];
- const summary = route.summary;
- const distance = (summary.lengthInMeters / 1000).toFixed(2); // Convert meters to kilometers
- const travelTime = Math.ceil(summary.travelTimeInSeconds / 60); // Convert seconds to minutes
- const trafficDelay = Math.ceil(summary.trafficDelayInSeconds / 60); // Convert seconds to minutes
- // Display route information
- document.getElementById('route-info').innerHTML = `
- <strong>Route Summary:</strong><br>
- Distance: ${distance} km<br>
- Estimated Travel Time: ${travelTime} minutes<br>
- Traffic Delay: ${trafficDelay} minutes
- `;
- // Extract route geometry
- const coordinates = route.legs[0].points.map(point => [point.latitude, point.longitude]);
- // Determine overall route color based on traffic delay
- const routeColor = getRouteColor(trafficDelay);
- // Draw the route on the map
- if (routeLine) {
- map.removeLayer(routeLine); // Remove the previous route
- }
- routeLine = L.polyline(coordinates, {
- color: routeColor,
- weight: 5
- }).addTo(map);
- // Add a popup to the route with traffic info
- routeLine.bindPopup(`
- <strong>Traffic Info:</strong><br>
- Distance: ${distance} km<br>
- Estimated Travel Time: ${travelTime} minutes<br>
- Traffic Delay: ${trafficDelay} minutes
- `);
- // Adjust the map view to fit the route
- map.fitBounds(L.latLngBounds(coordinates));
- } catch (error) {
- console.error('Error fetching route:', error);
- document.getElementById('route-info').innerHTML = 'Failed to load route information.';
- }
- }
- // Function to determine the route color based on traffic delay
- function getRouteColor(trafficDelay) {
- if (trafficDelay === 0) return '#28a745'; // Green: No traffic delay
- if (trafficDelay <= 5) return '#ffc107'; // Yellow: Moderate delay
- return '#dc3545'; // Red: Heavy delay
- }
- </script>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement