Select to view content in your preferred language

Listen to map selection without selecting a data source

294
1
10-04-2024 06:49 AM
инструкции
New Contributor

Hi,

I'm starting a new widget and wondering if I can listen to map selection without selecting a data source. I only want to get selected features from specific and pre-defined layers. I was expecting to get to the map view, iterate through the layers, find the ones I want and get the selected features from each one. I did it previously on WAB, and now I'm trying to do the same in ExB.

 

Thank you.

0 Kudos
1 Reply
TimWestern
MVP

If you are doing this with a custom widget, there are some things you can do

To get a map point something like this:

handleMapClick = async (event) => {
    if (this.state.mapSelectionActive) { 
	  // mapSelectionActive is a boolean indicating whether the map is in 'selection' mode or not
      // Query the map point to find various parts
      console.log('Map Click Event:', event)
      const mapPoint: Point = event.mapPoint
      this.selectMapPoint(mapPoint)
    }
  }

 

Once you have the mapPoint, you can then do the query for data in your selectMapPoint function.

Depending on your setup the querying of an individual layer could be trivial, or more complex.

But you could then process that map point to say do an intersect query something like this:

selectMapPoint = async (mapPoint: __esri.Point): Promise<void> => {
  console.log('selectMapPoint: ', mapPoint)

  // let's say its useDataSource index # 4 we want 
  var useDataSourceIndex = 4; 

  try {
    const dataSource = this.props.useDataSources[useDataSourceIndex]
    const dataSourceId = dataSource.dataSourceId
    const dsParcels: FeatureLayerDataSource = DataSourceManager.getInstance().getDataSource(dataSourceId) as FeatureLayerDataSource
    const queryUrl = dsParcels.url

    if (!queryUrl) {
      throw new Error('Query URL is not defined')
    }

    // Note parcelQueryService is an object with an executePointQuery method which takes the
    // queryUrl inferred from useDataSources via DataSourceManager, the mappoint, 
    // and then sets up the actual query and returns the results asynchronously
    // Note that this suggests a mappoint, if you needed to use geometry pulled from a
    // feature, the method might be strucftured differently
    const result = await this.parcelQueryService.executePointQuery(queryUrl, mapPoint)
	
    // in theory from here you can use the result for that layer how every you want.
    // note if you need more than one layer queried, then you have to do the other queries Perhaps in a foreach and collect them into a results field to then be able to use

    this.setState({ queryResults: result }) // Set the query Result into state     
    } catch (error) {
    console.error('Error in selectMapPoint:', error)
    } finally {
    this.setState({ isLoadingParcelSelection: false }) // Stop loading
    }
  })
}



You can find more information about queries here on the site, but this one has some examples of queries:

https://developers.arcgis.com/javascript/latest/tutorials/query-a-feature-layer-sql/

Hope this helps.

0 Kudos