Complete example
The JavaScript tab is the whole locator in one HTML page, and the other tabs run the same address search and nearby search in a native app.Prerequisites
- A publishable key. See Choose the right API key. Never put a secret key in client code.
- Your locations, each with a name, coordinates or an address, and an ID from your own system.
- Web: The examples use
radar-sdk-js5.3.0,@radarlabs/plugin-maps1.2.0, and@radarlabs/plugin-autocomplete1.2.0. - No location permission. The “Use my location” button asks for it only when the visitor selects it.
Load your locations
The locator searches geofences in your Radar project. Import your locations as geofences with the sametag (the example uses store), your own ID as the externalId, the display name as the description, and the fields you want to show as metadata. See CSV import.
Geofence metadata is returned to anyone who can run a search with your publishable key. Put only public information in it.
Accept an address
Radar.ui.autocomplete() renders a search input that suggests addresses, cities, and postal codes. When the visitor chooses a suggestion, onSelection receives an address with latitude, longitude, and formattedAddress, and the example uses those coordinates as the search origin.
This is the primary path, and it needs no location permission. For the component’s options, see Autocomplete, geocode, and validate addresses.
Accept the user’s location
The “Use my location” button callsRadar.getLocation(), which triggers the browser’s permission prompt. Call it from a button, not on page load, so the visitor understands why they are being asked. If the visitor denies permission, the example tells them to search by address and the rest of the page keeps working.
The last block of the example calls Radar.ipGeocode() to run a first search with no prompt. An IP-derived location is an estimate that can be wrong by a large distance, so label it as an estimate and let an address search replace it.
Search nearby locations
Radar.searchGeofences() returns the geofences nearest to near, sorted by straight-line distance.
Each geofence has
description, tag, externalId, metadata, and geometryCenter, a GeoJSON point in [longitude, latitude] order. For every parameter and field, see Search geofences.
Show the list and the map
The example converts the response into onelocations array and renders both the list and the markers from it in a single loop. That keeps the two views in sync: the same locations, in the same order, with each list button holding a reference to its marker. Before each search it clears the list and calls map.clearMarkers(), and afterward it calls map.fitToMarkers().
Selecting a list entry and selecting a marker both call selectLocation(), so they lead to the same state: the entry is highlighted and scrolled into view, its popup is open, and the map moves to it.
List content is written with textContent and popups use the text option, so names and metadata are never interpreted as markup.
Show route distance and travel time
Search results are ordered by straight-line distance, which ignores roads. Route distance and travel time follow roads and cost one extra request, so label each number for what it is. When a location is selected, the example callsRadar.distance() and replaces the straight-line label with routes.car.distance.text and routes.car.duration.text. If the request fails or no drivable route exists, the straight-line label stays. To show travel time for every result at once, make one Radar.matrix() request after the search:
Variations
A branch, ATM, or dealer finder is the same flow with different tags and metadata: import each type with its own tag, search several at once withtags: ['branch', 'atm'], and read each result’s tag to choose a marker color or icon. If your locations already come from your own API, skip geofences: build the same locations array from your list, sort it by straightLineMiles() from the search origin, and render from it. To find public places, such as a chain or a category of business, use Radar.searchPlaces(). See Search places.
Native apps
The same flow works in a native app. The Radar iOS, Android, and React Native SDKs provideRadar.searchGeofences, Radar.autocomplete, Radar.geocode, and Radar.getDistance, and the native tabs in Complete example geocode a typed address and search near it. To render the map, see Compatibility. For setup, see iOS SDK, Android SDK, and React Native SDK.
Handle results and errors
EveryRadar.* method in the example returns a promise that rejects with a RadarError. Read its status.
Test the integration
- Passing case: Block location for the page in your browser’s site settings, type an address, and choose a suggestion. The same locations appear in the list and on the map, nearest first, and selecting one shows a driving distance and time.
- Failing case: Search for a city where you have no locations. The empty state names the place you searched, and the next search works.
Production checklist
- The page uses the Live publishable key, and your locations are imported into the Live environment with the same tags.
- No secret key appears in client code, the page source, or a client-side environment variable.
- Geofence metadata contains only information you intend to be public.
- Address search is the default path, and location permission is requested only from a user action.
- Empty and error states exist for search, and the list works if the map fails.
- Straight-line and route distances are labeled, and the list and map use the same order.