This project demonstrates how to consume Baato APIs in Expo / MapLibre React Native projects. It includes implementations for map tiles, place search, reverse geocoding, place details, and directions.
- Node.js (v18 or higher)
- npm or yarn
- Expo CLI
- Android Studio (for Android development) or Xcode (for iOS development)
First, create a new Expo project and set up your environment to build a development build:
npx create-expo-app@latest
cd your-app-nameFor MapLibre React Native, you need a development build rather than using Expo Go. Follow these steps:
-
Create a development build for iOS:
npx expo run:ios
Or for Android:
npx expo run:android
-
Start the development server:
npx expo start
For more details on development builds, see the Expo documentation.
Follow the official MapLibre React Native Expo setup guide.
npm install @maplibre/maplibre-react-nativeAdd the following configuration to your app.json:
{
"expo": {
"plugins": ["@maplibre/maplibre-react-native"]
}
}After adding the plugin, rebuild your development build:
npx expo run:ios
# or
npx expo run:android- Sign up at Baato
- Get your access token from the dashboard
- Note the API URL (default:
https://api.baato.io/api/v1)
Create a .env file in your project root:
EXPO_PUBLIC_BAATO_ACCESS_TOKEN=your_baato_access_token
EXPO_PUBLIC_BAATO_API_URL=https://api.baato.io/api/v1npm install @tanstack/react-query @mapbox/polyline
npm install -D @types/mapbox__polylineConfigure MapLibre to use Baato map tiles by setting the mapStyle prop to the Baato style URL:
const mapStyle = `${process.env.EXPO_PUBLIC_BAATO_API_URL}/styles/breeze?key=${process.env.EXPO_PUBLIC_BAATO_ACCESS_TOKEN}`;
<Map
style={{ height: "100%", width: "100%" }}
mapStyle={mapStyle}
>
<Camera
initialViewState={{
center: [85.32675329244304, 27.723598579458407], // Kathmandu coordinates
zoom: 12,
}}
/>
</Map>Available Baato map styles include: breeze, dark, light, outdoor, and more. Check the Baato documentation for the complete list.
Implement place search with autocomplete using the Baato Search API:
const searchPlaces = async (query: string, lat?: number, lon?: number) => {
const params = new URLSearchParams({
q: query,
key: process.env.EXPO_PUBLIC_BAATO_ACCESS_TOKEN!,
});
if (lat !== undefined && lon !== undefined) {
params.append("lat", String(lat));
params.append("lon", String(lon));
}
const response = await fetch(
`${process.env.EXPO_PUBLIC_BAATO_API_URL}/search?` + params.toString(),
);
const result = await response.json();
return result.data;
};Key parameters:
q: Search query stringlat,lon: Optional coordinates for location-biased resultskey: Your Baato access token
Usage tips:
- Implement debouncing (300ms recommended) to reduce API calls
- Use the user's current map center for location-biased results
- Display results in a dropdown/flatlist for autocomplete functionality
Fetch detailed information about a place using the Place ID or OSM ID:
const getPlaceDetails = async (placeId?: number, osmId?: number) => {
const params = new URLSearchParams({
key: process.env.EXPO_PUBLIC_BAATO_ACCESS_TOKEN!,
});
if (placeId !== undefined) params.append("placeId", String(placeId));
if (osmId !== undefined) params.append("osmId", String(osmId));
const response = await fetch(
`${process.env.EXPO_PUBLIC_BAATO_API_URL}/places?` + params.toString(),
);
const result = await response.json();
return result.data[0];
};Common use cases:
- Display place details in a bottom sheet or modal
- Show additional information when a user selects a search result
- Fetch place type, address, and other metadata
Convert coordinates to place information (long press on map):
const reverseGeocode = async (lat: number, lng: number) => {
const params = new URLSearchParams({
lat: String(lat),
lon: String(lng),
key: process.env.EXPO_PUBLIC_BAATO_ACCESS_TOKEN!,
});
const response = await fetch(
`${process.env.EXPO_PUBLIC_BAATO_API_URL}/reverse?` + params.toString(),
);
const result = await response.json();
return result.data[0];
};Implementation:
- Add an
onLongPresshandler to your Map component - Display the result in a toast, snackbar, or custom UI component
- Show place name, address, and type to the user
Calculate routes between two or more points:
const getDirections = async (
points: [number, number][],
mode: string = "car",
) => {
const params = new URLSearchParams({
mode: mode,
key: process.env.EXPO_PUBLIC_BAATO_ACCESS_TOKEN!,
instructions: "true",
alternatives: "true",
});
points.forEach((point) => {
params.append("points[]", `${point[1]},${point[0]}`);
});
const response = await fetch(
`${process.env.EXPO_PUBLIC_BAATO_API_URL}/directions?` + params.toString(),
);
const result = await response.json();
return result.data[0];
};Travel modes: car, bike, foot
Displaying the route:
-
Decode the encoded polyline using
@mapbox/polyline:import * as polyline from "@mapbox/polyline"; const geojson = polyline.toGeoJSON(route.encodedPolyline);
-
Render the route on the map using GeoJSONSource and Layer:
<GeoJSONSource data={geojson}> <Layer type="line" layout={{ "line-join": "round", "line-cap": "round" }} paint={{ "line-color": "#2563eb", "line-width": 6 }} /> </GeoJSONSource>
-
Add markers for origin and destination points
Route information available:
distanceInMeters: Total distancetimeInMs: Estimated travel timeinstructionList: Turn-by-turn directionsencodedPolyline: Route geometry
-
Install dependencies:
npm install
-
Set up your
.envfile with your Baato credentials -
Run the development build:
npx expo run:ios # or npx expo run:android -
Start the development server:
npx expo start
- Map Tiles: Baato map style integration using the breeze style
- Place Search: Autocomplete search with location-based results
- Reverse Geocoding: Long press on map to get location information
- Place Details: Bottom sheet displaying place information
- Directions: Route calculation with multiple travel modes (car, bike, foot)
For detailed API documentation, visit Baato API Documentation.
For issues and questions, visit Baato Support.