Skip to main content
The Mapbox service provides routing functionality for delivery orders. It fetches driving routes between two coordinates, decodes polyline geometry, and calculates delivery information including ETA and shipping costs.

Functions

getDeliveryRoute

Fetches a driving route from origin (restaurant) to destination (customer) and returns structured delivery information.
getDeliveryRoute(
  origin: Coordinates,
  destination: Coordinates
): Promise<DeliveryInfo>
origin
Coordinates
required
Restaurant coordinates in WGS-84 format
destination
Coordinates
required
Customer delivery coordinates in WGS-84 format
DeliveryInfo
object
Complete delivery route information

Example

import { getDeliveryRoute } from '@/src/lib/services/mapboxService';

const restaurantLocation = {
  latitude: 40.7128,
  longitude: -74.0060
};

const customerLocation = {
  latitude: 40.7580,
  longitude: -73.9855
};

const deliveryInfo = await getDeliveryRoute(
  restaurantLocation,
  customerLocation
);

console.log(`Distance: ${deliveryInfo.distanceKm} km`);
console.log(`ETA: ${deliveryInfo.etaMinutes} minutes`);

Errors

Error
Error
Throws an error in the following cases:
  • EXPO_PUBLIC_MAPBOX_TOKEN is not configured
  • Mapbox API returns a non-OK status
  • No route found between the two points

calculateShippingCost

Calculates the shipping cost based on distance using the configured rate per kilometer.
calculateShippingCost(distanceKm: number): number
distanceKm
number
required
Distance in kilometers (typically from DeliveryInfo.distanceKm)
cost
number
Shipping cost in currency units (rounded to 2 decimal places)

Example

import { getDeliveryRoute, calculateShippingCost } from '@/src/lib/services/mapboxService';

const deliveryInfo = await getDeliveryRoute(origin, destination);
const shippingCost = calculateShippingCost(deliveryInfo.distanceKm);

console.log(`Shipping cost: $${shippingCost}`);

Configuration

The service requires a Mapbox API token configured in the environment:
EXPO_PUBLIC_MAPBOX_TOKEN=your_mapbox_token_here
The shipping cost calculation uses the rate from Config.restaurant.costPerKm. Refer to SETUP2.md for complete configuration details.

API reference

This service uses the Mapbox Directions API v5:
GET /directions/v5/mapbox/driving/{lon1},{lat1};{lon2},{lat2}
  ?geometries=polyline&overview=full&access_token={TOKEN}
Base URL: https://api.mapbox.com