MapImageLayer search widget

1260
9
02-09-2022 06:56 AM
KristianTuusjärvi
New Contributor II

Hello, is it possible to search from all of the MapImageLayer sublayers?

Should I use a collection of LayerSearchSource to iterate through all of the sublayers and then add the collection to Search widget's 'sources' property.

const sources = new Collection<LayerSearchSource>();

//Loop through MapImageLayer and add to sources??

const search = new Search({
view: view,
  sources: sources,
});

 

 

0 Kudos
9 Replies
Noah-Sager
Esri Regular Contributor

Hi @KristianTuusjärvi, yes, you can use a Sublayer just like a FeatureLayer as a LayerSearchSource.

https://developers.arcgis.com/javascript/latest/api-reference/esri-widgets-Search-LayerSearchSource....

I would recommend pulling out all the sublayers first, then add them as a LayerSearchSource individually so you can configure the settings like in this sample:

https://developers.arcgis.com/javascript/latest/sample-code/widgets-search-multiplesource/

KenBuja
MVP Esteemed Contributor

That sample is unreachable, but are there any updates to the existing sample?

https://developers.arcgis.com/javascript/latest/sample-code/sandbox/?sample=widgets-search-multiples...

0 Kudos
Noah-Sager
Esri Regular Contributor

Thanks @KenBuja. I updated the link. No updates to report.

0 Kudos
nadja_swiss_parks
Occasional Contributor II

Hi @Noah-Sager 

I'm not sure if the sample code has been modified, but there is no sample of a sublayer of a MapImageLayer in above sample code. I'm wondering how do you "pull out all the sublayers first"? Thank you for your answer.

@KristianTuusjärvi Have you managed to include the entire MapImageLayer as a source to Search? 

Best,
Nadja

0 Kudos
Noah-Sager
Esri Regular Contributor

Correct @nadja_swiss_parks, the sample I linked to shows how to set-up individual feature layers for use with the LayerSearchSource, like for example the searchFields and the outFields. It wouldn't work with just adding all Sublayers from a MapImageLayer without knowing more about the Sublayers. Does that make sense? So I don't think a programmatic approach would simplify things. I would just add the the Sublayers as individual LayerSearchSources.

0 Kudos
nadja_swiss_parks
Occasional Contributor II

Yes, thanks @Noah-Sager, this makes sense. However, I wonder how I would add the Sublayers as individual LayerSearchSource? Let's take the first Sublayer as an example. I tried several approaches to access the first Sublayer:

const MyLayerSearchSource= new LayerSearchSource({
  layer: MyMapImageLayer.findSublayerById(0)
});
 
const searchWidget = new Search({
    view: view,
    sources: [{
        layer: search_perimeter,
        searchFields: ["*"], 
        outFields: ["*"]
      }]
    });
This code-snippet throws the error "Uncaught (in promise) TypeError: a.load is not a function". How do I access the sublayer correctly?
0 Kudos
Noah-Sager
Esri Regular Contributor

Yeah, unfortunately you can't load a Sublayer directly, it needs to be a FeatureLayer or one of the other approved layer types in the doc: https://developers.arcgis.com/javascript/latest/api-reference/esri-widgets-Search.html#sources

What you can do is flatten the collection of Sublayers in the MapServer, then create FeatureLayers (and get the relevant LayerSearchSource attributes) out of each Sublayer.

https://developers.arcgis.com/javascript/latest/api-reference/esri-layers-MapImageLayer.html#allSubl...

You should be able to also use the Sublayer.createFeatureLayer() method, but I had trouble with the synchronicity of the requests.

https://developers.arcgis.com/javascript/latest/api-reference/esri-layers-support-Sublayer.html#crea...

After creating the FeatureLayer and the LayerSearchSource attributes, add them to an object, and then add that object to the Search widget sources Collection.

Here is a proof-of-concept app that should help clarify a potential workflow:

https://codepen.io/noash/pen/VwxgQzv?editors=1000

Try searching for a simple word here, like "new" or "yo".

KristianTuusjärvi
New Contributor II

I wonder if I can just add the whole MapImageLayer as a source to Search? It seems to be possible, but I don't understand how to choose the fields when there is multiple sublayers on MapImageLayer. 
Here it is mentioned that MapImageLayer can be used. Any ideas?
https://developers.arcgis.com/javascript/latest/api-reference/esri-widgets-Search-LayerSearchSource....

0 Kudos
Noah-Sager
Esri Regular Contributor

Sorry @KristianTuusjärvi, I just noticed this question. Unfortunately, MapImageLayer is not listed there. Perhaps we could improve the wording to make this more clear. What it means is that a FeatureLayer from a MapServer or a FeatureServer is usable. So if you had a MapServer like this:

https://sampleserver6.arcgisonline.com/arcgis/rest/services/Census/MapServer

You could also just use each Sublayer URL (effectively consuming it as a FeatureLayer) as a source for the Search widget:

https://sampleserver6.arcgisonline.com/arcgis/rest/services/Census/MapServer/0

https://sampleserver6.arcgisonline.com/arcgis/rest/services/Census/MapServer/1

https://sampleserver6.arcgisonline.com/arcgis/rest/services/Census/MapServer/2

https://sampleserver6.arcgisonline.com/arcgis/rest/services/Census/MapServer/3

 

0 Kudos