Building a McCance Drainage Contractor Tracking App in React


Building a McCance Drainage Contractor Tracking App in React
Introduction
With the growing need for real-time location tracking in field services, a web-based application can significantly enhance the efficiency of drainage contractors. This technical article outlines how to build a tracking application for McCance Drainage Contractors using React and Leaflet.js for mapping and geolocation tracking.
Technologies Used
React.js – For building the front-end user interface.
Leaflet.js – For rendering the map and tracking locations.
Geolocation API – To fetch the user's current location.
OpenStreetMap – As a free map tile provider.
Setting Up the React App
First, create a new React project if you haven’t already:
npx create-react-app mccance-drainage-app
cd mccance-drainage-app
npm install react-leaflet leaflet
Implementing Location Tracking
In McCanceDrainageApp.js
, implement the tracking functionality:
import React, { useState, useEffect } from "react";
import { MapContainer, TileLayer, Marker, Popup } from "react-leaflet";
import "leaflet/dist/leaflet.css";
const McCanceDrainageApp = () => {
const [location, setLocation] = useState(null);
useEffect(() => {
navigator.geolocation.getCurrentPosition(
(position) => {
setLocation({
lat: position.coords.latitude,
lng: position.coords.longitude,
});
},
(error) => {
console.error("Error getting location: ", error);
}
);
}, []);
return (
<div className="h-screen w-full">
<h1 className="text-xl font-bold text-center my-4">McCance Drainage Contractor Tracking</h1>
<div className="w-full h-[80vh]">
{location ? (
<MapContainer center={[location.lat, location.lng]} zoom={13} className="h-full w-full">
<TileLayer
url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
attribution="© OpenStreetMap contributors"
/>
<Marker position={[location.lat, location.lng]}>
<Popup>Your current location</Popup>
</Marker>
</MapContainer>
) : (
<p className="text-center">Fetching location...</p>
)}
</div>
</div>
);
};
export default McCanceDrainageApp;
Explanation of Code
Using
useState
anduseEffect
Hooks: These hooks manage the user's location state and fetch geolocation data upon component mount.navigator.geolocation.getCurrentPosition
API: Retrieves the user's current latitude and longitude.Rendering the Map with
react-leaflet
:MapContainer
: Initializes the map with a center and zoom level.TileLayer
: Uses OpenStreetMap for map tiles.Marker
andPopup
: Displays the user's location with a clickable marker.
Handling Errors: If geolocation fails, an error is logged to the console.
Running the Application
To start the application, run:
npm start
Open http://localhost:3000
in a browser to see the tracking app in action.
Future Enhancements
Real-time Tracking: Implement WebSockets or polling to update the location dynamically.
Backend Integration: Store and analyze tracking data using a database and API.
Multi-User Support: Allow tracking multiple drainage contractors on the same map.
Conclusion
This article demonstrated how to build a basic location-tracking app for McCance Drainage Contractors using React and Leaflet.js. By leveraging modern web technologies, this solution can enhance field service operations, optimize route planning, and improve service efficiency. Further improvements can be made by integrating real-time tracking and backend storage.
Subscribe to my newsletter
Read articles from Stella Josephine directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
