Features used
Getting started
If you haven’t already, sign up for Radar to get your API key. Then add the Radar SDK to your application.With npm or yarn
Add theradar-sdk-js and maplibre-gl packages:
With HTML
Add the following script to your HTML file:Create a map
Once the SDK is installed, you can render a Radar Map. The map serves as the foundation for your store locator, providing an interactive interface for users to explore store locations. Specify the HTML element where you want to render the map by providing the element’s ID or the element object itself. For more options to configure a map, see our Maps documentation.
Add a custom map style
Radar Maps Studio allows you to create fully customizable maps for your store locator. This tool enables you to select custom styles, fonts, colors, and more, all within the Radar dashboard. Follow this tutorial to create a custom map style. Add this style to your code as shown below.
Center the map with IP geocoding
Use Radar’s IP geocode API to localize the store locator to the user’s city without requesting location permissions. Below is sample code to call the API.
Add markers and popups to the map
Populate your map with markers to show the locations of each store. You’ll need a data source with your store coordinates and information like name and address. Each marker can include a popup that displays when users click on it, showing detailed store information. This provides users with quick access to the necessary details for visiting a location.
Add autocomplete for store search
The Radar autocomplete component allows users to search for addresses and locations, making it easy to find stores near a specific place.Integrating with a sidebar
For an existing store locator, the autocomplete component fits naturally at the top of this sidebar as a search input. If this store locator is net-new, consider adding a simple container positioned beside or overlaying your map where you display the store list and autocomplete search. A typical layout structure looks like this:
Recenter the map based on user searches
When a user searches for and selects a location, recenter the map to their selection. This creates a responsive experience where the search interface and map work together seamlessly. Update theonSelection callback to recenter the map. The flyTo method provides a smooth transition.
Calculate and display distances to stores
Show users how far each store is from the map’s center point using Radar’s matrix API. The distance calculations help users quickly identify their most convenient option. Set the map center as theorigins value and pass the list of store locations as the destinations value as a pipe-delimited string. Reference our matrix API documentation for additional information.
Once calculated, display these distance values in your sidebar next to each store. Make sure to recalculate distances as the map center changes.
