view.hitTest Response array empty when using AGO WebMap & MapServer

1037
3
07-14-2017 10:15 AM
by Anonymous User
Not applicable

Hi all,

I'm currently trying to configure a map that 'highlights selected feature', as discussed in previous topics like this:

jsapi 4.0 popup: set highlight graphic on feature layer click? 

I'm attempting to use a AGO WebMap with a MapServer. I can get the highlight to work when I add in an individual layer from the MapServer, URL similar to below:

"...arcgis/rest/services/xxx/xxx/MapServer/0"

This works just fine. However, if I attempt to add in the entire map server, URL similar to below:

"...arcgis/rest/services/xxx/xxx/MapServer"

The response object returned is empty so the highlight doesn't work.

Is there a way to get this to work without having to add every single layer from the MapService in individually? Some of our larger MapServers have dozens and dozens of layers, many of them grouped together, and I'd rather not have to add each one in individually if I don't have to. 

Here is a paste of the code:

Code Paste Here 

Thanks for the info!

Tags (1)
0 Kudos
3 Replies
by Anonymous User
Not applicable

Can anyone help with this?

0 Kudos
ThomasSolow
Occasional Contributor III

Map servers generally host images.  hitTest is a client-side search, so it won't be able to search images for features.

When you specify MapServer/X, you're actually pulling in the features from a layer hosted on a FeatureServer.  This will be searchable by hitTest.  Try pulling from the FeatureServer instead of the MapServer.  Just changing MapServer to FeatureServer in the URL may work ie: ...arcgis/rest/services/xxx/xxx/FeatureServer.

In most situations if you have a MapServer you will also have a FeatureServer.  It's possible this isn't the case though.

Also note: highlighting was added to the 4.XX API for 3D in 4.4.  I believe it's coming for 2D as well, possibly in 4.5.

Another way to do your own highlighting is to use geometryEngine.convexHull in order to get a new polygon geometry that surrounds the passed in geometry.  This works well for highlighting polygons and polylines.

by Anonymous User
Not applicable

Thank you for your input, this helped solve my issue. One thing that is lost with Feature Servers is the ability to have layers grouped, which is a pretty big deal for me. I want both the ability to have layers grouped in the layer list AND the ability to highlight a selected feature. I was able to get around this by doing the following:

1. Publish both a MapServer and a FeatureServer out to Server

2. Author an ArcGIS.com web map by importing the MapServer (this allows you to have grouped layers in your layer list)

3. Reference that maps 'ID' number in your JS API code under "portal item"

4. For each layer that you want to be highlighted when selected manual add them to your webmap using the FeatureServer REST url in your code using the "FeatureLayer" class from the API.

5. When creating the FeatureLayer in step 4, set the property 'listMode" to 'hide' and 'legendEnabled' to false....this will prevent the layer from showing up in the layer list and legend, as you are only adding it to the map so the .hitTest() function will have something to interact with. You don't actually need to see this on the map/in the layer list or legend as it is a duplicate of the layer that is already present in the MapServer added in step 2.

6. Use the JS code in this topic to set up the function to highlight the graphics layer 

This will give you the best of both worlds if you want to be able to both highlight any features selected AND still use a MapService to allow grouped layers.

0 Kudos