Skip to content
import SearchViewModel from "@arcgis/core/widgets/Search/SearchViewModel.js";
Inheritance:
SearchViewModelAccessor
Since
ArcGIS Maps SDK for JavaScript 4.0

Provides the logic for the Search widget and Search component, which performs search operations on locator service(s), map/feature service feature layer(s), and/or table(s). If using a locator with a geocoding service, the findAddressCandidates operation is used, whereas queries are used on feature layers.

See also

Constructors

Constructor

Constructor
Parameters
ParameterTypeDescriptionRequired
properties
See the properties table for a list of all the properties that may be passed into the constructor.

Properties

Any properties can be set, retrieved or listened to. See the Watch for changes topic.
PropertyTypeClass
activeSource
readonly
allSources
readonly
declaredClass
readonly inherited
defaultSources
readonly
goToOverride
inherited
placeholder
readonly
resultGraphic
readonly
resultLocation
readonly
results
readonly
selectedResult
readonly
state
readonly
suggestions
readonly
updating
readonly

activeSource

readonly Property
Type
SupportedSearchSource | null | undefined

The source object currently selected. Can be either a feature layer or a locator.

activeSourceIndex

autocast Property
Type
number

The selected source's index. This value is -1 when all sources are selected.

Default value
0

allPlaceholder

Property
Type
string | null | undefined

String value used as a hint for input text when searching on multiple sources. See the image below to view the location and style of this text.

search-allPlaceholder

Default value
"Find address or place"

allSources

readonly Property
Type
Collection<SupportedSearchSource>
Since
ArcGIS Maps SDK for JavaScript 4.8

The combined collection of defaultSources and sources. The defaultSources displays first in the Search UI.

autoNavigate

Property
Type
boolean
Since
ArcGIS Maps SDK for JavaScript 4.32

Indicates whether to automatically navigate to the selected result.

Default value
true

autoSelect

Property
Type
boolean

Indicates whether to automatically select and zoom to the first geocoded result. If false, the findAddressCandidates operation will still geocode the input string, but the top result will not be selected. To work with the geocoded results, you can set up a @search-complete event handler and get the results through the event object.

Default value
true

declaredClass

readonlyinherited Property
Type
string
Inherited from: Accessor
Since
ArcGIS Maps SDK for JavaScript 4.7

The name of the class. The declared class name is formatted as esri.folder.className.

defaultPopupTemplate

autocast Property
Type
PopupTemplate | null | undefined
Since
ArcGIS Maps SDK for JavaScript 4.32

The default popupTemplate.

defaultSources

readonly Property
Type
Collection<SupportedSearchSource>
Since
ArcGIS Maps SDK for JavaScript 4.8

A read-only property that is a Collection of LayerSearchSource and/or LocatorSearchSource. This property may contain ArcGIS Portal locators and any web map or web scene configurable search sources. Web maps or web scenes may contain map/feature service feature layer(s), and/or table(s) as sources.

This property is used to populate the Search UI if the sources property is not set.

defaultSymbols

Property
Type
SearchDefaultSymbols
Since
ArcGIS Maps SDK for JavaScript 4.22

The default symbol(s) for the search result. Choosing a symbol depends on the View type (SceneView or MapView), and the geometry type of the search result. This property allows developers to overwrite the default symbology for one or more geometry types.

Example
searchViewModel.defaultSymbols = {
point: new PictureMarkerSymbol({
url: "https://freesvg.org/img/1287178070.png",
size: 24,
width: 24,
height: 24
}),
polygon: new SimpleFillSymbol({
color: [235, 235, 235, 0.4],
outline: {
color: [130, 130, 130, 1],
width: 2
}
}),
polyline: new SimpleLineSymbol({
color: [130, 130, 130, 1],
width: 2
})
};

goToOverride

inherited Property
Type
GoToOverride | null | undefined
Inherited from: GoTo
Since
ArcGIS Maps SDK for JavaScript 4.8

This function provides the ability to override either the MapView goTo() or SceneView goTo() methods.

See also
Example
// The following snippet uses Search but can be applied to any
// widgets that support the goToOverride property.
search.goToOverride = function(view, goToParams) {
goToParams.options = {
duration: updatedDuration
};
return view.goTo(goToParams.target, goToParams.options);
};

includeDefaultSources

Property
Type
boolean | SourcesHandler
Since
ArcGIS Maps SDK for JavaScript 4.8

Indicates whether or not to include defaultSources in the Search UI. This can be a boolean value or a function that returns an array of Search sources.

Default value
true
Example
// includeDefaultSources passed as a boolean value
searchViewModel.includeDefaultSources = false;
// includeDefaultSources passed as a function
searchViewModel.includeDefaultSources = function(sourcesResponse) {
return sourcesResponse.defaultSources;
};

locationEnabled

Property
Type
boolean
Since
ArcGIS Maps SDK for JavaScript 4.8

Indicates whether location services are enabled.

locationEnabled

The use of this property is only supported on secure origins. To use it, switch your application to a secure origin, such as HTTPS. Note that localhost is considered "potentially secure" and can be used for easy testing in browsers that supports Window.isSecureContext (currently Chrome and Firefox).

maxInputLength

Property
Type
number

The maximum character length of the search text.

Default value
128

maxResults

Property
Type
number

The maximum number of results returned if not specified by the source.

Default value
6

maxSuggestions

Property
Type
number

The maximum number of suggestions returned if not specified by the source.

If working with the default ArcGIS Online Geocoding service, the default remains at 5.

Default value
6

messages

Property
Type
Record<string, unknown> | null | undefined

The associated message bundle.

minSuggestCharacters

Property
Type
number

The minimum number of characters needed for the search if not specified by the source.

Default value
3

placeholder

readonly Property
Type
string

The placeholder used by the activeSource.

popupEnabled

Property
Type
boolean

Indicates whether to display the Popup on feature click. The graphic can be clicked to display a Popup.

Default value
true

popupTemplate

autocast Property
Type
PopupTemplate | null | undefined

A customized PopupTemplate for the selected feature. Note that any templates defined on allSources take precedence over those defined directly on the template.

portal

autocast Property
Type
Portal | null | undefined
Since
ArcGIS Maps SDK for JavaScript 4.8

It is possible to search a specified portal instance's locator services Use this property to set this ArcGIS Portal instance to search.

resultCount

Property
Type
number | null | undefined
Since
ArcGIS Maps SDK for JavaScript 4.32

The number of results found in the search.

resultGraphic

readonly Property
Type
Graphic | null | undefined

The graphic used to highlight the resulting feature or location.

resultGraphicEnabled

Property
Type
boolean

Indicates if the resultGraphic will display at the location of the selected feature.

A graphic will be placed in the View's View.graphics for layer views that do not support the highlight method.

Default value
true

resultLocation

readonly Property
Type
Point | null | undefined

The point used to show a popup for the result graphic.

results

readonly Property
Type
SearchResults[] | null | undefined

An array of current results from the search.

searchAllEnabled

Property
Type
boolean

Indicates whether to display the option to search all sources. When true, the "All" option is displayed by default:

search-searchAllEnabled-true

When false, no option to search all sources at once is available:

search-searchAllEnabled-true-false

Default value
true

searchTerm

autocast Property
Type
string

The value of the search box input text string.

selectedResult

readonly Property
Type
SearchResult | null | undefined

The result selected from a search.

selectedSuggestion

readonly Property
Type
SuggestResult | null | undefined

The selected SuggestResult.

sources

autocast Property
Type
Collection<SupportedSearchSource>

Search may be used to search features in a FeatureLayer or table, or geocode locations with a locator. The sources property defines the sources from which to search for the view specified by the Search instance. There are two types of sources:

Any combination of these sources may be used together in the same instance of Search.

Examples
// Default sources[] when sources is not specified
[
{
url: "https://geocode-api.arcgis.com/arcgis/rest/services/World/GeocodeServer",
singleLineFieldName: "SingleLine",
outFields: ["Addr_type"],
name: "ArcGIS World Geocoding Service",
placeholder: "Adresse",
resultSymbol: {
type: "picture-marker", // autocasts as new PictureMarkerSymbol()
url: this.basePath + "/images/search/search-symbol-32.png",
size: 24,
width: 24,
height: 24,
xoffset: 0,
yoffset: 0
}
}
]
// Example of multiple sources[]
let sources = [
{
url: "https://geocode-api.arcgis.com/arcgis/rest/services/World/GeocodeServer",
singleLineFieldName: "SingleLine",
name: "Custom Geocoding Service",
placeholder: "Search Geocoder",
maxResults: 3,
maxSuggestions: 6,
suggestionsEnabled: false,
minSuggestCharacters: 0
}, {
layer: new FeatureLayer({
url: "https://services.arcgis.com/DO4gTjwJVIJ7O9Ca/arcgis/rest/services/GeoForm_Survey_v11_live/FeatureServer/0",
outFields: ["*"]
}),
searchFields: ["Email", "URL"],
displayField: "Email",
exactMatch: false,
outFields: ["*"],
name: "Point FS",
placeholder: "example: esri",
maxResults: 6,
maxSuggestions: 6,
suggestionsEnabled: true,
minSuggestCharacters: 0
},
{
layer: new FeatureLayer({
outFields: ["*"]
});
placeholder: "esri",
name: "A FeatureLayer",
prefix: "",
suffix: "",
maxResults: 1,
maxSuggestions: 6,
exactMatch: false,
searchFields: [], // defaults to FeatureLayer.displayField
displayField: "", // defaults to FeatureLayer.displayField
minSuggestCharacters: 0
}
];
let sources = [{ ... }, { ... }, { ... }]; // array of sources
searchViewModel.sources = sources;
// Add to source(s)
searchViewModel.sources.push({ ... }); // new source

state

readonly Property
Type
SearchViewModelState
Since
ArcGIS Maps SDK for JavaScript 4.8

The current state. This property was removed from Search and should now be accessed within the SearchViewModel.

Default value
"ready"

suggestionCount

Property
Type
number | null | undefined
Since
ArcGIS Maps SDK for JavaScript 4.32

The number of suggestions found for the search.

suggestionDelay

Property
Type
number

The millisecond delay after keyup and before making a suggest() network request.

Default value
350

suggestions

readonly Property
Type
SuggestResults[] | null | undefined

An array of results from the suggest method.

This is available if working with a 10.3 or greater geocoding service that has suggest capability loaded or a 10.3 or greater feature layer that supports pagination, i.e. supportsPagination = true.

suggestionsEnabled

Property
Type
boolean

Enable suggestions.

This is only available if working with a 10.3 or greater geocoding service that has [suggest capability loaded] (https://developers.arcgis.com/rest/geocode/api-reference/geocoding-suggest.htm) or a 10.3 or greater feature layer that supports pagination, i.e. supportsPagination = true.

Default value
true

updating

readonly Property
Type
boolean
Since
ArcGIS Maps SDK for JavaScript 4.16

Indicates whether the View or Portal is loading resources prior to use. If resources are still loading, this value is false. If resources are done loading, this value is true.

Default value
false

view

Property
Type
MapViewOrSceneView | null | undefined

A reference to the MapView or SceneView. Set this to link to a specific view.

Methods

MethodSignatureClass
clear(): void
emit
inherited
emit<Type extends EventNames<this>>(type: Type, event?: this["@eventTypes"][Type]): boolean
hasEventListener
inherited
hasEventListener<Type extends EventNames<this>>(type: Type): boolean
on
inherited
on<Type extends EventNames<this>>(type: Type, listener: EventedCallback<this["@eventTypes"][Type]>): ResourceHandle
search(searchItem?: SearchItem | null | undefined, options?: AbortOptions): Promise<SearchResponse | null | undefined>
searchNearby(options?: AbortOptions): Promise<SearchResponse | null | undefined>
select(value: SearchResult | null | undefined): Promise<SearchResult>
suggest(suggestTerm?: string, suggestionDelay?: number | null | undefined, options?: AbortOptions): Promise<SuggestResponse | null | undefined>
when(): Promise<void>

clear

Method
Signature
clear (): void

Clears the current value, search results, suggest results, graphic, and graphics layer. It also hides any open menus.

Returns
void

emit

inherited Method
Signature
emit <Type extends EventNames<this>>(type: Type, event?: this["@eventTypes"][Type]): boolean
Type parameters
<Type extends EventNames<this>>
Inherited from: EventedMixin
Since
ArcGIS Maps SDK for JavaScript 4.5

Emits an event on the instance. This method should only be used when creating subclasses of this class.

Parameters
ParameterTypeDescriptionRequired
type
Type

The name of the event.

event
this["@eventTypes"][Type]

The event payload.

Returns
boolean

true if a listener was notified

hasEventListener

inherited Method
Signature
hasEventListener <Type extends EventNames<this>>(type: Type): boolean
Type parameters
<Type extends EventNames<this>>
Inherited from: EventedMixin

Indicates whether there is an event listener on the instance that matches the provided event name.

Parameters
ParameterTypeDescriptionRequired
type
Type

The name of the event.

Returns
boolean

Returns true if the class supports the input event.

on

inherited Method
Signature
on <Type extends EventNames<this>>(type: Type, listener: EventedCallback<this["@eventTypes"][Type]>): ResourceHandle
Type parameters
<Type extends EventNames<this>>
Inherited from: EventedMixin

Registers an event handler on the instance. Call this method to hook an event with a listener.

Parameters
ParameterTypeDescriptionRequired
type
Type

An event or an array of events to listen for.

listener
EventedCallback<this["@eventTypes"][Type]>

The function to call when the event fires.

Returns
ResourceHandle

Returns an event handler with a remove() method that should be called to stop listening for the event(s).

PropertyTypeDescription
removeFunctionWhen called, removes the listener from the event.
Example
view.on("click", function(event){
// event is the event handle returned after the event fires.
console.log(event.mapPoint);
});
Method
Signature
search (searchItem?: SearchItem | null | undefined, options?: AbortOptions): Promise<SearchResponse | null | undefined>

Depending on the sources specified, search() queries the feature layer(s) and/or performs address matching using any specified Locator(s) and returns the applicable results.

Parameters
ParameterTypeDescriptionRequired
searchItem

This searchItem can be a string, point geometry, suggest candidate object, or an array containing [latitude,longitude]. If a geometry is supplied, then it will reverse geocode (locator) or findAddressCandidates with geometry instead of text (featurelayer).

options

An object containing an optional signal property that can be used to cancel the request.

Returns
Promise<SearchResponse | null | undefined>

When resolved, returns an object containing an array of search results.

searchNearby

Method
Signature
searchNearby (options?: AbortOptions): Promise<SearchResponse | null | undefined>

Returns search results near your current location. It checks whether locationEnabled is true. If so, and your browser supports geolocation, it will return search results nearby your current location.

Parameters
ParameterTypeDescriptionRequired
options

An object containing an optional signal property that can be used to cancel the request.

Returns
Promise<SearchResponse | null | undefined>

When resolved, returns SearchResponse containing an array of search objects. Each of these objects contains a SearchResult.

select

Method
Signature
select (value: SearchResult | null | undefined): Promise<SearchResult>
Since
ArcGIS Maps SDK for JavaScript 4.32

Selects a result.

Parameters
ParameterTypeDescriptionRequired
value

The result object to select.

Returns
Promise<SearchResult>

When resolved, returns SearchResult.

suggest

Method
Signature
suggest (suggestTerm?: string, suggestionDelay?: number | null | undefined, options?: AbortOptions): Promise<SuggestResponse | null | undefined>

Performs a suggest() request on the active Locator. It uses the current value or one that is passed in.

Suggestions are available if working with a 10.3 or greater geocoding service that has suggest capability loaded or a 10.3 or greater feature layer that supports pagination, i.e.supportsPagination = true.

Parameters
ParameterTypeDescriptionRequired
suggestTerm

The string value used to suggest() on an active Locator or feature layer. If nothing is passed in, takes the current value.

suggestionDelay

The millisecond delay after keyup and before making a suggest() network request.

options

An object containing an optional signal property that can be used to cancel the request.

Returns
Promise<SuggestResponse | null | undefined>

When resolved, returns SuggestResponse containing an array of result objects. Each of these results contains a SuggestResult.

when

Method
Signature
when (): Promise<void>
Since
ArcGIS Maps SDK for JavaScript 4.16

when() may be leveraged once the SearchViewModel has been updated.

Returns
Promise<void>

A promise that resolves when SearchViewModel has been updated.

Events

search-clear

Event
search-clear: CustomEvent<void>

Fires when a result is cleared from the input box or a new result is selected.

bubbles composed cancelable
Example
const searchWidget = new Search();
searchWidget.on("search-clear", function(event){
console.log("Search input textbox was cleared.");
});

search-complete

Event
search-complete: CustomEvent<SearchResponse>

Fires when the Search.search() method is called and returns its results.

bubbles composed cancelable
Example
const searchWidget = new Search();
searchWidget.on("search-complete", function(event){
// The results are stored in the event Object[]
console.log("Results of the search: ", event);
});

search-start

Event
search-start: CustomEvent<void>

Fires when the Search.search() method starts.

bubbles composed cancelable
Example
const searchWidget = new Search();
searchWidget.on("search-start", function(event){
console.log("Search started.");
});

select-result

Event

Fires when a search result is selected.

bubbles composed cancelable
Example
const searchWidget = new Search();
searchWidget.on("select-result", function(event){
console.log("The selected search result: ", event);
});

suggest-complete

Event
suggest-complete: CustomEvent<SuggestResponse | null>

Fires when the Search.suggest() method is called and returns its results.

bubbles composed cancelable
Example
const searchWidget = new Search();
searchWidget.on("suggest-complete", function(event){
// The results are stored in the event Object[]
console.log("Results of suggest: ", event);
});

suggest-start

Event
suggest-start: CustomEvent<{ searchTerm: string; }>

Fires when the Search.suggest() method starts.

bubbles composed cancelable
Example
const searchWidget = new Search();
searchWidget.on("suggest-start", function(event){
console.log("suggest-start", event);
});

Type definitions

SearchViewModelState

Type definition
Type
"disabled" | "ready" | "searching" | "loading"

SearchViewModelSelectResultEvent

Type definition

popupEnabled

Property
Type
boolean

Whether the popup is enabled for the selected result.

result

Property
Type
SearchResult

An object containing the results of the search.

source

Property
Type
SupportedSearchSource

The source of the selected result. Please see Search.sources for additional information on its properties.

sourceIndex

Property
Type
number | null | undefined

The index of the source of the selected result.