Ride-Hailing App Clones · Course book
Chapter 4: A Native Ride App with Google Maps
Ride apps live on phones, so the natural next step after a web version is a native mobile app. React Native lets you write the app in JavaScript and React while rendering real native components on iOS and Android, and Expo removes most of the setup pain. This chapter covers the front-end half of a full-stack ride app built with React Native, Expo and Google Maps, designed to talk later to a Node.js and MongoDB back end. It explains how the project is structured, how the home screen is built, how to obtain and protect a Google Maps key, how to show and style a native map, how to ask for the user's location properly, and how to show drivers around the user, with the distance calculations worked by hand.
Project structure and setup
A full-stack project like this is easiest to manage as two folders side by side: the mobile app and the server. Within the app, a clear structure pays off as the code grows.
| Folder | Contents |
|---|---|
| screens | One component per full screen, such as Home and Destination |
| components | Reusable pieces such as buttons, cards and map markers |
| global or styles | Shared colours, sizes and the custom map style |
| navigations | Stack and drawer navigators that move between screens |
| assets | Images such as car icons, and fonts |
| contexts | Shared state, such as the user's origin and destination |
Expo creates the project and runs a development server; the Expo Go app on a phone scans a QR code and loads the app instantly, with changes appearing as soon as files are saved. Libraries for navigation, maps and location are installed with npx expo install, which picks versions known to work with the project's Expo version, avoiding many compatibility problems.
Navigation in React Native uses a navigation library rather than URLs. A stack navigator pushes screens on top of each other, with a back gesture to return, while a drawer navigator slides a menu in from the side for account pages. Wrapping the whole app in a navigation container at the top of the component tree makes navigation available to every screen.
Building the home screen
The home screen shows a greeting, a row of service cards such as rides, reservations and deliveries, a large "Where to?" box, a short list of recent destinations and a small map of the area with nearby cars. React Native has no HTML elements: layouts use View for boxes, Text for text, Image for pictures and Pressable or touchable components for buttons. Styles are JavaScript objects created with StyleSheet.create, using flexbox much as on the web, though the default direction is a column rather than a row.
Horizontal lists of cards use FlatList with the horizontal prop, which renders only the visible items and scrolls smoothly. A ScrollView wraps the whole screen so that it scrolls on short phones, and SafeAreaView or the safe-area insets keep content clear of notches and the home indicator. Keeping all colours and sizes in a shared file, rather than scattering numbers through every style, makes it easy to keep the design consistent. Test on both a small and a large phone, and on both iOS and Android, because fonts, shadows and status bars differ between them.
Google Maps keys and a styled map
The react-native-maps library shows native maps: Apple Maps or Google Maps on iOS, and Google Maps on Android. Using Google's maps and services requires a Google Cloud project with billing set up and an API key with the right services enabled, such as the Maps SDK for Android and iOS, Places and Directions. The key goes into the app's configuration so that the native map can use it.
Keys embedded in a mobile app can be extracted by anyone who downloads it, so they must be restricted: limited to your app's package name or bundle identifier and to only the services the app needs. Setting usage quotas and billing alerts in the cloud console guards against unexpected charges if a key is misused.
A map can be restyled with a custom map style: a JSON array of rules, each selecting a feature type, such as roads, water or points of interest, and setting colours or visibility. A muted grey style with points of interest hidden keeps attention on the cars and the route, as real ride apps do. The style is passed to the map component's customMapStyle prop and stored in a separate file to keep the screen code clean.
The map's view is controlled by a region: a centre latitude and longitude plus latitudeDelta and longitudeDelta, the height and width of the visible area in degrees. One degree of latitude is about 111.32 km, so a latitudeDelta of 0.02 shows a strip 0.02 × 111.32 = 2.23 km tall, a comfortable neighbourhood view.
The longitude span needs more care, because lines of longitude converge towards the poles. A longitudeDelta of 0.02 at latitude 37.77° covers 0.02 × 111.32 × 0.790 = 1.76 km, less than the same number of degrees of latitude. A tall, narrow phone screen also has a different shape from a square, so libraries usually adjust one delta to match the screen's aspect ratio. Animating the map to a new region, for example when the rider's location arrives, with the map's animateToRegion method, is friendlier than jumping instantly, because the rider can see where the view has moved.
Some native map features need a development build rather than the Expo Go app, because they rely on native configuration, such as the Google Maps key on Android, that must be compiled into the app. Expo's build service can produce such a build for testing on a real device, and the same configuration is used later when building for the app stores.
Asking for location the right way
Expo's location module requests permission and reads the device's position. Permission handling must be careful, because mobile operating systems and app stores require it: the app asks only when the feature is needed, explains why in a clear message configured in the app settings, and keeps working, perhaps with a manual address, if the user says no.
import * as Location from "expo-location";
async function getUserLocation() {
const { status } = await Location.requestForegroundPermissionsAsync();
if (status !== "granted") return null; // fall back to typing an address
const { coords } = await Location.getCurrentPositionAsync({});
return { latitude: coords.latitude, longitude: coords.longitude };
}
The result is stored in shared state, for example a React context, so that both the home screen and the destination screen can use the rider's position as the default pickup. Foreground permission, used here, allows location only while the app is open; tracking in the background needs a separate, more sensitive permission that riders rarely need to give. Reverse geocoding the coordinates can display a friendly "Current location" address, which riders find far easier to check than a pair of numbers.
Drivers around you
A familiar touch is showing cars near the rider before they book. Each driver is a marker at its coordinates, drawn with a small car image instead of the default pin and rotated to the driver's heading. In this front-end stage the drivers can come from sample data; later, the server provides real positions. When positions update every few seconds, moving each marker smoothly from its old point to the new one, rather than letting it jump, makes the cars appear to drive along the streets.
With many drivers, checking every one on each update becomes slow, so real systems use geospatial indexes. MongoDB, the database planned for this project's server, supports a 2dsphere index and a $near query that returns documents sorted by distance from a point, optionally within a maximum distance, which is exactly the "drivers around you" query. Storing positions as GeoJSON points, with longitude first, is what makes that possible.
Practice tasks
- A map region has a
latitudeDeltaof 0.05. About how many kilometres tall is the visible area? - Compute the distance from the rider in the worked example to a driver at (37.7760, −122.4250).
- List the steps the app takes if the user refuses location permission.
- Explain how you would restrict a Google Maps key used in a mobile app, and why.
- Write the shape of a MongoDB document storing a driver's position as a GeoJSON point.
- Why does
FlatListperform better than mapping over an array inside aScrollViewfor long lists?
This course book is available to read online only at tech-academy.chrysolites.org.