Clearing graphic from map

758
3
Jump to solution
07-15-2014 06:53 AM
IbrahimHussein
Occasional Contributor

Hey guys, im fairly new to the javascript API.

I made a viewer where the user is able to draw and edit shapes and text. I am trying to figure out how to remove a single graphic that is selected.

the code below removes all graphics regardless if its selected or not. anyideas on what Im doing wrong?

map.graphics.on("click", function(evt){

  //window.alert("click caught");

  event.stop(evt);

  editingEnabled = true

  if (evt.ctrlKey === true || evt.metaKey === true) {  //delete feature if ctrl key is depressed

  window.alert("delete caught");

                map.graphics.clear(); // removes all graphics as opposed to only selected graphic

                currentLayer = this;

                editToolbar.deactivate();

                editingEnabled=false;

        }

});

//FIXED CODE (thanks to tim witt)  - just in case anyone came accross the same issue.

map.graphics.on("click", function(evt){

  //window.alert("click caught");

  event.stop(evt);

  editingEnabled = true

  if (evt.ctrlKey === true || evt.metaKey === true) {  //delete feature if ctrl key is depressed

  window.alert("delete caught");

                    map.graphics.remove(evt.graphic); // this removes single graphic

                currentLayer = this;

                editToolbar.deactivate();

                editingEnabled=false;

        }

});

0 Kudos
1 Solution

Accepted Solutions
TimWitt2
MVP Alum

Hey Ibrahim,

I was trying to do the same, when I eventually ended up going in a totally new direction. Check out the advanced draw tool I have created and feel free to use it: Advanced Draw - JSFiddle

I use the right-mouse click to delete graphics (and create a whole menu).

Let me know what you think!

Tim

View solution in original post

3 Replies
TimWitt2
MVP Alum

Hey Ibrahim,

I was trying to do the same, when I eventually ended up going in a totally new direction. Check out the advanced draw tool I have created and feel free to use it: Advanced Draw - JSFiddle

I use the right-mouse click to delete graphics (and create a whole menu).

Let me know what you think!

Tim

IbrahimHussein
Occasional Contributor

Ill take a look, thanks a lot!

edit - thanks again, this is what I was looking for. Should help me a great deal.

0 Kudos
TimWitt2
MVP Alum

No problem, glad I could help