Best practice for selecting features in map from a custom widget

1042
5
Jump to solution
04-10-2022 11:42 PM
MarkJTurnbull
New Contributor III

I am developing an app in ExB that receives URL parameters and based on the parameters passed, it will select and zoom to the features in the map.

I have worked out how to get the map and layer datasources the map is using, but am unable to select features in the layer datasource. I am trying to call the selectRecordsById(string[]) method on the layer datasource but my unit testing shows that the selectRecordsById() method is not doing anything, I am passing it objectids (is this correct?). Am I on the right track? 

Is there a sample somehwere that demonstrates the best practice for selecting features in a map from a custom widget?

1 Solution

Accepted Solutions
RobertScheitlin__GISP
MVP Emeritus

@MarkJTurnbull 

This is how I select features in a DS.

      const dsManager = DataSourceManager.getInstance()
      const featureDS = dsManager.getDataSource(myDSId) as FeatureLayerDataSource
      featureDS.query({objectIds:[ObjectId_Array]}).then(qr=>{
        MessageManager.getInstance().publishMessage(
          new DataRecordsSelectionChangeMessage(this.props.id, qr.records)
        )
        featureDS.selectRecordsByIds?.(qr.records.map((item) => item.getId()))
      })

View solution in original post

5 Replies
RobertScheitlin__GISP
MVP Emeritus

@MarkJTurnbull 

This is how I select features in a DS.

      const dsManager = DataSourceManager.getInstance()
      const featureDS = dsManager.getDataSource(myDSId) as FeatureLayerDataSource
      featureDS.query({objectIds:[ObjectId_Array]}).then(qr=>{
        MessageManager.getInstance().publishMessage(
          new DataRecordsSelectionChangeMessage(this.props.id, qr.records)
        )
        featureDS.selectRecordsByIds?.(qr.records.map((item) => item.getId()))
      })
MarkJTurnbull
New Contributor III

Thanks for getting back to me Robert.

I have tried implementing something very similar to your sample in my code and it is still not working. I have debugged and think it has something to do with the lifecycle of the featureDS, its status is showing as unloaded and it has no records. I am deriving my datasourceID from the JimuMapView.dataSourceId and then getting a MapDataSource from the DataSourceManager, which I am then using mapDataSource.getDataSourceByLayer(layer id) to get a featureLayerDataSource using a nominated layerID. This is the datasource that is appearing unloaded. I have since added a load method with a where clause of 1=1 then run a query against the loaded featureLayerDataSource with the where clause for the selection. It seems to now perform the select (as the URL gets querystring parameters with the layer reference and objectid for the selected faeture) but the map widget is not showing the selected features. Am I on the right track? Why is the featureLayerDataSource in an unloaded state (especially since the layer is visble in the map widget)? Should I be doing something else to get the FeatureLayerDataSource into a loaded state?

0 Kudos
MarkJTurnbull
New Contributor III

Correction, I have found that the map is actually showing the selected feature. It seems as though the message action is failing to zoom on the map. The message seems to be getting through because the pan to action works, it just doesn't want to zoom to it. Is this a bug/known issue?

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

There is no bug that I am aware of.

0 Kudos
LoisLi
by
New Contributor

Hey, I came across this thread when trying to work on a similar functionality, which is to highlight features in  the built-in map widget. Calling selectRecordsByIds() doesn't seem to work in my code and the search lead me this thread. It's called in a useEffect() that watches the change of recordIds. 

However, after I tried this code that includes publishing a message, I still don't see highlighted feature in my map widget. I can get correct data records from the query method though.

I have added the code below to check what records are selected and the result is an empty array. I also added publishMessages: ["DATA_RECORDS_SELECTION_CHANGE"] into manifest JSON, but not sure if I was doing the correct thing. Just wondering did I do anything wrong, e.g. anti-pattern coding? Any help or further questions will help.  

widgetDS.selectRecordsByIds?.(qr.records.map((item) => item.getId()))
console.log('check ds selected records',widgetDS.getSelectedRecords())

 

0 Kudos