Real-Time Delivery Tracking
Track Your Delivery
let map, marker;
function initMap() {
// Initialize the map
const initialPosition = { lat: 37.7749, lng: -122.4194 }; // Example: San Francisco
map = new google.maps.Map(document.getElementById("map"), {
zoom: 14,
center: initialPosition,
});
// Add a marker to the map
marker = new google.maps.Marker({
position: initialPosition,
map: map,
title: "Delivery Location",
});
// Start tracking the delivery
fetchDeliveryLocation();
}
function fetchDeliveryLocation() {
// Simulate fetching real-time location from the server
setInterval(async () => {
const response = await fetch("/get-location"); // Replace with your backend endpoint
const data = await response.json();
const newPosition = { lat: data.lat, lng: data.lng };
// Update marker position
marker.setPosition(newPosition);
// Center map on new position
map.setCenter(newPosition);
}, 5000); // Fetch location every 5 seconds
}
const express = require("express");
const app = express();
const cors = require("cors");
app.use(cors());
app.use(express.json());
// Simulated delivery coordinates
let deliveryLocation = { lat: 37.7749, lng: -122.4194 };
// Update delivery location (simulating movement)
setInterval(() => {
deliveryLocation.lat += (Math.random() - 0.5) * 0.001; // Random lat change
deliveryLocation.lng += (Math.random() - 0.5) * 0.001; // Random lng change
}, 5000);
// Endpoint to get current delivery location
app.get("/get-location", (req, res) => {
res.json(deliveryLocation);
});
// Start the server
const PORT = 3000;
app.listen(PORT, () => {
console.log(`Server running on http://localhost:${PORT}`);
});