3

I need to remove a geometry from the map. It may be a marker, circle, polyline etc. I don't see any method like remove, clear or destroy. I already know how to clear everything on the map but this is not what I need.

I would like to remove it with javascript.

PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
asked Aug 10, 2014 at 16:32
7
  • In ArcMap,simply start editing (from editor toolbar) and pressing delete key,deletes any selected feature from different layers.But seems this is not exactly what you want.May you provide more information how you want geometries get removed from map? Commented Aug 10, 2014 at 16:38
  • Thanks for your fast response. I have a button and on click I want to delete the geometry with javascript. Commented Aug 10, 2014 at 16:41
  • And how you select geometry which must get selected?is it included in map selection list? Commented Aug 10, 2014 at 16:51
  • I use esri/toolbars/edit and when user clicks on a shape then the toolbar is activated. A global variable is used to "store" the selected geometry. Commented Aug 10, 2014 at 16:55
  • 1
    @Reza note that this question is about the ArcGIS Server JavaScript API rather than ArcMap Commented Aug 11, 2014 at 3:20

3 Answers 3

6

If the Graphic is added directly to the map, you can just call map.graphics.remove(the_graphic), where the_graphic is the graphic that you want to remove.

You can also call map.graphics.clear(); to remove all graphics added in this way.

If you have added graphics to a particular graphics layer, then you need to remove the graphic from that particular layer, by calling something like gLayer.remove(the_graphic) where gLayer is your graphic layer and the_graphic is the graphic that you want to remove.

For completeness sake, if you want to remove all graphics contained in the map, you can use the following function:

 function clearGraphics() {
 //first remove all graphics added directly to map
 map.graphics.clear();
 //now go into each graphic layer and clear it
 var graphicLayerIds = map.graphicsLayerIds;
 var len = graphicLayerIds.length;
 for (var i = 0; i < len; i++) {
 var gLayer = map.getLayer(graphicLayerIds[i]);
 //clear this Layer
 gLayer.clear();
 }
 }
answered Aug 11, 2014 at 3:31
1
  • I wanted to remove only one graphic, not the whole map. Commented Aug 11, 2014 at 5:59
4

The trick is to know the graphic that coresponds to the geometry.

So you only have to use :

map.graphics.remove(your_graphic);
PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
answered Aug 11, 2014 at 5:59
5
  • 3
    You should note that this will not work if the given graphic is added to a particular graphic layer Commented Aug 11, 2014 at 8:19
  • I don't use layers. I add them dirrectrly to the map.graphics. Commented Aug 11, 2014 at 8:34
  • 2
    You might not be doing this right now, but this warning for other people who search for this, and reach this post in the future. The clarification is for them. Commented Aug 11, 2014 at 8:35
  • this answer seems almost like a copy/paste of the longer and more thorough answer that @DevdattaTengshe gave. Why not just accept that? Commented Jul 26, 2015 at 22:32
  • Because @DevdattaTengshe removes all graphics, I wanted to remove only one graphic. Commented Jul 27, 2015 at 6:32
1

It sounds like you're working with graphics elements on the map, which were added using the Editor toolbar.

In this case, the graphics are probably located in the map's default graphicsLayer, or in another graphics layer. See map.graphicsLayerIds for a method to iterate through these layers.

Once you have a pointer to the graphic via its layer, you can then delete it.

answered Aug 11, 2014 at 3:24

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.