Overview
Mapbox provides the mapping infrastructure for TableOrder’s context-aware ordering system:- Map rendering: Displays the restaurant location and allows users to tap pins to trigger Table or Delivery mode
- Geofencing: Calculates distance from user to restaurant using GPS coordinates
- Directions API: Computes optimized driving routes with ETA and polyline geometry
- Route visualization: Shows the delivery path on a map after checkout
TableOrder uses
@rnmapbox/maps (React Native Mapbox GL) instead of React Native Maps for better performance and native-quality rendering.Prerequisites
Before configuring Mapbox, ensure you have:- A Mapbox account (free tier available)
- Two access tokens: one public and one secret
- Expo CLI installed (
npm install -g expo-cli)
Token setup
Create a Mapbox account
Visit mapbox.com and sign up for a free account. The free tier includes:
- 50,000 free map loads per month
- 100,000 Directions API requests per month
Create a public token
Navigate to Account → Access Tokens.Your account comes with a Default Public Token that you can use immediately. Copy this token—it starts with
pk..Public tokens are safe to include in client bundles. They’re scoped to your account and can be rotated if compromised.
Create a secret token
Click Create a token and configure:
- Name:
TableOrder Download Token - Scopes: Check
DOWNLOADS:READonly - Token type: Secret
sk. and cannot be retrieved again.Service implementation
TableOrder’s Mapbox integration is encapsulated insrc/lib/services/mapboxService.ts:
src/lib/services/mapboxService.ts
Key features
Polyline decoding
Uses
@mapbox/polyline to decode compressed geometry into coordinate arrays for MapView renderingUnit conversion
Automatically converts meters to kilometers and seconds to minutes for display
Error handling
Throws descriptive errors for missing tokens, network failures, or invalid routes
Type safety
Fully typed with TypeScript interfaces matching the Mapbox API response
Directions API usage
TableOrder calls the Mapbox Directions API whenever a user completes a delivery order. The API returns:| Response Field | Type | Description |
|---|---|---|
distance | number | Total route distance in meters |
duration | number | Estimated driving time in seconds |
geometry | string | Compressed polyline (precision 5) |
Request format
Mapbox requires coordinates in longitude, latitude order (opposite of Google Maps). The service handles this conversion automatically.
Example response
DeliveryInfo object:
Shipping cost calculation
TableOrder uses a simple per-kilometer pricing model defined inconfig.ts:
src/lib/core/config.ts
calculateShippingCost function applies this rate:
src/lib/services/mapboxService.ts
Geofencing logic
TableOrder uses GPS coordinates and the Haversine formula to determine if a user is within the restaurant’s delivery radius:config.ts:
Troubleshooting
Maps display a watermark but no tiles
Cause: Missing or invalidEXPO_PUBLIC_MAPBOX_TOKEN
Solution: Verify the token in .env matches your Mapbox dashboard. Ensure it starts with pk. and has no extra whitespace.
Build fails with “Failed to download Mapbox SDK”
Cause: Missing or invalidRNMAPBOX_MAPS_DOWNLOAD_TOKEN or missing DOWNLOADS:READ scope
Solution: Create a new secret token with the correct scope. Run npx expo prebuild --clean to clear cached configuration.
Directions API returns 401 Unauthorized
Cause: Public token not properly loaded at runtime Solution: Restart the dev server and rebuild the app. Verifyprocess.env.EXPO_PUBLIC_MAPBOX_TOKEN is accessible in config.ts.
Routes display incorrectly or in wrong location
Cause: Coordinate order reversed (lat/lon instead of lon/lat) Solution: The service handles this automatically. If using custom code, ensure coordinates are passed inlongitude, latitude order to the Mapbox API.
API limits and pricing
Free tier limits
| Resource | Free Tier | Overage Cost |
|---|---|---|
| Map loads | 50,000/month | $0.50 per 1,000 |
| Directions API | 100,000/month | $0.50 per 1,000 |
| Geocoding | 100,000/month | $0.50 per 1,000 |
For a restaurant with 100 daily delivery orders, you’ll use ~3,000 Directions API requests per month—well within the free tier.
Next steps
Stripe configuration
Set up payment processing
Telegram bot
Configure receipt dispatch