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?
Solved! Go to Solution.
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.
Hi Fabien,
Check out the remove graphic method from the GraphicsLayer class:
ok but can not find a simple example to apply in my code
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);
}
});
}));
applying this code gives me this error:
"Uncaught ReferenceError: lang is not defined"
That means you need the dojo/lang require
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);
}
});
}));
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); } })); }));
good, but not in all cases use the tag <select>.
thanks for the help
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.