Search Box provides a rich UI for single-box location search, allowing your users to search for addresses and points of interest by place name, address, or category.
This page includes reference documentation for the MapboxSearchBox element, a part of the Mapbox Search JS Web framework.
For installation instructions and a helpful introduction to using Search Box in your website or app, see our Web Search Box Quickstart Guide.
MapboxSearchBox, also available as the element <mapbox-search-box>,
is an element that lets you search for places, addresses, and landmarks using
the Mapbox Search Box API.
It can control a Mapbox GL JS map to zoom to the selected result.
Additionally, MapboxSearchBox implements the IControl
interface.
To use this element, you must have a Mapbox access token.
new MapboxSearchBox()
const search = new MapboxSearchBox();
search.accessToken = 'YOUR_MAPBOX_ACCESS_TOKEN';
map.addControl(search);<mapbox-search-box
access-token="YOUR_MAPBOX_ACCESS_TOKEN"
proximity="0,0"
>
</mapbox-search-box>The Mapbox access token to use for all requests.
search.accessToken = 'pk.my-mapbox-access-token';The value of the input element.
console.log(search.value);API-specific options to pass to the underlying SearchBoxCore interface.
search.options = {
language: 'en',
country: 'US',
};Options defining the behavior of web component or its underlying search functionality. Distinct from the SearchBoxOptions options, these do not correspond to the core API specification.
search.componentOptions = {
allowReverse: true,
flipCoordinates: true,
flyTo: true
};The PopoverOptions to define popover positioning.
search.popoverOptions = {
placement: 'top-start',
flip: true,
offset: 5
};The input element's placeholder text. The default value may be localized if SearchBoxOptions#language is set.
A callback providing the opportunity to validate and/or manipulate the input text before it triggers a search, for example by using a regular expression.
If a truthy string value is returned, it will be passed into the underlying search API. If null, undefined or empty string is returned, no search request will be performed.
Enable search only when the input value length is more than 3 characters.
search.interceptSearch = (val) => val?.length > 3 ? val : null;A mapbox-gl instance to use when creating Markers. Required if MapboxSearchBox#marker is true.
If true, a Marker will be added to the map at the location of the user-selected result using a default set of Marker options. If the value is an object, the marker will be constructed using these options. If false, no marker will be added to the map. Requires that MapboxSearchBox#mapboxgl also be set.
search.marker = {
color: 'red',
draggable: true
};Focuses the input element.
Sets the input text and triggers a search programmatically
| Name | Description |
|---|---|
| text string |
Connects the search box to a Map, which handles both setting proximity and zoom after a suggestion click.
| Name | Description |
|---|---|
| map mapboxgl.Map |
const search = new MapboxSearchBox();
search.bindMap(map);Fired when the user is typing and is provided a list of suggestions.
The underlying response from SearchBoxCore is passed as the event's detail.
search.addEventListener('suggest', (event) => {
const suggestions = event.detail.suggestions;
// ...
});Fired when SearchBoxCore has errored providing a list of suggestions.
The underlying error is passed as the event's detail.
search.addEventListener('suggesterror', (event) => {
const error = event.detail;
// ...
});Fired when the user has selected a suggestion.
The underlying response from SearchBoxCore is passed as the event's detail.
search.addEventListener('retrieve', (event) => {
const featureCollection = event.detail;
// ...
});Fired when the user has changed the <input> text.
The new input value is passed as the event's detail.
search.addEventListener('input', (event) => {
if (e.target !== e.currentTarget) return;
const searchText = event.detail;
// ...
});Fired when the user has cleared the box, either by deleting all text or clicking the "Clear" icon.
search.addEventListener('clear', () => {
console.log('search box cleared');
// ...
});Fired when the user moved focus away from the MapboxSearchBox component.
search.addEventListener('blur', () => {
console.log('search box blurred');
// ...
});Options to configure component-specific Search behavior
| Name | Description |
|---|---|
| allowReverse boolean | Allow the user to query a coordinate string (e.g. "lng,lat") to get a reverse search result. |
| customSearch function (text: string): Promise<Array<SearchBoxSuggestion>> | A function accepting the query string which performs supplemental search results
on top of those from the Mapbox Search Box API. Expected to return a Promise
which resolves to an array of suggestions as described in the
Mapbox Search Box API
.
Additionally, each suggestion must include a
_geometry
property matching the
"geometry" object format specified in the
retrieved Feature
format.
|
| flipCoordinates boolean | If true, the coordinates in the query string are expected to be (lat,lng) instead of (lng,lat). |
| flyTo (mapboxgl.FlyToOptions | boolean) | If
false
, animating the map to a selected result is disabled. If
true
(default), animating the map will use the default animation parameters. If an object, it will be passed as
options
to the map
flyTo
method.
|