Ride-Hailing App Clones · Course book
Chapter 1: Foundations and the Map
Ride-hailing apps feel effortless: open the app, see a map centred on you, type a destination and choose a ride. Behind that simplicity sits a careful combination of a web framework, a map service, location data and a back end. This first chapter of the ride-app project lays the foundations with Next.js, React and Tailwind CSS, and puts an interactive Mapbox map on the screen. It explains how a Next.js project is organised, how the home screen of a ride app is laid out, how a web map works from tiles and zoom levels to coordinates, and how to initialise a map safely inside a React component. Later chapters build on this to add search, routes, ride options and sign-in.
Next.js, React and Tailwind
Next.js is a React framework. It adds file-based routing, where each file in the pages or app folder becomes a route, so search.js becomes /search; server-side rendering and static generation for fast first loads; and a build system that bundles and optimises the code. React supplies the components; Next.js supplies the structure around them.
Tailwind CSS styles elements with small utility classes such as flex, h-screen, bg-gray-200 or rounded-full, written directly in the markup. A ride app's interface is mostly boxes, rows, rounded buttons and icons, which utility classes express quickly. Some projects also use a library that wraps Tailwind classes into named styled components, for example const ActionButton = tw.div`flex bg-gray-200 flex-1 m-1 h-32 items-center flex-col justify-center rounded-lg`, so the JSX stays readable while the styles stay in Tailwind.
Setting up takes a few commands: create the Next.js app, install Tailwind with its configuration files, add the Tailwind directives to the global stylesheet, and start the development server. Tailwind scans the project's files for class names and generates only the CSS actually used, which keeps the final stylesheet small.
Two files deserve attention early. In the pages-based structure, _app.js wraps every page, so it is where the global stylesheet, including Tailwind's, is imported and where shared providers are added later, such as the one that tracks the signed-in user. The public folder holds files served as they are, such as the logo and icons, at paths like /logo.svg. The newer App Router uses a layout.js file for the same purpose. Either way, keeping global concerns in one wrapper means individual pages only describe their own content.
Laying out the home screen
The home screen of a ride app follows a familiar pattern, designed for a phone held in one hand.
| Area | Contents | Layout idea |
|---|---|---|
| Map | Interactive map centred on the user's area | Top half of the screen, fixed height |
| Header | App logo, user's name and profile picture | Flex row with space between |
| Action buttons | Ride, two-wheelers, reserve | Three equal boxes in a flex row |
| Where to? | A large button that opens the search page | Full-width, rounded, grey background |
The whole screen is a vertical flex column with h-screen, so the map and the lower panel share the height. Each action button is a link to its page; the "Where to?" button links to /search using Next.js's Link component, which switches pages on the client without a full reload and can prefetch the next page in the background. Icons and images live in the public folder and are referenced from the site root.
Building the interface as small components, Map, Header, ActionButtons and WhereToButton, makes each one easy to read and lets the map component be reused later on the confirmation page, where it will show the route.
Because the app imitates a phone interface, test it with the browser's device toolbar set to a typical phone width such as 375 pixels. Touch targets should be at least about 44 pixels tall so they are easy to tap, and text on the action buttons should stay short. A design that only looks right on a laptop screen will feel cramped or stretched on a real phone, and ride apps are used almost entirely on phones.
How web maps work
A web map is not one giant picture. It is a mosaic of square tiles, each 256 or 512 pixels across, loaded on demand for the area and zoom level being viewed. At zoom level 0 the whole world fits in one tile; each zoom level doubles the scale, so level z has 2z × 2z tiles. Vector tiles, used by Mapbox, contain shapes and labels rather than images, which the browser draws itself, so maps stay sharp at any size and can be restyled instantly.
Positions are given as longitude and latitude. Mapbox, like most web map libraries and GeoJSON, writes them in that order, longitude first, as [−122.4194, 37.7749] for San Francisco; mixing up the order is the most common bug in map code, and it usually puts the marker in the ocean or in Antarctica. Longitude runs from −180 to 180 and latitude only from −90 to 90, so a "latitude" above 90 is a sure sign the two have been swapped.
Because the Web Mercator projection stretches the map towards the poles, the ground distance covered by one pixel depends on both zoom and latitude: metres per pixel ≈ 156,543.03 × cos(latitude) / 2zoom, for 256-pixel tiles.
Adding a Mapbox map to a React component
Mapbox GL JS draws the map in a <div> using the graphics hardware. It needs an access token from a Mapbox account, set as mapboxgl.accessToken. Tokens used in the browser are public by design, so restrict them in the Mapbox dashboard to your site's addresses, and keep them in an environment variable whose name starts with NEXT_PUBLIC_ so Next.js includes it in the browser bundle.
import { useEffect, useRef } from "react";
import mapboxgl from "mapbox-gl";
mapboxgl.accessToken = process.env.NEXT_PUBLIC_MAPBOX_TOKEN;
export default function Map() {
const container = useRef(null);
useEffect(() => {
const map = new mapboxgl.Map({
container: container.current,
style: "mapbox://styles/mapbox/streets-v12",
center: [-122.4194, 37.7749],
zoom: 12,
});
return () => map.remove();
}, []);
return <div ref={container} className="flex-1" />;
}
Three details make this robust. The map is created inside useEffect, which runs only in the browser after the component has mounted; Mapbox needs the real page and its graphics context, which do not exist during server-side rendering. A ref gives the effect the actual <div> to draw into, more reliable than looking it up by id. And the effect returns a clean-up function that calls map.remove(), so navigating away does not leave an invisible map consuming memory. The empty dependency list means the map is created once, not on every render. Mapbox's stylesheet must also be imported for controls and markers to look right.
Markers, styles and what comes next
A marker shows a point, such as the user's position: new mapboxgl.Marker().setLngLat([lng, lat]).addTo(map). The style URL switches the map's look, from detailed streets to a minimal light or dark style that keeps attention on the route. Navigation controls for zoom and rotation can be added with one line each, though ride apps usually keep the map clean and rely on gestures.
To centre the map on the user, the browser's geolocation API can supply coordinates once the user grants permission; until then, a sensible default city is shown.
The map can also move smoothly. map.flyTo({ center, zoom }) animates to a new position, which helps users keep their bearings when the view changes, and map.fitBounds adjusts the centre and zoom so that a set of points fits on screen, which the confirmation page will use to show both pickup and destination. Markers can be customised with an HTML element of your own, for example a small car icon, instead of the default pin. Finally, map providers require their attribution, the small copyright notice in the corner, to remain visible; it is part of the terms of use of the map data.
With the home screen and map in place, the next steps are a search page for pickup and destination, converting the typed places into coordinates, and drawing both points and the route between them on a confirmation map.
Practice tasks
- Compute the metres per pixel at zoom 16 on the equator and at latitude 60°, where cos 60° = 0.5.
- A map is 400 pixels wide at zoom 13 on the equator. Roughly how many kilometres does it span?
- Write the coordinates of a point at latitude 51.5074 and longitude −0.1278 in the order Mapbox expects.
- Explain why the map must be created inside
useEffectin a Next.js app. - What goes wrong if the effect does not return a clean-up function, and the user moves between pages many times?
- Sketch the components of the home screen and describe which one you would reuse on later pages.
This course book is available to read online only at tech-academy.chrysolites.org.