Solved! Go to Solution.
Can you post some code to reproduce what you're seeing? I'm guessing the argument you're passing to GraphicsLayer.remove() isn't a graphic.
Here's a simple page that shows using GraphicsLayer.remove(): http://jsfiddle.net/RsbD2/
function showSelection(featureSet) {
//remove all the selectedGraphics from the map, unless the CTRL key is pressed, leaving any drawnGraphics on the map
if (!selectEvent.ctrlKey) {
selectedGraphics.clear();
accounts = [];
}
//QueryTask returns a feature set. Loop through the featureset and add them to the map
dojo.forEach(featureSet.features, function(feature){
var graphic = feature;
//add the feature if it is not already in the collection
if (accounts.indexOf(feature.attributes.ACCT) == -1) {
accounts.push(feature.attributes.ACCT);
graphic.setSymbol(symbol);
//add the graphic to the selectedGraphics layer
selectedGraphics.add(graphic);
} else {
accounts.splice(accounts.indexOf(feature.attributes.ACCT),1);
selectedGraphics.remove(graphic);
}
});
}
Are you sure you're getting to selectGraphics.remove()? Try putting a console.log("removed graphic"); after selectGraphics.remove(). I ask because I think you're using array.splice incorrectly. Check out the MDN doc for more info: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/splice
Taking a closer look at this, graphic is a reference to a feature returned from a query task, meaning that it isn't actually in your graphics layer yet. You need to find the graphic in selectedGraphics.graphics with a particular ACCT value and pass that to selectedGraphics.remove(). I would use dojo.filter or maybe dojo.some with selectedGraphics.graphics to do this.
function showSelection(featureSet) {
//remove all the selectedGraphics from the map, unless the CTRL key is pressed, leaving any drawnGraphics on the map
if (!selectEvent.ctrlKey) {
selectedGraphics.clear();
accounts = [];
}
//QueryTask returns a feature set. Loop through the featureset and add them to the map
dojo.forEach(featureSet.features, function(feature){
var graphic = feature;
//add the feature if it is not already in the collection
if (accounts.indexOf(feature.attributes.ACCT) == -1) {
accounts.push(feature.attributes.ACCT);
graphic.setSymbol(symbol);
//add the graphic to the selectedGraphics layer
selectedGraphics.add(graphic);
} else {
accounts.splice(accounts.indexOf(feature.attributes.ACCT),1);
//get the graphic that matches the feature
var filteredGraphics = dojo.filter(selectedGraphics.graphics, function(item) {
return item.attributes.ACCT == feature.attributes.ACCT;
})
graphic = filteredGraphics[0];
selectedGraphics.remove(graphic);
}
});
}