How to force a refresh on a FeatureLayer

10227
14
03-02-2020 04:51 AM
PHerk
by
New Contributor II

We have a WebMap with a FeatureLayer. The FeatureLayer has 2 sublayers. One has areas (polygons) where you can click on, the other layer should be visible after a click on the area. This is accomplished by a filter. If a field in the FeatureLayer.attributes is filled with a value, the polygon is visible in the color defined in ArcGIS online portal. In the Javascript code I return a value in the specific field. That works. The refresh() command does not update the polygon on the screen. The issue is that I have to reload the browser to see the change on the screen. The layer is updated by using the zoom buttons, but I can't find a method to refresh the layer in code, or even better, refresh the polygon. I made a silly hidescreen -> zoom in -> zoom out -> show screen function to have the layer refreshed by controlling the zoom buttons, but I can't imagine there are no other options. Found various similar questions in this community portal, tried all suggestions but there is no success. 

Who has a solution?

Appreciated.

0 Kudos
14 Replies
Egge-Jan_Pollé
MVP Regular Contributor

Hi P Herk‌,

What about the refresh() method?

refresh() - FeatureLayer | ArcGIS API for JavaScript 4.14 

Cheers,

Egge-Jan

0 Kudos
PHerk
by
New Contributor II

refresh() method does not update the graphics in the map....

0 Kudos
Noah-Sager
Esri Regular Contributor

Not sure I understand what a FeatureLayer with sublayers is, but perhaps the load() method is what you want:

FeatureLayer.load() - API Reference

0 Kudos
JohnGrayson
Esri Regular Contributor

Having a simple CodePen (or similar) that demonstrates the workflow normally helps others understand the issue and provide suggestions.

PHerk
by
New Contributor II

OK, you are right. I will ask my customer to make a map public, put up the Codepen and we can go from there. Thank you all for the replies.

0 Kudos
PHerk
by
New Contributor II

OK the CodePen is working.

https://codepen.io/powerm/pen/BaNwNzd?editors=1010 

The featurelayer has 2 layers. One layer has 4 polygons with colors Purple, Olive, Red and Blue. Always visible.

The second layer has the same polygons, in the color green, but initial invisible because of a filter setting.

The filter is set, so the layer polygon is invisible depending of a value in a Dat_uitv field.

No value, no polygon visible.

A valid date value, a green polygon visible.

The goal is that a polygon can be clicked, a timestamp will be set in the Dat_uitv field in the FeatureLayer. This results in a visible second layer by a green color.

This works for 3 polygons, the purple one excluded.

When these 3 polygons are clicked, the Dat_uitv belonging to the graphic is set with the actual date/time stamp.

For this sample, a second click on the polygon, removes the date and clears the Dat_uitv field, so the polygon will have only layer visible, the original color.

You can toggle as much as you like.

So, by clicking on the polygon the date is constructed and returned to ArcGIS online through the ApplyEdits() function.

This works like a charm, except, the screen is not updated, and a call to refresh() does not help.

When the polygon is clicked, and the screen is redrawn, the polygon shows the second layer, because the date field Dat_uitv, has a valid date value.

The only 2 ways to force the graphics to show the correct layer, is a zoom in / zoom out, or pan the polygons uit of the screen, and pan back.

I am looking for a way to update the screen, so I see the correct layers directly after a click on the polygon.

Currently I made a function where I zoom the screen but that is pretty ridiculous, because you need a delay etc. etc.

I would be very pleased if I can get directions how a direct screen update, refresh, redrawn, can be achieved by a function or command.

I do not want to redraw the whole FeatureLayer, because this is just a sample with a few field. The Featurelayer for a client has hundreds of polygons...

0 Kudos
JohnGrayson
Esri Regular Contributor

Is your intent to work with the layers in the Web Map?  If so, you should find the specific FeatureLayer you want to update that has been added to the map (via the Web Map) instead of creating a new instance.

0 Kudos
PHerk
by
New Contributor II

Hi John, that is correct, but using this: webMap.findLayerById('b7408a1c740d401288521f3834088778') does not work.

b7408a1c740d401288521f3834088778 is the id of the Featurelayer in ArcGIS online.

0 Kudos
JohnGrayson
Esri Regular Contributor

To find a layer that is currently in the view, you can find it via the 'layers' collection of the map:

view.when(()=>{
 const myFeatureLayer = view.map.layers.find(layer=>{ 
   return (layer.title === "Vlakken - gereed");
 }); 
 console.info(myFeatureLayer.title, myFeatureLayer.id);
);‍‍‍‍‍‍‍‍‍‍‍‍

The id of the layer in the Web Map is different than the id of the portal item.

0 Kudos