Esri / arcgis-experience-builder-sdk-resources

ArcGIS Experience Builder samples
https://developers.arcgis.com/experience-builder/
Apache License 2.0
119 stars 111 forks source link

Selecting features from datasource #185

Open Party-Pelican opened 1 week ago

Party-Pelican commented 1 week ago

Could we get additional documentation or a sample of how to select features from a datasource based on a click on a map or the intersection of a drawn graphic?

I have been scratching my head over how to accomplish this. The SDK provides a selectFeaturesByGraphic method in the JimuMapView class but it selects everything regardless of whether or not the layer actually has a popup enabled or not. If I would like to select from a specific list of datasources (i.e. only those that are visible and have a popup enabled). I find myself having to loop every layer in the map, executing a query, and then looping the results and using the selectRecordsByIds on the datasource, but even this doesn't always give me the proper behavior, often times features are not highlighted in the map, even though results are returned. I attached a snippet of that code below.

Therefore, can we get additional documentation on how to select features from a datasource based on a graphic or mouse click?

async function handleClick(evt: __esri.ViewClickEvent) {
    const clickedPoint = new Graphic({ geometry: evt.mapPoint });

    const queries = queriableDataSources
      .filter((ds) =>
        jimuMapView.getJimuLayerViewByDataSourceId(ds.id).isLayerVisible()
      )
      .map((ds) => {
        return ds.query({
          geometry: clickedPoint.geometry.toJSON(),
          spatialRel: "esriSpatialRelEnvelopeIntersects",
          returnGeometry: true,
          outFields: ["*"],
          distance: 0,
          units: "esriSRUnit_Meter",
        } as FeatureLayerQueryParams);
      });

    const results = (await Promise.all(queries)).filter(
      (q) => q.records.length
    );

    console.log("results", results);

    results.forEach((result) => {
      result.records[0].dataSource.selectRecordsByIds(
        result.records.map((record) => record.getId()),
        result.records
      );
    });

  }
qlqllu commented 1 week ago

@Party-Pelican The main reason why you can't use selectFeaturesByGraphic is it selects layers even the popup is not enabled, right?

Party-Pelican commented 1 week ago

@qlqllu yes, that is correct. Is there another method that I should be using instead? what do you recommend? I appreciate any info.

qlqllu commented 1 week ago

This method is the best choice for your requirements, but it seems to miss some features. If we add an option, such as a callback function, you can check whether you need to select by checking the layer property in the function. Is this a good solution for your case?

Party-Pelican commented 1 week ago

That would work, so in the callback function, would I need to clear the selection of layers that don't have the popup enabled or do the selection in the callback? I'm not sure of the specifics, but if it's possible to allow for more options in this method, that would be great!