Select to view content in your preferred language

Feature Layer: Update if exists, add if does not, remove if does not exist anymore

361
0
05-31-2022 05:18 AM
bladesv2
New Contributor

Hi,

I am building a React based web app which tracks some moving objects. For simplicity's sake, let's assume I track cars.

Cars may change position or various states over time, and they may be removed and no longer tracked.
I am managing the tracked objects with a Redux state, simply as an array which contains the Cars data.

I'm having trouble implementing the following behaviour:

1. If a car has been added, add him to the map.

2. If a car has been removed, remove him

3. If a car's location has changed, change it on the map as well.

 

I've tried using the useEffect() hook with combintation with FeaturesLayer.applyEdits(), but as seen in the image below, I am not sure how to efficiently perform the add and delete operations.

 

 if (props.map.findLayerById(props.id) !== undefined) {
            console.log("FeaturesLayer:layer with id : ", props.id, "already exists")
            const featLayer = props.map.findLayerById(props.id) as FeatureLayer
            // featLayer.source = new Collection(props.source)
            featLayer.applyEdits({
                updateFeatures: updated_source,
                addFeatures: /* How to detect which features do not exist already in the layer
                                 but exist in the new source? */ [],
                deleteFeatures: /* How to detect which features do not exist in the new source anymore? */ [],

            })

 

Thank you

 

0 Kudos
0 Replies