--- title: Maps and Geocoding description: Turn addresses into points and back with @stacksjs/maps, choosing the provider in config/maps.ts. --- # Maps and Geocoding `@stacksjs/maps` geocodes through one contract, whatever the provider: `search`, `suggest` (typeahead), `retrieve` (a suggestion's full result), `reverse`, and `verify` (does this delivery address exist?). Commerce's delivery geocoding uses it too. ## Choose a provider `config/maps.ts` names the driver. `MAPS_DRIVER` in `.env` overrides it, so you can switch providers without changing code: ```typescript import type { MapsConfig } from '@stacksjs/types' import { env } from '@stacksjs/env' export default { driver: env.MAPS_DRIVER || 'nominatim', country: 'US', // searched within when a call names no countries language: 'en', drivers: { mapshq: { key: env.MAPSHQ_KEY }, nominatim: { userAgent: 'Acme Deliveries (ops@acme.example)' }, }, } satisfies MapsConfig ``` | Driver | Operations | Notes | | --- | --- | --- | | `nominatim` | search, reverse, verify | No account. One request a second, and no search-as-you-type, under the OpenStreetMap usage policy. Name your app and a contact in `userAgent`. | | `photon` | search, suggest, reverse | No account. Fair-use limits on the public instance. | | `mapshq` | all five | Run `buddy hq:connect mapshq`, which writes `MAPSHQ_KEY`. Typeahead is billed per session, so pass the same `sessionToken` to `suggest` and the `retrieve` that ends it. | Calling an operation the driver lacks throws `UnsupportedGeocodingOperationError`, naming both the driver and the operation. ## Geocode ```typescript import { Geocoder } from '@stacksjs/maps' const [place] = await Geocoder.search('12320 W Pico Blvd, Los Angeles', { limit: 1 }) place.center // { lat: 34.0286, lng: -118.4516 } const controller = new AbortController() const suggestions = await Geocoder.suggest(input, { sessionToken, signal: controller.signal }) ``` Options apply to one call only, and override the config: `limit`, `language`, `countries`, `proximity`, `bbox`, `signal` and `sessionToken`. ## Attribution OpenStreetMap's licence requires credit wherever its data is shown, and that covers results from Nominatim, Photon and MapsHQ. Show the driver's attribution next to the results: ```typescript const { driver } = await Geocoder.resolve() driver.attribution // 'Data © OpenStreetMap contributors, ODbL 1.0' ``` ## Testing ```typescript import { fakeGeocodingDriver, Geocoder } from '@stacksjs/maps' const driver = fakeGeocodingDriver([{ id: 'a', result: { text: '12320 W Pico Blvd', center: { lat: 34.0286, lng: -118.4516 } } }]) Geocoder.use(driver) // ... driver.calls records every call and its options Geocoder.reset() ```