ArcGIS Maps SDK – How to Temporarily Disable a Feature Layer’s Popup and Restore It Later

arcgis-maps-sdk-javascriptpopup

I have an ArcGIS Server JavaScript API v4 map containing some feature layers which each have a popupTemplate defined, so clicking on the layer(s) automatically shows the layers' popup.

I need to disable the popups temporarily, so that clicking on the map does something else, but does not show the popup. Then, after my operation is over, I want to restore all of the feature layer popups.

Is there an easy way to accomplish this? Some of the default widgets exhibit this behaviour, as in this example with the Sketch Widget:

https://codepen.io/slead/pen/gOvgwLp

Note that while the widget is idle, clicking on the map shows the layer popups, but when the widget is active the popups do not display. When the widget is no longer active, the popups are restored.

How can I achieve the same outcome? See the rough mockup in the code:

  • the first time the user clicks on the map, show the console message and don't display the popup
  • when the listener is disabled, revert to showing the layer popups
clickListener = view.on("click", (event) => {
  console.log("disable layer popup while listener is active, allow while inactive")
  clickListener.remove()
})

Best Answer

The answer I came up with is to watch the view's popup, and close it where necessary:

view.popup.watch("visible", (visible) => {
  if (visible){
    view.popup.visible = condition_goes_here;
  }
});

Using this approach the popup is not shown when clicking on the feature layer when the condition is met. Otherwise, the popup shows as usual.

Related Question