Geocoding is a custom HTML element that renders a UI search component, allowing users to forward and reverse geocode search and control a Mapbox GL JS map.
This page includes reference documentation for the MapboxGeocoder element, part of the Mapbox Search JS Web framework.
For installation instructions and a helpful introduction to using Address Confirmation in your website or app, see our Web Geocoding Quickstart Guide.
MapboxGeocoder, also available as the element <mapbox-geocoder>,
is an element that lets you search for addresses and places using
the Mapbox Geocoding API.
It can control a Mapbox GL JS map to zoom to the selected result.
Additionally, MapboxGeocoder implements the IControl
interface.
To use this element, you must have a Mapbox access token.
new MapboxGeocoder()
const search = new MapboxGeocoder();
search.accessToken = 'YOUR_MAPBOX_ACCESS_TOKEN';
map.addControl(search);<mapbox-geocoder
access-token="YOUR_MAPBOX_ACCESS_TOKEN"
proximity="0,0"
>
</mapbox-geocoder>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);Options to pass to the underlying GeocodingCore interface.
search.options = {
language: 'en',
country: 'US',
};Options defining the behavior of web component or its underlying search functionality. Distinct from the GeocodingOptions options, these do not correspond to the core API specification.
search.componentOptions = {
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 GeocodingOptions#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 MapboxGeocoder#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 MapboxGeocoder#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 Geocoder to a Map, which handles both setting proximity and zoom after a suggestion click.
| Name | Description |
|---|---|
| map mapboxgl.Map |
const search = new MapboxGeocoder();
search.bindMap(map);Fired when the user is typing and is provided a list of suggestions.
The underlying response from GeocodingCore is passed as the event's detail.
search.addEventListener('suggest', (event) => {
const suggestions = event.detail.suggestions;
// ...
});Fired when GeocodingCore 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 GeocodingCore is passed as the event's detail.
search.addEventListener('retrieve', (event) => {
const feature = 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 MapboxGeocoder component.
search.addEventListener('blur', () => {
console.log('search box blurred');
// ...
});Options to configure component-specific Search behavior
| Name | Description |
|---|---|
| customSearch function (text: string): Promise<Array<GeocodingFeature>> | A function accepting the query string which performs supplemental search results on top of those from the Mapbox Geocoding API. Expected to return a Promise which resolves to an array of GeoJSON-like Features as described in the Mapbox Geocoding API . |
| 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.
|