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– Reza2014年08月10日 16:38:48 +00:00Commented 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.Manolis Xountasis– Manolis Xountasis2014年08月10日 16:41:30 +00:00Commented Aug 10, 2014 at 16:41
-
And how you select geometry which must get selected?is it included in map selection list?Reza– Reza2014年08月10日 16:51:15 +00:00Commented 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.Manolis Xountasis– Manolis Xountasis2014年08月10日 16:55:09 +00:00Commented Aug 10, 2014 at 16:55
-
1@Reza note that this question is about the ArcGIS Server JavaScript API rather than ArcMapStephen Lead– Stephen Lead2014年08月11日 03:20:22 +00:00Commented Aug 11, 2014 at 3:20
3 Answers 3
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– Manolis Xountasis2014年08月11日 05:59:09 +00:00Commented Aug 11, 2014 at 5:59
The trick is to know the graphic that coresponds to the geometry.
So you only have to use :
map.graphics.remove(your_graphic);
-
3You should note that this will not work if the given graphic is added to a particular graphic layerDevdatta Tengshe– Devdatta Tengshe2014年08月11日 08:19:01 +00:00Commented Aug 11, 2014 at 8:19
-
I don't use layers. I add them dirrectrly to the map.graphics.Manolis Xountasis– Manolis Xountasis2014年08月11日 08:34:00 +00:00Commented Aug 11, 2014 at 8:34
-
2You 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– Devdatta Tengshe2014年08月11日 08:35:48 +00:00Commented 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?Stephen Lead– Stephen Lead2015年07月26日 22:32:06 +00:00Commented Jul 26, 2015 at 22:32
-
Because @DevdattaTengshe removes all graphics, I wanted to remove only one graphic.Manolis Xountasis– Manolis Xountasis2015年07月27日 06:32:01 +00:00Commented Jul 27, 2015 at 6:32
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.