I can't remove the graphic added to the map

642
9
06-27-2024 07:34 AM
emreaktas1
Regular Contributor

hello esri community.
I have a small problem can you help me?

 

js code

 

   items = document.querySelectorAll(".item");

  // console.log(items)
 
  items.forEach(item => {

     
      item.addEventListener("click", () => {
          item.classList.toggle("checked");

         // if(item.className == "item checked"){
            //alert("ekle")

         let valitem;
         valitem =   item.children[2].innerHTML;

         console.log(" ada parsel " , valitem);
   
        let query = layer.createQuery();
              query.where = `adaparsel = '${valitem}'`;
              query.outFields = ["*"];
              query.returnGeometry = true
               
              layer.queryFeatures(query).then((result) => {
                console.log(result);
                //let features = result.features;

                result.features.map((feature) => {
                  let staneName= feature.attributes["adaparsel"];
                  document.getElementById("grafiksonuclar").innerHTML= staneName + "Parsel No"

                  resultgraphics = new Graphic({
                    geometry: feature.geometry,
                  })
                  view.graphics.add(resultgraphics);
                view.goTo(resultgraphics);

                if(item.classList == "item"){
                  document.getElementById("grafiksonuclar").innerHTML= ""
                  view.graphics.remove(resultgraphics);        
                 }                              
            });
       });

 

0 Kudos
9 Replies
MatthewDriscoll
MVP Alum

Try, resultGraphic.graphic.removeAll(); or resultGraphic.removeAll()

0 Kudos
emreaktas1
Regular Contributor

hi @MatthewDriscoll 

I didn't get results.
appeared on screen did not remove black graphics

 

emreaktas1_0-1719554992986.png

 

 

I didn't get results.
It did not remove the black graphics appearing on the screen.
I cannot add or remove graphics with the checkbox

Tags (2)
0 Kudos
mleahy_cl
Regular Contributor

I believe part of the problem may be that every time you execute layer.queryFeatures(query),  the features you get in the results are new objects (even if they refer to the same underlying fearures).  So when you later try to remove them, you are essentially removing new feature objects that were never actually added to the view (I haven't tested this recently to confirm, but I'm pretty sure that's how it works).

 

I see two ways to deal with this:

1. Keep a list of the feature objects added to the view's graphics layer, and when you want to remove a graphic based on your query results, find the corresponding graphic that was added (e.g., using objectid as a lookup) and remove that from the views graphics layer instead.

2. Instead of adding features as graphics, create a client-side feature layer that matches the schema of the layer you are querying, and use appyEdits to add/remove features in it.  Then you can use objectids from your query results when you need to delete features from the layer.

I might be off the mark here, but hopefully one of these alternatives works for you. 

0 Kudos
emreaktas1
Regular Contributor

hi @mleahy_cl 

https://developers.arcgis.com/javascript/latest/sample-code/sandbox/?sample=widgets-featuretable-pop...

 

What I actually want to do is to show the data selected with selecet on the map, as in the example here.
By adding graphics this makes my application difficult to run.
How can I show the selected layer on the map in a simpler way?

0 Kudos
mleahy_cl
Regular Contributor

Ok, it sounds to me like what you want is to highlight features.  You can highlight features on a LayerView (see the map view's `whenLayerView()` method) with a list of one or more object IDs.  That returns a handle object for that specific highlight.  When you want to remove that highlight, you use its `remove()` method.

Here's a sample: https://developers.arcgis.com/javascript/latest/sample-code/highlight-point-features/

The highlight method specifically (on the FeatureLayerView class): https://developers.arcgis.com/javascript/latest/api-reference/esri-views-layers-FeatureLayerView.htm...

0 Kudos
emreaktas1
Regular Contributor

hi @mleahy_cl  

Yes, what I wanted was to highlight and I did this with the whenLayerView method.
What is the method to add a line between 2 selected data as in the screenshot.
I want to add a line to the selected data as in the screenshot

emreaktas1_0-1720168507756.png

 

emreaktas1_1-1720168578746.png

 

 

 

0 Kudos
mleahy_cl
Regular Contributor

That's a somewhat different question.  I think you'd be looking at creating your own geometry (e.g., a line from the centre of the highlighted item to some related location?), and either add a graphic using that geometry to the map's graphics layer, or to a client-side feature layer.  You could get the arrow effect with a CIM symbol

0 Kudos
emreaktas1
Regular Contributor

hi @mleahy_cl 

How can I make a line (eg Cım Symbol) from the center of the highlighted element to the center of the other highlighted element?
Is there any sample code on the subject?

0 Kudos
mleahy_cl
Regular Contributor

I don't know of any examples offhand.  You can query the geometry for the features you're working with, and use those to derive start and end points (e.g., you can get a centroid from polygons).  Use those two points to construct a polyline geometry, then use that geometry to create your graphic to display on the map.

0 Kudos