require(["esri/widgets/Search/SearchViewModel"], (SearchVM) => { /* code goes here */ });
import SearchVM from "@arcgis/core/widgets/Search/SearchViewModel.js";
esri/widgets/Search/SearchViewModel
Provides the logic for the Search widget, 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
-
Parameterproperties Objectoptional
See the properties for a list of all the properties that may be passed into the constructor.
Property Overview
Name | Type | Summary | Class |
---|---|---|---|
The source object currently selected. | SearchViewModel | ||
The selected source's index. | SearchViewModel | ||
String value used as a hint for input text when searching on multiple sources. | SearchViewModel | ||
The combined collection of defaultSources and sources. | SearchViewModel | ||
Indicates whether to automatically select and zoom to the first geocoded result. | SearchViewModel | ||
The name of the class. | Accessor | ||
A read-only property that is a Collection of LayerSearchSource and/or LocatorSearchSource. | SearchViewModel | ||
The default symbol(s) for the search result. | SearchViewModel | ||
This function provides the ability to override either the MapView goTo() or SceneView goTo() methods. | SearchViewModel | ||
Indicates whether or not to include defaultSources in the Search UI. | SearchViewModel | ||
Indicates whether location services are enabled within the widget. | SearchViewModel | ||
The maximum character length of the search text. | SearchViewModel | ||
The maximum number of results returned by the widget if not specified by the source. | SearchViewModel | ||
The maximum number of suggestions returned by the widget if not specified by the source. | SearchViewModel | ||
The minimum number of characters needed for the search if not specified by the source. | SearchViewModel | ||
The placeholder used by the activeSource. | SearchViewModel | ||
Indicates whether to display the Popup on feature click. | SearchViewModel | ||
A customized PopupTemplate for the selected feature. | SearchViewModel | ||
It is possible to search a specified portal instance's locator services Use this property to set this ArcGIS Portal instance to search. | SearchViewModel | ||
The graphic used to highlight the resulting feature or location. | SearchViewModel | ||
Indicates if the resultGraphic will display at the location of the selected feature. | SearchViewModel | ||
An array of current results from the search. | SearchViewModel | ||
Indicates whether to display the option to search all sources. | SearchViewModel | ||
The value of the search box input text string. | SearchViewModel | ||
The result selected from a search. | SearchViewModel | ||
The selected SuggestResult. | SearchViewModel | ||
The Search widget may be used to search features in a FeatureLayer or table, or geocode locations with a locator. | SearchViewModel | ||
The current state of the widget. | SearchViewModel | ||
The millisecond delay after keyup and before making a suggest network request. | SearchViewModel | ||
An array of results from the suggest method. | SearchViewModel | ||
Enable suggestions for the widget. | SearchViewModel | ||
Indicates whether the | SearchViewModel | ||
SearchViewModel |
Property Details
-
activeSource
activeSource LayerSearchSource |LocatorSearchSourcereadonly
-
The source object currently selected. Can be either a feature layer or a locator.
-
activeSourceIndex
activeSourceIndex Number
-
The selected source's index. This value is
-1
when all sources are selected.- Default Value:0
-
allPlaceholder
allPlaceholder String
-
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.
- Default Value:"Find address or place"
-
allSources
allSources Collection<(LayerSearchSource|LocatorSearchSource)>readonly
Since: ArcGIS Maps SDK for JavaScript 4.8SearchViewModel since 4.0, allSources added at 4.8. -
The combined collection of defaultSources and sources. The defaultSources displays first in the Search UI.
-
autoSelect
autoSelect 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
-
defaultSources
defaultSources Collection<(LayerSearchSource|LocatorSearchSource)>readonly
Since: ArcGIS Maps SDK for JavaScript 4.8SearchViewModel since 4.0, defaultSources added at 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
defaultSymbols Object
Since: ArcGIS Maps SDK for JavaScript 4.22SearchViewModel since 4.0, defaultSymbols added at 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.
- Properties
-
optional A SimpleMarkerSymbol, PointSymbol3D, TextSymbol, CIMSymbol, or WebStyleSymbol used for representing the point geometry that is being drawn for the search result. SimpleMarkerSymbol may also be used to symbolize point features in a SceneView. However, it is recommended you use PointSymbol3D instead.
polygon SimpleFillSymbol|PolygonSymbol3D|CIMSymbolA SimpleFillSymbol, PolygonSymbol3D, or CIMSymbol used for representing the polygon geometry that is being drawn for the search result. SimpleFillSymbol may also be used to symbolize polygon features in a SceneView. However, it is recommended you use PolygonSymbol3D instead.
polyline SimpleLineSymbol|LineSymbol3D|CIMSymbolA SimpleLineSymbol, LineSymbol3D, or CIMSymbol used for representing the polyline geometry that is being drawn for the search result. SimpleLineSymbol may also be used to symbolize polyline features in a SceneView. However, it is recommended you use LineSymbol3D instead.
Exampleconst searchWidget = new Search(); searchWidget.viewModel.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
goToOverride GoToOverride
Since: ArcGIS Maps SDK for JavaScript 4.8SearchViewModel since 4.0, goToOverride added at 4.8. -
This function provides the ability to override either the MapView goTo() or SceneView goTo() methods.
- See also
-
- esri/views/MapView#goTo
- SceneView
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); };
-
Since: ArcGIS Maps SDK for JavaScript 4.8SearchViewModel since 4.0, includeDefaultSources added at 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 let searchWidget = new Search({ view: view }); searchWidget.viewModel.includeDefaultSources = false; // includeDefaultSources passed as a function let searchWidget = new Search({ view: view }); searchWidget.viewModel.includeDefaultSources = function(sourcesResponse) { return sourcesResponse.defaultSources; };
-
locationEnabled
locationEnabled Boolean
Since: ArcGIS Maps SDK for JavaScript 4.8SearchViewModel since 4.0, locationEnabled added at 4.8. -
Indicates whether location services are enabled within the widget.
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
maxInputLength Number
-
The maximum character length of the search text.
- Default Value:128
-
maxResults
maxResults Number
-
The maximum number of results returned by the widget if not specified by the source.
- Default Value:6
-
maxSuggestions
maxSuggestions Number
-
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
-
minSuggestCharacters
minSuggestCharacters Number
-
The minimum number of characters needed for the search if not specified by the source.
- Default Value:1
-
placeholder
placeholder Stringreadonly
-
The placeholder used by the activeSource.
-
popupTemplate
popupTemplate PopupTemplate
-
A customized PopupTemplate for the selected feature. Note that any templates defined on allSources take precedence over those defined directly on the template.
- Default Value:null
-
portal
portal Portal
Since: ArcGIS Maps SDK for JavaScript 4.8SearchViewModel since 4.0, portal added at 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.
-
resultGraphic
resultGraphic Graphicreadonly
-
The graphic used to highlight the resulting feature or location.
- Default Value:null
-
resultGraphicEnabled
resultGraphicEnabled Boolean
-
Indicates if the resultGraphic will display at the location of the selected feature.
A graphic will be placed in the View's graphics for layer views that do not support the
highlight
method.- Default Value:true
-
An array of current results from the search.
- Default Value:null
-
searchAllEnabled
searchAllEnabled Boolean
-
Indicates whether to display the option to search all sources. When
true
, the "All" option is displayed by default:When
false
, no option to search all sources at once is available:- Default Value:true
-
searchTerm
searchTerm String
-
The value of the search box input text string.
-
selectedResult
selectedResult Objectreadonly
-
The result selected from a search.
- Default Value:null
-
selectedSuggestion
selectedSuggestion Geometry |SuggestResultreadonly
-
The selected SuggestResult.
- Default Value:null
-
sources
sources Collection<(LayerSearchSource|LocatorSearchSource)>
-
The Search widget 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 widget instance. There are two types of sources:Any combination of these sources may be used together in the same instance of the Search widget.
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 } ];
// Set source(s) on creation let searchWidget = new Search({ viewModel: { // autocasts as new SearchViewModel() sources: [] } }); // Set source(s) let searchWidget = new Search(); let sources = [{ ... }, { ... }, { ... }]; //array of sources searchWidget.viewModel.sources = sources; // Add to source(s) let searchWidget = new Search(); searchWidget.viewModel.sources.push({ ... }); //new source
-
state
state Stringreadonly
Since: ArcGIS Maps SDK for JavaScript 4.8SearchViewModel since 4.0, state added at 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" |"loading"
- Default Value:"ready"
-
suggestions
suggestions SuggestResult[]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
-
suggestionsEnabled
suggestionsEnabled Boolean
-
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
-
updating
updating Booleanreadonly
Since: ArcGIS Maps SDK for JavaScript 4.16SearchViewModel since 4.0, updating added at 4.16. -
Indicates whether the
View
orPortal
is loading resources prior to use. If resources are still loading, this value isfalse
. If resources are done loading, this value istrue
.- Default Value:false
-
- Default Value:null
Method Overview
Name | Return Type | Summary | Class |
---|---|---|---|
Adds one or more handles which are to be tied to the lifecycle of the object. | Accessor | ||
Clears the current value, search results, suggest results, graphic, and graphics layer. | SearchViewModel | ||
Emits an event on the instance. | SearchViewModel | ||
Indicates whether there is an event listener on the instance that matches the provided event name. | SearchViewModel | ||
Returns true if a named group of handles exist. | Accessor | ||
Registers an event handler on the instance. | SearchViewModel | ||
Removes a group of handles owned by the object. | Accessor | ||
Promise<SearchResponse> | Depending on the sources specified, | SearchViewModel | |
Promise<SearchResponse> | Returns search results near your current location. | SearchViewModel | |
Promise<SuggestResponse> | Performs a suggest() request on the active Locator. | SearchViewModel | |
Promise |
| when |
Method Details
-
Inherited from Accessor
Since: ArcGIS Maps SDK for JavaScript 4.25Accessor since 4.0, addHandles added at 4.25. -
Adds one or more handles which are to be tied to the lifecycle of the object. The handles will be removed when the object is destroyed.
// Manually manage handles const handle = reactiveUtils.when( () => !view.updating, () => { wkidSelect.disabled = false; }, { once: true } ); this.addHandles(handle); // Destroy the object this.destroy();
ParametershandleOrHandles WatchHandle|WatchHandle[]Handles marked for removal once the object is destroyed.
groupKey *optionalKey identifying the group to which the handles should be added. All the handles in the group can later be removed with Accessor.removeHandles(). If no key is provided the handles are added to a default group.
-
Clears the current value, search results, suggest results, graphic, and graphics layer. It also hides any open menus.
-
hasHandles
InheritedMethodhasHandles(groupKey){Boolean}
Inherited from AccessorSince: ArcGIS Maps SDK for JavaScript 4.25Accessor since 4.0, hasHandles added at 4.25. -
Returns true if a named group of handles exist.
ParametergroupKey *optionalA group key.
ReturnsType Description Boolean Returns true
if a named group of handles exist.Example// Remove a named group of handles if they exist. if (obj.hasHandles("watch-view-updates")) { obj.removeHandles("watch-view-updates"); }
-
on
on(type, listener){Object}
-
Registers an event handler on the instance. Call this method to hook an event with a listener.
ParametersReturnsType Description Object Returns an event handler with a remove()
method that should be called to stop listening for the event(s).Property Type Description remove Function When called, removes the listener from the event. Exampleview.on("click", function(event){ // event is the event handle returned after the event fires. console.log(event.mapPoint); });
-
Inherited from Accessor
Since: ArcGIS Maps SDK for JavaScript 4.25Accessor since 4.0, removeHandles added at 4.25. -
Removes a group of handles owned by the object.
ParametergroupKey *optionalA group key or an array or collection of group keys to remove.
Exampleobj.removeHandles(); // removes handles from default group obj.removeHandles("handle-group"); obj.removeHandles("other-handle-group");
-
search
search(searchTerm){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.Parameteroptional 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).
ReturnsType Description Promise<SearchResponse> When resolved, returns an object containing an array of search results.
-
searchNearby
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.ReturnsType Description Promise<SearchResponse> When resolved, returns SearchResponse containing an array of search objects. Each of these objects contains a SearchResult.
-
suggest
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
.Parametervalue StringoptionalThe string value used to suggest() on an active Locator or feature layer. If nothing is passed in, takes the current value of the widget.
ReturnsType Description Promise<SuggestResponse> When resolved, returns SuggestResponse containing an array of result objects. Each of these results contains a SuggestResult.
-
Since: ArcGIS Maps SDK for JavaScript 4.16when since 4.0, when added at 4.16. -
when()
may be leveraged once theSearchViewModel
has been updated. This method takes two input parameters: acallback
function and anerrback
function. Thecallback
executes when the instance of the class loads. Theerrback
executes if the instance of the class fails to load.ParametersReturnsType Description Promise Returns a new Promise for the result of callback
.Example// Although this example uses MapView, any class instance that is a promise may use when() in the same way let view = new MapView(); view.when(function(){ // This function will execute once the promise is resolved }, function(error){ // This function will execute if the promise is rejected due to an error });
Type Definitions
-
When resolved, returns this response after calling search.
- Properties
-
activeSourceIndex Number
The index of the source from which the search result was obtained.
An array of error objects returned from the search results.
numResults NumberThe number of search results.
searchTerm StringThe searched expression
An array of objects representing the results of search. See object specification table below for more information about the result object.
- Specification
-
results SearchResult[]
An array of search results.
sourceIndex NumberThe index of the currently selected source.
source ObjectThe source of the selected result.
-
The result object returned from the search.
-
When resolved, returns this response 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.An array of error objects returned from the suggest results.
numResults NumberThe number of suggest results.
searchTerm StringThe search expression used for the suggest.
An array of objects representing the results of suggest. See object specification table below for more information about the result object.
- Specification
-
results SuggestResult[]
An array of suggest results.
sourceIndex NumberThe index of the currently selected source.
source ObjectThe source of the selected result.
-
The result object returned from a suggest.
Event Overview
Name | Type | Summary | Class |
---|---|---|---|
|
Fires when a result is cleared from the input box or a new result is selected. |
SearchViewModel | |
|
{activeSourceIndex: Number,errors: Error[],numResults: Number,searchTerm: String,results: Object[],results.results: SearchResult[],results.sourceIndex: Number,results.source: Object[]} |
Fires when the search() method is called and returns its results. |
SearchViewModel |
|
Fires when the search() method starts. |
SearchViewModel | |
|
{result: Object,result.extent: Extent,result.feature: Graphic,result.name: String,source: Object,sourceIndex: Number} |
Fires when a search result is selected. |
SearchViewModel |
|
{activeSourceIndex: Number,errors: Error[],numResults: Number,searchTerm: String,results: Object[],results.results: SuggestResult[],results.sourceIndex: Number,results.source: Object} |
Fires when the suggest method is called and returns its results. |
SearchViewModel |
|
Fires when the suggest() method starts. |
SearchViewModel |
Event Details
-
Fires when a result is cleared from the input box or a new result is selected.
Examplelet searchWidget = new Search(); searchWidget.viewModel.on("search-clear", function(event){ console.log("Search input textbox was cleared."); });
-
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.
An array of error objects returned from the search results.
numResults NumberThe number of results from the search.
searchTerm StringThe searched expression.
An array of objects representing the results of the search. See object specification table below for more information about the result object.
Examplelet 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); });
-
Fires when the search() method starts.
Examplelet searchWidget = new Search(); searchWidget.viewModel.on("search-start", function(event){ console.log("Search started."); });
-
Fires when a search result is selected.
Examplelet searchWidget = new Search(); searchWidget.viewModel.on("select-result", function(event){ console.log("The selected search result: ", event); });
-
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.An array of error objects returned from the suggest results.
numResults NumberThe number of suggest results.
searchTerm StringThe search expression used for the suggest.
An array of objects representing the results of suggest. See object specification table below for more information about the result object.
- Specification
-
results SuggestResult[]
An array of objects containing the suggest results.
sourceIndex NumberThe index of the currently selected source.
source ObjectThe source of the selected result.
Examplelet searchWidget = new Search(); searchWidget.viewModel.on("suggest-complete", function(event){ // The results are stored in the event Object[] console.log("Results of suggest: ", event); });
-
Fires when the suggest() method starts.
Examplelet searchWidget = new Search(); searchWidget.viewModel.on("suggest-start", function(event){ console.log("suggest-start", event); });