Developed by: Pandit Programmer Date: 11/07/2025
A real-time web-based location tracker built using PHP, MySQL, JavaScript, jQuery, Bootstrap, and Google Maps API.
This project enables live user location tracking and history logging.
- Google Maps JavaScript API – map rendering and marker control
- Geolocation API – for live tracking
- PHP + MySQL – backend logic and storage
- Bootstrap & Alertify.js – responsive UI and alerts
- Save the complete code as
index.php
- Place it in your project root
Create a MySQL database (trackdb
) and run the following SQL:
CREATE TABLE `user_locations` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`coordinates` VARCHAR(255) NOT NULL,
`address` TEXT NOT NULL,
`created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP(),
PRIMARY KEY (`id`)
);
-
Replace
YOUR_API_KEY
in the<script src="https://maps.googleapis.com/...">
tag -
In the Google Cloud Console:
-
Restrict API key to your domain
-
Enable:
- Maps JavaScript API
- Geocoding API
-
- Database Access: Use minimal-privilege MySQL users for production.
- API Keys: Restrict keys to specific referrers and enable only required APIs.
project-root/
└── index.php ← Contains PHP, HTML, CSS, and JS
Function | Description |
---|---|
insert_location |
Saves location data (coordinates + address) |
get_logs |
Returns last 20 locations in JSON |
delete_location |
Deletes a location by ID |
export_csv |
Exports logs as downloadable CSV |
- Map rendered inside element with
id="map"
Button | Functionality |
---|---|
📍 Locate Me | Center map on current marker |
⏸️ Pause/Resume | Toggle geolocation tracking |
🕓 Show Logs | Display past locations in modal |
📄 Export CSV | Download logs as CSV file |
- Shows recent 20 entries with delete options
Function | Description |
---|---|
initMap() |
Initializes map, sets default view, starts tracking |
startTracking() |
Tracks location with watchPosition , calls insertLocation() on move |
updateMap(lat, lng) |
Moves or creates marker, pans/zooms |
reverseGeocode() |
Converts coordinates to address |
insertLocation() |
Sends location to PHP backend |
fetchLogs() |
Loads recent logs and injects into modal |
deleteLocation(id) |
Deletes a log entry |
toggleTracking() |
Pauses/resumes location tracking |
[User Location] → [Google Maps Marker] → [Reverse Geocode]
↓ ↓
[updateMap] [Address String]
↓ ↓
[insertLocation()] → [PHP → MySQL]
↓
[Fetch Logs (JSON)] ← fetchLogs()
↓
[Logs Modal]
INSERT INTO user_locations (...)
SELECT * FROM user_locations ORDER BY id DESC LIMIT 20
DELETE FROM user_locations WHERE id = ?
mysqli
with prepared statements$_POST
for AJAX handling- Output buffering for CSV export
- jQuery for AJAX and DOM
- Alertify.js for toasts and confirmations
- Bootstrap for UI components
- Google Maps JavaScript API
- ✔️ Prepared statements (SQL injection protection)
- ✔️ Clean AJAX-based frontend/backend interaction
- ✔️ UX: modals, tooltips, status indicators
- ✔️ Logical separation of concerns between PHP and JS
Feature | Status |
---|---|
Location tracking | ✅ |
Reverse geocoding | ✅ |
Insert into DB | ✅ |
Fetch recent logs | ✅ |
Delete individual log | ✅ |
Export to CSV | ✅ |
Mobile responsiveness | ✅ |
- 🛡️ Restrict Google API key to specific domains
- 🗄️ Move database credentials to a secure config file
- 🧹 Separate JS and CSS into their own files
- 📤 Paginate logs or add date filters for large datasets