require(["esri/widgets/Search/SearchViewModel"], function(SearchVM) { /* code goes here */ });
Class: esri/widgets/Search/SearchViewModel
Inheritance: SearchViewModel Accessor
Since: ArcGIS API for JavaScript 4.0

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

See also:

Constructors

new SearchViewModel(properties)
Parameter:
properties Object
optional

See the properties for a list of all the properties that may be passed into the constructor.

Property Overview

Any properties can be set, retrieved or listened to. See the Working with Properties topic.
NameTypeSummaryClass
FeatureLayer | Locator

The source object currently selected.

more details
more detailsSearchViewModel
Number

The selected source's index.

more details
more detailsSearchViewModel
String

String value used as a hint for input text when searching on multiple sources.

more details
more detailsSearchViewModel
Collection<(FeatureLayerSource|LocatorSource)>

The combined collection of defaultSources and sources.

more details
more detailsSearchViewModel
Boolean

Indicates whether to automatically select and zoom to the first geocoded result.

more details
more detailsSearchViewModel
String

The name of the class.

more details
more detailsAccessor
Collection<(FeatureLayerSource|LocatorSource)>

A read-only property that is a Collection of FeaturelayerSources and/or LocatorSources.

more details
more detailsSearchViewModel
Symbol

The default symbol for the search result.

more details
more detailsSearchViewModel
GoToOverride

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

more details
more detailsSearchViewModel
Boolean | Function

Indicates whether or not to include defaultSources in the Search UI.

more details
more detailsSearchViewModel
Boolean

Indicates whether location services are enabled within the widget.

more details
more detailsSearchViewModel
Number

The maximum character length of the search text.

more details
more detailsSearchViewModel
Number

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

more details
more detailsSearchViewModel
Number

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

more details
more detailsSearchViewModel
Number

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

more details
more detailsSearchViewModel
String

The placeholder used by the activeSource.

more details
more detailsSearchViewModel
Boolean

Indicates whether to display the Popup on feature click.

more details
more detailsSearchViewModel
PopupTemplate

A customized PopupTemplate for the selected feature.

more details
more detailsSearchViewModel
Portal

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

more details
more detailsSearchViewModel
Graphic

The graphic used to highlight the resulting feature or location.

more details
more detailsSearchViewModel
Boolean

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

more details
more detailsSearchViewModel
Object[]

An array of current results from the search.

more details
more detailsSearchViewModel
Boolean

Indicates whether to display the option to search all sources.

more details
more detailsSearchViewModel
String

The value of the search box input text string.

more details
more detailsSearchViewModel
Object

The result selected from a search.

more details
more detailsSearchViewModel
Geometry | SuggestResult

The selected SuggestResult.

more details
more detailsSearchViewModel
Collection<(FeatureLayerSource|LocatorSource)>

The Search widget may be used to search features in a FeatureLayer or geocode locations with a Locator.

more details
more detailsSearchViewModel
String

The current state of the widget.

more details
more detailsSearchViewModel
Number

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

more details
more detailsSearchViewModel
SuggestResult[]

An array of results from the suggest method.

more details
more detailsSearchViewModel
Boolean

Enable suggestions for the widget.

more details
more detailsSearchViewModel
MapView | SceneView

A reference to the MapView or SceneView.

more details
more detailsSearchViewModel

Property Details

activeSourceFeatureLayer|Locatorreadonly

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

activeSourceIndexNumber

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

Default Value:0
allPlaceholderString

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 in the context of the widget.

search-allPlaceholder

Default Value:"Find address or place"
Since: ArcGIS API for JavaScript 4.8

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

autoSelectBoolean

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
declaredClassStringreadonly inherited
Since: ArcGIS API for JavaScript 4.7

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

Since: ArcGIS API for JavaScript 4.8

A read-only property that is a Collection of FeaturelayerSources and/or LocatorSources. This property may contain ArcGIS Portal locators and any web map or web scene configurable search sources.

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

defaultSymbolSymbol
Since: ArcGIS API for JavaScript 4.8

The default symbol for the search result.

Default Value:PictureMarkerSymbol
See also:
goToOverrideGoToOverride
Since: ArcGIS API 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 the Search widget 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);
};
includeDefaultSourcesBoolean|Function
Since: ArcGIS API 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
locationEnabledBoolean
Since: ArcGIS API for JavaScript 4.8

Indicates whether location services are enabled within the widget.

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).

maxInputLengthNumber

The maximum character length of the search text.

Default Value:128
maxResultsNumber

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

Default Value:6
maxSuggestionsNumber

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

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

Default Value:6
minSuggestCharactersNumber

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

Default Value:1
placeholderStringreadonly

The placeholder used by the activeSource.

popupEnabledBoolean

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

Default Value:true
popupTemplatePopupTemplate

A customized PopupTemplate for the selected feature. Note that specifying a wildcard {*} for the popupTemplate will return all fields in addition to search-specific fields.

Default Value:null
portalPortal
Since: ArcGIS API 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.

resultGraphicGraphicreadonly

The graphic used to highlight the resulting feature or location.

Default Value:null
resultGraphicEnabledBoolean

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

Default Value:true
resultsObject[]readonly

An array of current results from the search.

Default Value:null
searchAllEnabledBoolean

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
searchTermString

The value of the search box input text string.

selectedResultObjectreadonly

The result selected from a search.

Default Value:null
selectedSuggestionGeometry|SuggestResultreadonly

The selected SuggestResult.

Default Value:null

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

Any combination of one or more Locator and FeatureLayer sources may be used together in the same instance of the Search widget.

Examples:
// Default sources[] when sources is not specified
[
  {
    locator: new Locator("//geocode.arcgis.com/arcgis/rest/services/World/GeocodeServer"),
    singleLineFieldName: "SingleLine",
    outFields: ["Addr_type"],
    name: "ArcGIS World Geocoding Service",
    localSearchOptions: {
      minScale: 300000,
      distance: 50000
    },
    placeholder: i18n.placeholder,
    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[]
var sources = [
{
  locator: ,
  singleLineFieldName: "SingleLine",
  name: "Custom Geocoding Service",
  localSearchOptions: {
    minScale: 300000,
    distance: 50000
  },
  placeholder: "Search Geocoder",
  maxResults: 3,
  maxSuggestions: 6,
  suggestionsEnabled: false,
  minSuggestCharacters: 0
}, {
  featureLayer: 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
},
{
  featureLayer: 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
}
];
// Set source(s) on creation
var searchWidget = new Search({
  viewModel: { // autocasts as new SearchViewModel()
    sources: []
  }
});

// Set source(s)
var searchWidget = new Search();
var sources = [{ ... }, { ... }, { ... }]; //array of sources
searchWidget.viewModel.sources = sources;

// Add to source(s)
var searchWidget = new Search();
searchWidget.viewModel.sources.push({ ... });  //new source
stateStringreadonly
Since: ArcGIS API for JavaScript 4.8

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

Possible Values: ready | disabled | searching

Default Value:ready
suggestionDelayNumber

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

Default Value:150
suggestionsSuggestResult[]readonly

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.

Default Value:null
suggestionsEnabledBoolean

Enable suggestions for the widget.

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

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

Default Value:null

Method Overview

NameReturn TypeSummaryClass

Clears the current value, search results, suggest results, graphic, and graphics layer.

more details
more detailsSearchViewModel

Emits an event on the instance.

more details
more detailsSearchViewModel
Boolean

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

more details
more detailsSearchViewModel
Object

Registers an event handler on the instance.

more details
more detailsSearchViewModel
Promise<SearchResponse>

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.

more details
more detailsSearchViewModel
Promise<SearchResponse>

Returns search results near your current location.

more details
more detailsSearchViewModel
Promise<SuggestResponse>

Performs a suggest() request on the active Locator.

more details
more detailsSearchViewModel

Method Details

clear()

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

emit(type, event)
Since: ArcGIS API for JavaScript 4.5

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

Parameters:
type String

The name of the event.

event Object

The event payload.

hasEventListener(type){Boolean}

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

Parameter:
type String

The name of the event.

Returns:
TypeDescription
BooleanReturns true if the class supports the input event.
on(type, listener){Object}

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

Parameters:
type String

The name of event to listen for.

listener Function

The function to call when the event is fired.

Returns:
TypeDescription
ObjectReturns an event handler with a remove() method that can be called to stop listening for the event.
PropertyTypeDescription
removeFunctionWhen called, removes the listener from the event.
See also:
Example:
view.on("click", function(event){
  // event is the event handle returned after the event fires.
  console.log(event.mapPoint);
});

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.

Parameter:
optional

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

Returns:
TypeDescription
Promise<SearchResponse>When resolved, returns an object containing an array of search results.
searchNearby(){Promise<SearchResponse>}

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.

Returns:
TypeDescription
Promise<SearchResponse>When resolved, returns SearchResponse containing an array of search objects. Each of these objects contains a SearchResult.
suggest(value){Promise<SuggestResponse>}

Performs a suggest() request on the active Locator. It also uses the current value of the widget 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.

Parameter:
value String
optional

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

Returns:
TypeDescription
Promise<SuggestResponse>When resolved, returns SuggestResponse containing an array of result objects. Each of these results contains a SuggestResult.

Type Definitions

FeatureLayerSource

The following properties define a FeatureLayer-based source whose features may be searched by the Search widget.

Properties:
autoNavigate Boolean
optional
Default Value:true

Indicates whether to automatically navigate to the selected result once selected.

displayField String

The results are displayed using this field. Defaults to the layer's displayField or the first string field.

exactMatch Boolean
optional
Default Value:false

Indicates to only return results that match the search value exactly. This property only applies to string field searches. exactMatch is always true when searching fields of type number.

featureLayer FeatureLayer

The feature layer queried in the search. This is required.

filter Object

As of version 4.4, this property replaces the now deprecated searchQueryParams, suggestQueryParams, and searchExtent properties. Please see the object specification table below for details.

Specification:
where String

The where clause specified for filtering suggests or search results.

geometry Geometry

The filter geometry for suggests or search results.

maxResults Number
optional
Default Value:6

Indicates the maximum number of search results to return.

maxSuggestions Number
optional
Default Value:6

Indicates the maximum number of suggestions to return for the widget's input.

minSuggestCharacters Number
optional
Default Value:1

Indicates the minimum number of characters required before querying for a suggestion.

name String

The name of the source for display.

outFields String[]

Specifies the fields returned with the search results.

placeholder String

Used as a hint for the source input text.

popup Popup

The Popup instance used for the selected result.

popupEnabled Boolean
optional
Default Value:true

Indicates whether to display a Popup when a selected result is clicked.

popupOpenOnSelect Boolean
optional
Default Value:true

Indicates whether to show the Popup when a result is selected.

prefix String

Specify this to prefix the input for the search text.

resultGraphicEnabled Boolean
optional
Default Value:true

Indicates whether to show a graphic on the map for the selected source using the resultSymbol.

resultSymbol Symbol

The symbol used for the resultGraphic.

searchFields String[]

An array of string values representing the names of fields in the feature layer to search.

suffix String

Specify this to suffix the input for the search value.

suggestionsEnabled Boolean

Indicates whether to display suggestions as the user enters input text in the widget. The default value is true.

suggestionTemplate String

A template string used to display multiple fields in a defined order when suggestions are displayed. This takes precedence over displayField. Field names in the template must have the following format: {FieldName}. An example suggestionTemplate could look something like: Name: {OWNER}, Parcel: {PARCEL_ID}.

withinViewEnabled Boolean

Indicates whether to constrain the search results to the view's extent.

zoomScale Number

Applicable to the specified source. If the result does not have an associated extent, specify this number to use as the zoom scale for the result.

LocatorSource

The following properties define a source pointing to a Locator that may be used to geocode locations with the Search widget.

Properties:
autoNavigate Boolean
optional
Default Value:true

Indicates whether to automatically navigate to the selected result once selected.

categories String[]

A string array which limits the results to one or more categories. For example "Populated Place" or "airport". Only applicable when using the World Geocode Service. View the World Geocoding Service documentation for more information.

countryCode String

Constricts search results to a specified country code. For example, US for United States or SE for Sweden. Only applies to the World Geocode Service. View the World Geocoding Service documentation for more information.

filter Object

As of version 4.4, this property replaces the now deprecated searchQueryParams, suggestQueryParams, and searchExtent properties. Please see the object specification table below for details.

Specification:
where String

The where clause specified for filtering suggests or search results.

geometry Geometry

The filter geometry for suggests or search results.

localSearchOptions Object

Sets the sources for local distance and minScale for searching. See the object specification table below for details.

Specification:
distance Number

The distance to search.

minScale Number

The minimum scale used to search locally.

locationToAddressDistance Number
optional
Default Value:1500

When reverse geocoding a result, use this distance in meters.

locator Locator

The locator task used to search. This is required and defaults to the World Geocoding Service.

maxResults Number
optional
Default Value:6

Indicates the maximum number of search results to return.

maxSuggestions Number
optional
Default Value:6

Indicates the maximum number of suggestions to return for the widget's input.

minSuggestCharacters Number
optional
Default Value:1

Indicates the minimum number of characters required before querying for a suggestion.

name String

The name of the source for display.

outFields String[]

Specifies the fields returned with the search results.

placeholder String

Used as a hint for the source input text.

popup Popup

The Popup instance used for the selected result.

popupEnabled Boolean
optional
Default Value:true

Indicates whether to display a Popup when a selected result is clicked.

popupOpenOnSelect Boolean
optional
Default Value:true

Indicates whether to show the Popup when a result is selected.

prefix String

Specify this to prefix the input for the search text.

resultGraphicEnabled Boolean
optional
Default Value:true

Indicates whether to show a graphic on the map for the selected source using the resultSymbol.

resultSymbol Symbol

The symbol used for the resultGraphic.

searchTemplate String

A template string used to display multiple fields in a defined order when results are displayed, e.g. "{Street}, {City}, {ZIP}".

singleLineFieldName String

The field name of the Single Line Address Field in the REST services directory for the locator service. Common values are SingleLine and SingleLineFieldName.

suggestionsEnabled Boolean
optional
Default Value:true

Indicates whether to display suggestions as the user enters input text in the widget.

suffix String

Specify this to suffix the input for the search value.

withinViewEnabled Boolean

Indicates whether to constrain the search results to the view's extent.

zoomScale Number

Applicable to the specified source. If the result does not have an associated extent, specify this number to use as the zoom scale for the result.

SearchResponse

When resolved, returns this response after calling search.

Properties:
activeSourceIndex Number

The index of the source from which the search result was obtained.

errors Error[]

An array of error objects returned from the search results.

numResults Number

The number of search results.

searchTerm String

The searched expression

results Object[]

An array of objects representing the results of search. See object specification table below for more information about the result object.

Specification:

An array of search results.

sourceIndex Number

The index of the currently selected source.

source Object

The source of the selected result.

SearchResult

The result object returned from the search.

Properties:
extent Extent

The extent, or bounding box, of the returned feature.

feature Graphic

The resulting feature or location obtained from the search.

name String

The name of the result.

SuggestResponse

When resolved, returns this reponse after calling suggest.

Properties:
activeSourceIndex Number

The index of the source from which suggestions are obtained. This value is -1 when all sources are selected.

errors Error[]

An array of error objects returned from the suggest results.

numResults Number

The number of suggest results.

searchTerm String

The search expression used for the suggest.

results Object[]

An array of objects representing the results of suggest. See object specification table below for more information about the result object.

Specification:

An array of suggest results.

sourceIndex Number

The index of the currently selected source.

source Object

The source of the selected result.

SuggestResult

The result object returned from a suggest.

Properties:
key String

The key related to the suggest result.

text String

The string name of the suggested location to geocode.

sourceIndex Number

The index of the currently selected result.

Event Overview

NameTypeSummaryClass

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

more details
more detailsSearchViewModel
{activeSourceIndex: Number,errors: Error[],numResults: Number,searchTerm: String,results: Object[],}

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

more details
more detailsSearchViewModel

Fires when the search() method starts.

more details
more detailsSearchViewModel
{result: Object,source: Object,sourceIndex: Number}

Fires when a search result is selected.

more details
more detailsSearchViewModel
{activeSourceIndex: Number,errors: Error[],numResults: Number,searchTerm: String,results: Object[],}

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

more details
more detailsSearchViewModel

Fires when the suggest() method starts.

more details
more detailsSearchViewModel

Event Details

search-clear

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

Example:
var searchWidget = new Search();

searchWidget.viewModel.on("search-clear", function(event){
  console.log("Search input textbox was cleared.");
});
search-complete

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

Properties:
activeSourceIndex Number

The index of the source from which the search result was obtained.

errors Error[]

An array of error objects returned from the search results.

numResults Number

The number of results from the search.

searchTerm String

The searched expression.

results Object[]

An array of objects representing the results of the search. See object specification table below for more information about the result object.

Specification:

An array of objects containing the search results.

sourceIndex Number

The index of the currently selected source.

source Object[]

The source of the selected result.

Example:
var searchWidget = new Search();

searchWidget.viewModel.on("search-complete", function(event){
  // The results are stored in the event Object[]
  console.log("Results of the search: ", event);
});
search-start

Fires when the search() method starts.

Example:
var searchWidget = new Search();

searchWidget.viewModel.on("search-start", function(event){
  console.log("Search started.");
});
select-result

Fires when a search result is selected.

Properties:
result Object

An object containing the results of the search.

Specification:
extent Extent

The extent of the result to zoom to.

feature Graphic

The graphic feature to place at the location of the search result.

name String

The string name of the geocoded location.

source Object

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

sourceIndex Number

The index of the source of the selected result.

Example:
var searchWidget = new Search();

searchWidget.viewModel.on("select-result", function(event){
  console.log("The selected search result: ", event);
});
suggest-complete

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

Properties:
activeSourceIndex Number

The index of the source from which suggestions are obtained. This value is -1 when all sources are selected.

errors Error[]

An array of error objects returned from the suggest results.

numResults Number

The number of suggest results.

searchTerm String

The search expression used for the suggest.

results Object[]

An array of objects representing the results of suggest. See object specification table below for more information about the result object.

Specification:

An array of objects containing the suggest results.

sourceIndex Number

The index of the currently selected source.

source Object

The source of the selected result.

Example:
var searchWidget = new Search();

searchWidget.viewModel.on("suggest-complete", function(event){
  // The results are stored in the event Object[]
  console.log("Results of suggest: ", event);
});
suggest-start

Fires when the suggest() method starts.

Example:
var searchWidget = new Search();

searchWidget.viewModel.on("suggest-start", function(event){
  console.log("suggest-start", event);
});

API Reference search results

NameTypeModule
Loading...