Tracking user location in real-time is a powerful feature for many mobile applications โ whether it's food delivery, ride-hailing, or in our case: a mobile barber app that shows the barber approaching the customer.
In this tutorial, Iโll walk you through how I built a real-time location tracking feature using:
Letโs dive in! ๐ง
Weโll build a Map Page that does the following:
โ
Tracks the barberโs location in real time
โ
Updates the Firebase database with the latest location
โ
Shows a route from barber to customer using Google Maps Directions
โ
Automatically navigates back when the barber is near (within 30 meters)
Weโll use these tools:
javascript
npm install react-native-maps react-native-maps-directions expo-location
npm install @react-native-async-storage/async-storage
npm install firebase axios
javascript
User opens MapTrackingPage
โ
Request Location Permission
โ
Fetch user destination from Firestore
โ
Every 10 seconds:
โโ Track barber location
โโ Save to Firebase (Barbers/1)
โโ Fetch user location
โโ Call Distance Matrix API
โโ If distance < 30 meters โ goBack()
Hereโs a breakdown of the main parts of the feature.
javascript
const [barberLocation, setBarberLocation] = useState({
latitude: 0,
longitude: 0,
latitudeDelta: 0.0922,
longitudeDelta: 0.0421,
});
We define both barber and customer location with default regions.
javascript
let { status } = await Location.requestForegroundPermissionsAsync();
if (status !== "granted") {
throw new Error("Location permission denied");
}
We need permission before tracking the user.
Every 10 seconds, we:
javascript
await Location.watchPositionAsync(
{ enableHighAccuracy: true, timeInterval: 20 },
async (location) => {
const docRef = doc(db, "Barbers", "1");
await updateDoc(docRef, {
location: {
latitude: location.coords.latitude,
longitude: location.coords.longitude,
},
});
}
);
From Customers/1, we fetch the customerโs saved location:
javascript
const docRef = doc(db, "Customers", "1");
const docSnap = await getDoc(docRef);
setUserLocation({
latitude: docSnap.data().location.latitude,
longitude: docSnap.data().location.longitude,
});
We use the Google Distance Matrix API to calculate the distance between barber and user:
javascript
const url = `https://maps.googleapis.com/maps/api/distancematrix/json?origins=${barberLat},${barberLong}&destinations=${userLat},${userLong}&key=${GOOGLE_MAPS_APIKEY}`;
const { data } = await axios.get(url);
const distance = parseFloat(data.rows[0].elements[0].distance.text) * 1000;
if (distance < 30) {
navigation.goBack(); // Barber has arrived
}
We use react-native-maps and MapViewDirections to show everything visually:
javascript
<MapView>
<Marker coordinate={barberLocation} />
<Marker coordinate={userLocation} />
<MapViewDirections
origin={barberLocation}
destination={userLocation}
apikey={GOOGLE_MAPS_APIKEY}
strokeWidth={3}
strokeColor="red"
/>
</MapView>
Before deploying, keep these in mind:
Task | Status |
Hide Google Maps API Key in .env | โ ๏ธ |
Use authenticated user ID (not hardcoded "Barbers/1") | โ ๏ธ |
Add Firestore security rules | โ |
Handle GPS denied / unavailable | โ |
Add loading & error states | ๐ก |
And thatโs it! We now have a real-time tracking page that:
This feature adds a premium user experience to any location-based service.