My map has some markers and when I click to identify a marker, I'm adding a marker to it. When I click the next marker though, I'd like for the previous marker to be removed since I'm only interested in the more recently identified element.

I'm using the legacy modules, JavaScript API.

    var symbol = new esri.symbol.SimpleMarkerSymbol(esri.symbol.SimpleMarkerSymbol.STYLE_SQUARE, 18,
        new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_SOLID,
        new dojo.Color([0,0,0]), 1),        // border color
        new dojo.Color([255,255,0,0.80]));  // fill color
    App.map.graphics.add(new esri.Graphic(queryResults[0].feature.geometry, symbol));
  • I'm very new to the arcgis stuff so if my terms are not used correctly, please let me know and I'll do my best to clarify. The map marker is coming from the map service/layer. The marker I'm adding is simply a square symbol to indicate which marker received the mouse click. – HPWD Feb 27 '14 at 16:40

Try using something like map.graphics.clear() (It your case it looks like it may need to be App.map.graphics.clear() but you can put it in context of your code.) But you can call a .clear() method on the graphics layer in the map to clear all the graphics. If you just want to remove specific graphics without removing all of them, that would require other steps, such as saving a reference to each individual graphic and then deleting them individually as needed. But if you're just trying to clear and add a new marker each time, just use the .clear() method.

More details on working with graphics at https://developers.arcgis.com/javascript/jshelp/inside_graphics.html

  • The App.map.graphics.clear() is just what I needed. I was in the process of reading about the .clear() method when your answer came in which was spot-on. Thank you. – HPWD Feb 27 '14 at 18:10

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.