how remove new Graphic() a map?

3106
9
Jump to solution
04-17-2016 10:04 AM
CloudingSoft
Occasional Contributor

hello, this is how I add a dynamic new Graphic() to my map, by clicking a button.

idButtonDojo = 1;

var newButton = 'ButtonNodeAddMark_'+idButtonDojo;

// <data> It is a variable data

$( "#"+newButton ).on( "click", {

   y : <data>,

   x : <data>,

  nombre : <data>,

  direccion : <data>,

  telefono : <data>,

   color : <data>

}, addDinamicMark );

function addDinamicMark(event) {

   var mapPnt = new Point(event.data.y,event.data.x, new SpatialReference({wkid:4326}));

   _width=_height=50;

   setMark(webMercatorUtils.geographicToWebMercator(mapPnt),event.data.nombre,event.data.direccion,event.data.telefono,event.data.color,_width,_height);

}

function setMark(pnt,nombre,direccion,telefono,color,_width,_height) {

   var Symbol = new PictureMarkerSymbol("https://static.arcgis.com/images/Symbols/Shapes/"+color+"Pin1LargeB.png",_width,_height);

   var Template = new InfoTemplate("${Nombre}","<i><font color='grey'>Dirección:</font></i> ${Direccion}<br />\n\
  <i><font color='grey'>Telefono:</font></i> ${Contacto}<br />");

   var mark = new Graphic(

  pnt,

   Symbol,

  {

   "Nombre": nombre,

   "Direccion": "<br/>"+direccion,

   "Contacto": "<br/>"+telefono

  },

   Template);

   map.graphics.add(mark);

   map.infoWindow.resize(270, 350);

}

Now I want to remove this same NEW GRAPHIC() the map with another button, how I can do?

Tags (1)
0 Kudos
1 Solution

Accepted Solutions
RobertScheitlin__GISP
MVP Emeritus

Fabien,

  OK, Don't forget to mark your questions as answered by clicking on the "Correct Answer" link on the reply that answered your question.

View solution in original post

0 Kudos
9 Replies
AdrianWelsh
MVP Honored Contributor

Hi Fabien,

Check out the remove graphic method from the GraphicsLayer class:

GraphicsLayer | API Reference | ArcGIS API for JavaScript

CloudingSoft
Occasional Contributor

ok but can not find a simple example to apply in my code

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Fabien,

  This is how you do that.

require "dojo/_base/array", dojoArray

on(dojo.byId("deleteBtn"), "click", lang.hitch(this,function(){

     dojoArray.some(map.graphics.graphics, function(gra, index){

          if(gra.attribbutes.nombre === 'what ever'){

               map.graphics.remove(gra);

          }

     });

}));

0 Kudos
CloudingSoft
Occasional Contributor

applying this code gives me this error:

"Uncaught ReferenceError: lang is not defined"

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

That means you need the dojo/lang require

0 Kudos
CloudingSoft
Occasional Contributor

ok, and as would pass a parameter to compare the gran.attributes.Nombre?

var _myVar_ = "my point name";

on(dojo.byId("deleteBtn"), "click", lang.hitch(this,function(){

     dojoArray.some(map.graphics.graphics, function(gra, index){

         

if(gra.attributes && gra.attributes.Nombre === _myVar_ ){

   map.graphics.remove(gra);

}

     });

}));

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Fabien,

you would need to get the scope of the dojoArray function to the same scope as the _myVar_ is in so to do that you need a lang.hitch on the dojoArray function.

var _myVar_ = "my point name";
on(dojo.byId("deleteBtn"), "click", lang.hitch(this,function(){
     dojoArray.some(map.graphics.graphics, lang.hitch(this, function(gra){
        if(gra.attributes && gra.attributes.Nombre === _myVar_ ){
           map.graphics.remove(gra);
        }
     }));
}));

Or using my recommend dojo version of code from your previous post set PictureMarkerSymbol to map

on(dojo.byId("deleteBtn"), "click", lang.hitch(this,function(){
     var selected = registry.byId('choferSelect').get('value');
     dojoArray.some(map.graphics.graphics, lang.hitch(this, function(gra){
        if(gra.attributes && gra.attributes.Nombre === choferes[selected - 1].nombre){
           map.graphics.remove(gra);
        }
     }));
}));
CloudingSoft
Occasional Contributor

good, but not in all cases use the tag <select>.

thanks for the help

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Fabien,

  OK, Don't forget to mark your questions as answered by clicking on the "Correct Answer" link on the reply that answered your question.

0 Kudos