A real-time bus tracking application built with Node.js, Socket.IO, and Google Maps API.
- Real-time Bus Tracking: Live updates of bus positions every 3 seconds
- User Location Detection: GPS-based location detection with fallback
- Route Planning: Google Maps integration for optimal route calculation
- Nearby Bus Search: Find buses within a specified radius
- Interactive Dashboard: Live bus status and information
- Responsive Design: Mobile-first design that works on all devices
- Socket.IO Integration: Real-time communication between server and clients
- Node.js (v16 or higher)
- Google Maps API Key with the following APIs enabled:
- Maps JavaScript API
- Places API
- Directions API
- Geocoding API
Create the following folder structure in VS Code:
bus-tracking-app/
├── server.js
├── package.json
├── .env
├── README.md
└── public/
├── index.html
├── styles.css
└── script.js
Copy and paste the provided code into each respective file:
- server.js: Main backend server code
- package.json: Dependencies and scripts
- .env: Environment variables (replace YOUR_API_KEY)
- public/index.html: Frontend HTML structure
- public/styles.css: All styling and animations
- public/script.js: Frontend JavaScript logic
Open terminal in VS Code and run:
npm installThis will install:
- express (web framework)
- socket.io (real-time communication)
- cors (cross-origin resource sharing)
- dotenv (environment variables)
- nodemon (development auto-restart)
- Go to Google Cloud Console
- Create a new project or select existing one
- Enable the following APIs:
- Maps JavaScript API
- Places API
- Directions API
- Geocoding API
- Create credentials (API Key)
- Copy the API key
Edit the .env file and replace with your actual API key:
PORT=3000
GOOGLE_MAPS_API_KEY=your_actual_google_maps_api_key_here
NODE_ENV=developmentIn public/index.html, find this line:
src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&callback=initMap&libraries=geometry,places"Replace YOUR_API_KEY with your actual Google Maps API key.
For development (auto-restart on changes):
npm run devFor production:
npm startOpen your browser and navigate to:
http://localhost:3000
- Find Your Location: Click "My Location" to detect your current position
- Enter Destination: Type your destination in the input field
- Plan Route: Click "Plan Route" to see the route, time, and distance
- Track Buses: Watch the bus markers move in real-time on the map
- Bus Details: Click any bus marker to see detailed information
- Nearby Buses: Click "Nearby Buses" to find buses near your location
Get all buses with their current status and location.
Get specific bus information by ID.
Calculate route between origin and destination.
Find buses near a specific location.
buses-update: Real-time bus position updatesconnect: Client connection establisheddisconnect: Client disconnected
- Backend: Node.js, Express.js, Socket.IO
- Frontend: HTML5, CSS3, JavaScript (ES6+)
- Maps: Google Maps JavaScript API
- Real-time: Socket.IO for WebSocket communication
- Styling: Custom CSS with modern glassmorphism design
- server.js: Main backend server with API routes and Socket.IO
- package.json: Node.js dependencies and npm scripts
- .env: Environment variables (keep this file private)
- public/index.html: Main HTML structure and layout
- public/styles.css: All CSS styling including responsive design
- public/script.js: Frontend JavaScript for map and real-time features
- Google Maps not loading: Check your API key and enabled APIs
- Buses not updating: Check browser console for Socket.IO errors
- Location not detected: Allow location permissions in browser
- Port already in use: Change PORT in .env file
- Use browser dev tools to debug JavaScript
- Check server logs in terminal for backend issues
- Ensure all files are in correct directory structure
- Test with different browsers for compatibility
This project is licensed under the MIT License.
If you encounter any issues:
- Check the browser console for errors
- Verify all files are copied correctly
- Ensure Google Maps API key is valid and APIs are enabled
- Check that all dependencies are installed with
npm install