Select to view content in your preferred language

How do I add a popup to every layer in a map service in 4.x?

1282
5
Jump to solution
06-11-2021 04:24 PM
MattStayner
Occasional Contributor II

How can I add a map service so that my users can click on features in the map service? In 3.x I used ArcGISDynamicMapServiceLayer. In 4.x I'm now using MapImageLayer to load the map service. Is that correct? To enable the popup on my FeatureLayer I just added popupTemplate: {}. You can't use that on MapImageLayer. What can I do to enable a popup?

FYI, I'm using a custom panel to show the information, so I don't actually need a popup, I just need await view.popup.fetchFeatures(event) to trigger.

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
EthanBodin1
New Contributor III

It looks like we have to configure popups for each sublayer. In my case I don't know all the sublayers beforehand; this code enables popups for every sublayer when the layer initially loads:

layer.when(() => {
    layer.allSublayers.forEach((sublayer) => {
        sublayer.popupEnabled = true;
    });
});

View solution in original post

5 Replies
EthanBodin1
New Contributor III

It looks like we have to configure popups for each sublayer. In my case I don't know all the sublayers beforehand; this code enables popups for every sublayer when the layer initially loads:

layer.when(() => {
    layer.allSublayers.forEach((sublayer) => {
        sublayer.popupEnabled = true;
    });
});
MattStayner
Occasional Contributor II

@EthanBodin1 thanks for the help!

That wasn't exactly what I needed, but allSublayers got me on the right path. It turns out, in may case any way, popupEnabled was already true, it was just missing the popupTemplate. Here's what I ended up with:

layerToAdd.allSublayers.forEach((sublayer) => {
  sublayer.popupEnabled = true // required to trigger info panel
  sublayer.popupTemplate = { // required to trigger info panel
    outFields: ['*'] // required to view all attributes in popup
  }
})

Thanks again for the help!

0 Kudos
JohnLucotch2
Occasional Contributor

@MattStayner  On your code I'm assuming this is when you were adding a layer through code? I'm trying to get all the popups in the Map Image to turn on without having to format each popup.

 

 

0 Kudos
MattStayner
Occasional Contributor II

@JohnLucotch2 I actually put the code above inside a `.when` so it runs once the layer is added (see below). I hope that helps!

layerToAdd.when(() => {
   layerToAdd.allSublayers.forEach((sublayer) => {
     sublayer.popupEnabled = true; // required to trigger info panel
     sublayer.popupTemplate = { // required to trigger info panel
       outFields: ["*"], // required to view all attributes in popup
      };
   });
});

 

JohnLucotch2
Occasional Contributor

Thanks so much @MattStayner!  

0 Kudos