- getFooterNode
- MapboxGeocoder
- Parameters
- Examples
- addTo
- clear
- query
- setInput
- setProximity
- getProximity
- setRenderFunction
- getRenderFunction
- setLanguage
- getLanguage
- getZoom
- setZoom
- getFlyTo
- setFlyTo
- getPlaceholder
- setPlaceholder
- getBbox
- setBbox
- getCountries
- setCountries
- getTypes
- setTypes
- getMinLength
- setMinLength
- getLimit
- setLimit
- getFilter
- setFilter
- setOrigin
- getOrigin
- setAccessToken
- setAutocomplete
- getAutocomplete
- setFuzzyMatch
- getFuzzyMatch
- setRouting
- getRouting
- setWorldview
- getWorldview
- on
- off
- transformFeatureToGeolocationText
- getAddressInfo
Don't include this as part of the options object when creating a new MapboxGeocoder instance.
A geocoder component using the Mapbox Geocoding API
options
Objectoptions.accessToken
String Required.options.origin
String Use to set a custom API origin. (optional, defaulthttps://api.mapbox.com
)options.mapboxgl
Object? A mapbox-gl instance to use when creating Markers. Required ifoptions.marker
istrue
.options.zoom
Number On geocoded result what zoom level should the map animate to when abbox
isn't found in the response. If abbox
is found the map will fit to thebbox
. (optional, default16
)options.flyTo
(Boolean | Object) Iffalse
, animating the map to a selected result is disabled. Iftrue
, animating the map will use the default animation parameters. If an object, it will be passed asoptions
to the mapflyTo
orfitBounds
method providing control over the animation of the transition. (optional, defaulttrue
)options.placeholder
String Override the default placeholder attribute value. (optional, defaultSearch
)options.proximity
(Object |"ip"
)? a geographical point given as an object withlatitude
andlongitude
properties, or the string 'ip' to use a user's IP address location. Search results closer to this point will be given higher priority.options.trackProximity
Boolean Iftrue
, the geocoder proximity will dynamically update based on the current map view or user's IP location, depending on zoom level. (optional, defaulttrue
)options.collapsed
Boolean Iftrue
, the geocoder control will collapse until hovered or in focus. (optional, defaultfalse
)options.clearAndBlurOnEsc
Boolean Iftrue
, the geocoder control will clear it's contents and blur when user presses the escape key. (optional, defaultfalse
)options.clearOnBlur
Boolean Iftrue
, the geocoder control will clear its value when the input blurs. (optional, defaultfalse
)options.bbox
Array? a bounding box argument: this is a bounding box given as an array in the format[minX, minY, maxX, maxY]
. Search results will be limited to the bounding box.options.countries
string? a comma separated list of country codes to limit results to specified country or countries.options.types
string? a comma seperated list of types that filter results to match those specified. See https://docs.mapbox.com/api/search/#data-types for available types. If reverseGeocode is enabled and no type is specified, the type defaults to POIs. Otherwise, if you configure more than one type, the first type will be used.options.minLength
Number Minimum number of characters to enter before results are shown. (optional, default2
)options.limit
Number Maximum number of results to show. (optional, default5
)options.language
string? Specify the language to use for response text and query result weighting. Options are IETF language tags comprised of a mandatory ISO 639-1 language code and optionally one or more IETF subtags for country or script. More than one value can also be specified, separated by commas. Defaults to the browser's language settings.options.filter
Function? A function which accepts a Feature in the Carmen GeoJSON format to filter out results from the Geocoding API response before they are included in the suggestions list. Returntrue
to keep the item,false
otherwise.options.localGeocoder
Function? A function accepting the query string which performs local geocoding to supplement results from the Mapbox Geocoding API. Expected to return an Array of GeoJSON Features in the Carmen GeoJSON format.options.externalGeocoder
Function? A function accepting the query string and current features list which performs geocoding to supplement results from the Mapbox Geocoding API. Expected to return a Promise which resolves to an Array of GeoJSON Features in the Carmen GeoJSON format.options.reverseMode
(distance | score) Set the factors that are used to sort nearby results. (optional, defaultdistance
)options.reverseGeocode
boolean Iftrue
, enable reverse geocoding mode. In reverse geocoding, search input is expected to be coordinates in the formlat, lon
, with suggestions being the reverse geocodes. (optional, defaultfalse
)options.flipCoordinates
boolean Iftrue
, search input coordinates for reverse geocoding is expected to be in the formlon, lat
instead of the defaultlat, lon
. (optional, defaultfalse
)options.enableEventLogging
Boolean Allow Mapbox to collect anonymous usage statistics from the plugin. (optional, defaulttrue
)options.marker
(Boolean | Object) Iftrue
, 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. Iffalse
, no marker will be added to the map. Requires thatoptions.mapboxgl
also be set. (optional, defaulttrue
)options.render
Function? A function that specifies how the results should be rendered in the dropdown menu. This function should accepts a single Carmen GeoJSON object as input and return a string. Any HTML in the returned string will be rendered.options.getItemValue
Function? A function that specifies how the selected result should be rendered in the search bar. This function should accept a single Carmen GeoJSON object as input and return a string. HTML tags in the output string will not be rendered. Defaults to(item) => item.place_name
.options.mode
String A string specifying the geocoding endpoint to query. Options aremapbox.places
andmapbox.places-permanent
. Themapbox.places-permanent
mode requires an enterprise license for permanent geocodes. (optional, defaultmapbox.places
)options.localGeocoderOnly
Boolean Iftrue
, indicates that thelocalGeocoder
results should be the only ones returned to the user. Iffalse
, indicates that thelocalGeocoder
results should be combined with those from the Mapbox API with thelocalGeocoder
results ranked higher. (optional, defaultfalse
)options.autocomplete
Boolean Specify whether to return autocomplete results or not. When autocomplete is enabled, results will be included that start with the requested string, rather than just responses that match it exactly. (optional, defaulttrue
)options.fuzzyMatch
Boolean Specify whether the Geocoding API should attempt approximate, as well as exact, matching when performing searches, or whether it should opt out of this behavior and only attempt exact matching. (optional, defaulttrue
)options.routing
Boolean Specify whether to request additional metadata about the recommended navigation destination corresponding to the feature or not. Only applicable for address features. (optional, defaultfalse
)options.worldview
String Filter results to geographic features whose characteristics are defined differently by audiences belonging to various regional, cultural, or political groups. (optional, default"us"
)options.enableGeolocation
Boolean Iftrue
enable user geolocation feature. (optional, defaultfalse
)options.addressAccuracy
("address"
|"street"
|"place"
|"country"
) The accuracy for the geolocation feature with which we define the address line to fill. The browser API returns the user's position with accuracy, and sometimes we can get the neighbor's address. To prevent receiving an incorrect address, you can reduce the accuracy of the definition. (optional, default"street"
)
var geocoder = new MapboxGeocoder({ accessToken: mapboxgl.accessToken });
map.addControl(geocoder);
Returns MapboxGeocoder this
Add the geocoder to a container. The container can be either a mapboxgl.Map
, an HTMLElement
or a CSS selector string.
If the container is a mapboxgl.Map
, this function will behave identically to Map.addControl(geocoder)
.
If the container is an instance of HTMLElement
, then the geocoder will be appended as a child of that HTMLElement
.
If the container is a CSS selector string, the geocoder will be appended to the element returned from the query.
This function will throw an error if the container is none of the above.
It will also throw an error if the referenced HTML element cannot be found in the document.body
.
For example, if the HTML body contains the element <div id='geocoder-container'></div>
, the following script will append the geocoder to #geocoder-container
:
var geocoder = new MapboxGeocoder({ accessToken: mapboxgl.accessToken });
geocoder.addTo('#geocoder-container');
container
(String | HTMLElement | mapboxgl.Map) A reference to the container to which to add the geocoder
Clear and then focus the input.
ev
Event? the event that triggered the clear, if available
Set & query the input
searchInput
string location name or other search input
Returns MapboxGeocoder this
Set input
searchInput
string location name or other search input
Returns MapboxGeocoder this
Set proximity
proximity
(Object |"ip"
) The newoptions.proximity
value. This is a geographical point given as an object withlatitude
andlongitude
properties or the string 'ip'.disableTrackProximity
Boolean If true, setstrackProximity
to false. True by default to preventtrackProximity
from unintentionally overriding an explicitly set proximity value. (optional, defaulttrue
)
Returns MapboxGeocoder this
Get proximity
Returns Object The geocoder proximity
Set the render function used in the results dropdown
fn
Function The function to use as a render function. This function accepts a single Carmen GeoJSON object as input and returns a string.
Returns MapboxGeocoder this
Get the function used to render the results dropdown
Returns Function the render function
Get the language to use in UI elements and when making search requests
Look first at the explicitly set options otherwise use the browser's language settings
language
String Specify the language to use for response text and query result weighting. Options are IETF language tags comprised of a mandatory ISO 639-1 language code and optionally one or more IETF subtags for country or script. More than one value can also be specified, separated by commas.
Returns MapboxGeocoder this
Get the language to use in UI elements and when making search requests
Returns String The language(s) used by the plugin, if any
Get the zoom level the map will move to when there is no bounding box on the selected result
Returns Number the map zoom
Set the zoom level
zoom
Number The zoom level that the map should animate to when abbox
isn't found in the response. If abbox
is found the map will fit to thebbox
.
Returns MapboxGeocoder this
Get the parameters used to fly to the selected response, if any
Returns (Boolean | Object) The flyTo
option
Set the flyTo options
flyTo
(Boolean | Object) If false, animating the map to a selected result is disabled. If true, animating the map will use the default animation parameters. If an object, it will be passed asoptions
to the mapflyTo
orfitBounds
method providing control over the animation of the transition.
Get the value of the placeholder string
Returns String The input element's placeholder value
Set the value of the input element's placeholder
placeholder
String the text to use as the input element's placeholder
Returns MapboxGeocoder this
Get the bounding box used by the plugin
Returns Array<Number> the bounding box, if any
Set the bounding box to limit search results to
Returns MapboxGeocoder this
Get a list of the countries to limit search results to
Returns String a comma separated list of countries to limit to, if any
Set the countries to limit search results to
countries
String a comma separated list of countries to limit to
Returns MapboxGeocoder this
Get a list of the types to limit search results to
Returns String a comma separated list of types to limit to
Set the types to limit search results to
types
countries
String a comma separated list of types to limit to
Returns MapboxGeocoder this
Get the minimum number of characters typed to trigger results used in the plugin
Returns Number The minimum length in characters before a search is triggered
Set the minimum number of characters typed to trigger results used by the plugin
minLength
Number the minimum length in characters
Returns MapboxGeocoder this
Get the limit value for the number of results to display used by the plugin
Returns Number The limit value for the number of results to display used by the plugin
Set the limit value for the number of results to display used by the plugin
limit
Number the number of search results to return
Returns MapboxGeocoder
Get the filter function used by the plugin
Returns Function the filter function
Set the filter function used by the plugin.
filter
Function A function which accepts a Feature in the Carmen GeoJSON format to filter out results from the Geocoding API response before they are included in the suggestions list. Returntrue
to keep the item,false
otherwise.
Returns MapboxGeocoder this
Set the geocoding endpoint used by the plugin.
origin
Function A function which accepts an HTTPS URL to specify the endpoint to query results from.
Returns MapboxGeocoder this
Get the geocoding endpoint the plugin is currently set to
Returns Function the endpoint URL
Set the accessToken option used for the geocoding request endpoint.
accessToken
String value
Returns MapboxGeocoder this
Set the autocomplete option used for geocoding requests
value
Boolean The boolean value to set autocomplete to
Get the current autocomplete parameter value used for requests
Returns Boolean The autocomplete parameter value
Set the fuzzyMatch option used for approximate matching in geocoding requests
value
Boolean The boolean value to set fuzzyMatch to
Get the current fuzzyMatch parameter value used for requests
Returns Boolean The fuzzyMatch parameter value
Set the routing parameter used to ask for routable point metadata in geocoding requests
value
Boolean The boolean value to set routing to
Get the current routing parameter value used for requests
Returns Boolean The routing parameter value
Set the worldview parameter
code
String The country code representing the worldview (e.g. "us" | "cn" | "jp", "in")
Get the current worldview parameter value used for requests
Returns String The worldview parameter value
Subscribe to events that happen within the plugin.
type
String name of event. Available events and the data passed into their respective event objects are:- clearEmitted when the input is cleared
- loading
{ query } Emitted when the geocoder is looking up a query
- results
{ results } Fired when the geocoder returns a response
- result
{ result } Fired when input is set
- error
{ error } Error as string
- loading
fn
Function function that's called when the event is emitted.
Returns MapboxGeocoder this;
Remove an event
Returns MapboxGeocoder this
This function transforms the feature from reverse geocoding to plain text with specified accuracy
This function transforms the feature from reverse geocoding to AddressInfo object
feature
object
Returns object