Select to view content in your preferred language

Query Related Records for Visible Layer

396
0
02-27-2023 02:17 PM
Rice_GIS
New Contributor III

Looking to implement this example (https://developers.arcgis.com/javascript/latest/sample-code/query-related-features/) but instead of the query working for just the one layer, I need it to fire depending on which of my three layers are visibile from the LayerList and to show the related records for each respective service layer. The code below shows what I've added to what is basically the same code, just instead of a legend, I use a grouplayer and a layer list with conditional visibility to show only one selected layer at a time. 

 

1. Bringing in my layers and grouping them together.

// Create the layer
        var serverlayer0 = new FeatureLayer({
          url: "https://organizationurl.MapServer/0",
		  outFields: ["*"]
        });
		
		var serverlayer1 = new FeatureLayer({
		url:"https://organizationurl.MapServer/1",
		visible: false,
		outFields: ["*"]
		});
		
		var serverlayer2 = new FeatureLayer({
		url:"https://organizationurl.MapServer/2",
		visible: false,
		outFields: ["*"]
		});
		
		const groupLayer = new GroupLayer({
		title: "Alabama Species Distribution",
		visible: true,
		visibilityMode: "exclusive",
		expanded: true,
		layers: [serverlayer0,serverlayer1,serverlayer2]
		});

2. Constructing the LayerList

const layerlist = new LayerList({ view: view });
        // Expand widget to expand and contract the layer list widget
        const layerlistExpand = new Expand({
          expandTooltip: "Show Layer List",
          expanded: true,
          view: view,
          content: layerlist
        });

3. The part of my code that is relevant to the query function showing where the query functions based on the layer in the first const.

const visibleLayer = serverlayer0;
        
		let highlight, grid;
		

        // call clearMap method when clear is clicked
        const clearbutton = document.getElementById("clearButton");
        clearbutton.addEventListener("click", clearMap);
		

		view.on("click", function (event) {
          clearMap();
          queryFeatures(event);
		  });

        function queryFeatures(screenPoint) {
          const point = view.toMap(screenPoint);
		  
		  

          // Query the for the feature ids where the user clicked
          visibleLayer                      
            .queryObjectIds({
              geometry: point,
              spatialRelationship: "intersects",
              returnGeometry: false,
              outFields: ["*"]
			  
            })

            .then(function (objectIds) {
              if (!objectIds.length) {
                return;
              }

              // Highlight the area returned from the first query
              view.whenLayerView(visibleLayer).then(function (layerView) {
                if (highlight) {
                  highlight.remove();
                }
                highlight = layerView.highlight(objectIds);
              });
			  

              // Query the for the related features for the features ids found
              return visibleLayer.queryRelatedFeatures({
                outFields: ["SpeciesGroup", "CommonName", "ProtectionLevels"],
                relationshipId: visibleLayer.relationships[0].id,
                objectIds: objectIds
			  });
            })

 

The way it's all working right now is, when I run it on my browser, is it exclusively queries for the visibileLayer construct. It will even run the query if all the layers are hidden or I select any of the other layers in my layer list. I'm looking for a way to write that construct so that it is calling on whichever layer is visible in the mapview or active in the layerlist widget. I've tried something along the lines of if(serverlayer0.visible) but I can't get something to stick. Much help would be appreciated.

Tags (3)
0 Kudos
0 Replies