Select to view content in your preferred language

How to edit a graphic, or query a GraphicsLayer?

998
2
10-21-2024 01:25 PM
Sparkles
Emerging Contributor

I'm trying to allow the user to edit a graphic (to drag the points on a polyline) by clicking on it - I'm using GraphicsLayer. https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/can-graphics-layers-be-queried/td...On this post, I read that queryFeatures are not available on GraphicsLayer, and in order to query it, you have to convert the GraphicsLayer into a FeatureLayer. 

I tried the solution from  that past, with this code: 
```

           let layer = new FeatureLayer({
                source: tracksLayer.graphics._items
                objectIdField: "ObjectID"
            });

            arcgisView.whenLayerView(layer).then(function (lyrView) {
                console.log(lyrView)
                 lyrView.watch("updating", function (val) {
                     if (!val) {  // wait for the layer view to finish updating
                         lyrView.queryFeatures().then(function (results) {
                          console.log(results);  // prints all the client-side graphics to the console
                         });
                     }
                 });
            })


```



And I get the following error:
```{name: 'view:no-layerview-for-layer', details: {…}, message: 'No layerview has been found for the layer'}```

I'm not sure if using tracksLayer.graphics._items was correct. tracksLayer is my GraphicsLayer.
Also, I'm not sure of the use of   objectIdField: "ObjectID" (I got that from the documentation).


Is there something I'm missing? Or is there another way to allow the user to edit the points on a polyline (in GraphicsLayer?) without being able to query it? This approach of having to change a GraphicsLayer to a FeatureLayer seems too complex, and maybe I should just redo everything and create a FeatureLayer from the start?

Thanks so much for the help!

0 Kudos
2 Replies
JohnGrayson
Esri Alum

Check out the section called "Add an array of client-side features" in the expand section called "Creating a FeatureLayer" in this help doc.  Also, check out the 'dataUpdating' property of the FeatureLayerView.

0 Kudos
Sparkles
Emerging Contributor

Hi @JohnGrayson , thanks for your response! Are you referring to this portion?

let features = [
 {
   geometry: {
     type: "point",
     x: -100,
     y: 38
   },
   attributes: {
     ObjectID: 1,
     DepArpt: "KATL",
     MsgTime: Date.now(),
     FltId: "UAL1"
   }
 },
 ...
];

// geometryType and spatialReference of the layer
// will be inferred from the first feature in the array
// if it has a geometry.
let layer = new FeatureLayer({
  source: features,  // autocast as a Collection of new Graphic()
  objectIdField: "ObjectID"
});



This is what I was looking at as an example for my code. Instead of making a custom features array in the example, I just used  tracksLayer.graphics._items (as that's what had my real data). Would I have to convert my data into that format? Thanks!

0 Kudos