- MapboxGeocoder
- Parameters
- Examples
- 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
- on
- off
- relatedTarget
A geocoder component using Mapbox Geocoding API
options
Objectoptions.accessToken
String Required.options.origin
String Use to set a custom API origin. Defaults to https://api.mapbox.com.options.mapboxgl
Object? A mapbox-gl instance to use when creating Markers. Required ifoptions.marker
is true.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)? 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, the object will be passed to the map method to specify a custom animation when a result is selected.options.placeholder
String Override the default placeholder attribute value. (optional, default"Search"
)options.proximity
Object? a proximity argument: this is a geographical point given as an object with latitude and longitude properties. Search results closer to this point will be given higher priority.options.trackProximity
Boolean If true, the geocoder proximity will automatically update based on the map view. (optional, defaulttrue
)options.collapsed
Boolean If true, the geocoder control will collapse until hovered or in focus. (optional, defaultfalse
)options.clearAndBlurOnEsc
Boolean If true, the geocoder control will clear it's contents and blur when user presses the escape key. (optional, defaultfalse
)options.clearOnBlur
Boolean If true, 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, you should specify one type. 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.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.reverseMode
("distance"
|"score"
) Set the factors that are used to sort nearby results. (optional, default'distance'
)options.reverseGeocode
boolean? Enable reverse geocoding. Defaults to false. Expects coordinates to be lat, lon.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. 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.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, default'mapbox.places'
)options.localGeocoderOnly
Boolean? Iftrue
, indicates that the localGeocoder results should be the only ones returned to the user. Iffalse
, indicates that the localGeocoder results should be combined with those from the Mapbox API.
var geocoder = new MapboxGeocoder({ accessToken: mapboxgl.accessToken });
map.addControl(geocoder);
Returns MapboxGeocoder this
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 The new options.proximity value. This is a geographical point given as an object with latitude and longitude properties.
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 MapboxGeocoder this
Set the flyTo options
flyTo
(Object | Boolean) 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, the object will be passed to the flyTo map method to specify a custom animation.
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
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
If relatedTarget is not found, assume user targeted the suggestions list.
In that case, do not clear on blur. There are other edge cases where
ev.relatedTarget could be null. Clicking on list always results in null
relatedtarget because of upstream behavior in suggestions
.
The ideal solution would be to check if ev.relatedTarget is a child of the list. See issue #258 for details on why we can't do that yet.