How to add a map to your app
A map is two jobs: drawing tiles in the browser and turning addresses into coordinates. One key can be public, the other never should be.

Key takeaways
- A map has two parts: a tile layer drawn in the browser and geocoding (address to coordinates) that should run on your server.
- Map display keys are visible to every visitor by design, so restrict them to your domain. Geocoding and routing keys go in Backend → Secrets.
- Store latitude and longitude on each row when it is saved. Geocoding on every page view is slow, costly and often against the provider terms.
To add a map to your app, pick a map provider (Mapbox, Google Maps Platform, MapLibre with a tile host, or Leaflet with OpenStreetMap-based tiles), create an account and a key, render the map component in your frontend, and store a latitude and longitude on every record you want to pin. Addresses get turned into coordinates once, on the server, when a record is saved. That split, display in the browser and lookups on the server, is the whole design, and most map bugs come from mixing the two up.
What you'll build
The running example is a directory of venues: each venue has an address, the map shows a pin per venue, clicking a pin opens a card, and a search box finds venues near a typed place. The same pieces cover a delivery tracker, a real estate listing site or a store locator; only the data changes.
- A map view that loads tiles, fits the visible pins and clusters them when there are many.
- A geocode step that runs when a venue is created or its address changes, writing `lat` and `lng` to the row.
- A nearby search that geocodes the visitor's typed place on the server, then queries your own table by distance.
What you need before you start
- 01A provider account. Mapbox and Google Maps Platform both require an account, and Google requires a billing account even for its free monthly allowance. Free tiers and per-request prices change, so read the current pricing page of whichever you pick before launch.
- 02Two keys if your provider allows it. A public display token for the map itself, and a separate server key for geocoding. Mapbox splits these into public (`pk.`) and secret (`sk.`) tokens; with Google, create two keys and restrict each to the APIs it needs.
- 03A backend. Geocoding runs in an edge function, so the project needs Startbase Cloud or a connected Supabase project.
- 04Coordinates in your data model. Add `lat` (numeric), `lng` (numeric) and `geocoded_at` (timestamp) to the table you are mapping.
Which key is public and which is secret
This is where map integrations differ from almost every other integration. The map library runs in the visitor's browser and must send a key with every tile request, so the display key is public by design. Anyone can open developer tools and copy it. You cannot hide it; you can only limit what it does.
- Display key: goes in the app's client environment, for example `VITE_MAPBOX_TOKEN` in the workspace `.env`. Restrict it in the provider dashboard to your published domain (your `*.startbase.app` address and any custom domain) and to map display scopes only.
- Geocoding or routing key: goes in Backend → Secrets as something like `MAPBOX_SECRET_TOKEN` or `GOOGLE_GEOCODING_KEY`. Only your edge function reads it. Startbase rejects secret names that start with `VITE_`, `EXPO_PUBLIC_` or `NEXT_PUBLIC_`, which is a useful guardrail against mixing the two.
Why bother with the split if the display key is restricted? Because referrer restrictions are checked by the provider using a header a script can fake, so a leaked unrestricted geocoding key is an open tab on your bill. Keep the expensive calls behind your own function where you can add sign-in checks and rate limits.
The data flow, step by step
Saving a venue
- 01The owner submits the form with `address: "18 Harbor Street, Portland"`.
- 02The app inserts the row, then calls the `geocode-venue` function with the venue id.
- 03The function reads the address from the database (not from the request body), calls the provider's geocoding API with the secret key, and takes the first result above a confidence threshold.
- 04It writes `lat: 45.5231`, `lng: -122.6765`, `geocoded_at: now()` back to the row. If nothing matched, it sets a `geocode_status` of `not_found` so the form can ask the owner to fix the address or drop a pin by hand.
Showing the map
The map page loads venues with their stored coordinates straight from your database, no provider call per row. The library draws the tiles with the public key and places one marker per venue. Above roughly 100 to 200 pins, turn on clustering so the map stays readable and fast; treat that number as a starting point and test on a mid-range phone.
Searching nearby
The visitor types "Pearl District". The app sends it to a `geocode-query` function, which returns one coordinate pair. The app then asks your own database for venues within, say, 5 km of that point, sorted by distance. For a few thousand rows a simple bounding-box filter plus a distance sort is fine; for more, ask for the PostGIS extension and a spatial index.
A paste-ready brief
Add a map to my venues directory using Mapbox. Use a public Mapbox token from the client env (VITE_MAPBOX_TOKEN) for display only. Add lat, lng, geocoded_at and geocode_status columns to venues. Write an edge function geocode-venue that reads the venue's address from the database, geocodes it with MAPBOX_SECRET_TOKEN from Secrets, and saves the coordinates; call it after create and after any address change. Show all venues as clustered pins, open a card with name, address and a Directions link on click, and fit the map to the visible pins. Add a 'near a place' search that geocodes the typed text server-side and lists venues within 5 km by distance. Cache query geocodes in a geocode_cache table for 30 days. Only signed-in users can trigger geocode-venue.
Before you paste it, add your secret token in Backend → Secrets, and put the public token in the workspace `.env`. Swap Mapbox for Google Maps Platform or MapLibre if you prefer; the shape of the brief stays the same.
Maps in an Expo app
For mobile projects the common choice is `react-native-maps`, which runs in Expo Go, so you can try the map on your phone from the preview. Apple Maps is the default on iOS. The server side does not change at all: the same geocoding function and the same stored coordinates serve both your web and mobile frontends. Asking for the device's location needs a permission prompt; write a one-line reason the user will understand, and keep a manual search for people who say no.
Pitfalls that cost money or trust
- Geocoding on every render. A list page that geocodes 50 addresses on load makes 50 paid calls per visitor and breaks most providers' caching terms. Geocode on write, store the result.
- Free geocoders used like paid ones. The public Nominatim service allows at most one request per second and requires an identifying user agent. Fine for an admin batch job, wrong for user-facing search.
- An unrestricted display key. Without a domain restriction, anyone can reuse your token on their own site and you pay for their traffic.
- Trusting coordinates from the browser. If pins mean something (a delivery zone, a check-in), compute or verify location server-side.
- Privacy. A home address on a public map is personal data. Round coordinates or show an area for people, and say so in your privacy policy.
- Map tiles on a slow phone. Lazy-load the map below the fold, and render a static list first so the page is useful before the tiles arrive.
For a fuller picture of where pins fit in a product, the directory website guide covers listings, search and moderation around the map.
Frequently asked questions
Is it safe to put a map API key in my frontend code?
Only the display key, and only when it is restricted to your domain and to map display. Keys for geocoding, routing or anything billed per request belong in Backend → Secrets, read by an edge function.
Do I need Google Maps, or is there a free option?
Leaflet and MapLibre are free libraries, and OpenStreetMap data is free to use with attribution. You still need a tile source that allows your traffic, which usually means a commercial tile provider with its own free tier, or hosting tiles yourself.
How do I show the closest locations to a user?
Turn the user location or typed place into one coordinate pair, then query your own table by distance. Store coordinates on each row ahead of time so the search never calls the provider per result.
Can the map work in a mobile app built with Expo?
Yes. react-native-maps works in Expo Go, so you can test on your phone from the preview, and the same backend geocoding function serves the web and mobile versions.

