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.

  • 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? – Reza Aug 10 '14 at 16:38
  • Thanks for your fast response. I have a button and on click I want to delete the geometry with javascript. – Manolis Xountasis Aug 10 '14 at 16:41
  • And how you select geometry which must get selected?is it included in map selection list? – Reza Aug 10 '14 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. – Manolis Xountasis Aug 10 '14 at 16:55
  • 1
    @Reza note that this question is about the ArcGIS Server JavaScript API rather than ArcMap – Stephen Lead Aug 11 '14 at 3:20
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();
        }

    }
  • I wanted to remove only one graphic, not the whole map. – Manolis Xountasis Aug 11 '14 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);
  • 3
    You should note that this will not work if the given graphic is added to a particular graphic layer – Devdatta Tengshe Aug 11 '14 at 8:19
  • I don't use layers. I add them dirrectrly to the map.graphics. – Manolis Xountasis Aug 11 '14 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. – Devdatta Tengshe Aug 11 '14 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? – Stephen Lead Jul 26 '15 at 22:32
  • Because @DevdattaTengshe removes all graphics, I wanted to remove only one graphic. – Manolis Xountasis Jul 27 '15 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.