AnsweredAssumed Answered

Remove a single graphics from graphics layer

Question asked by rralbritton on Apr 10, 2017
Latest reply on Apr 11, 2017 by rralbritton

I have a series of graphics that get added to a single graphics layer when a user toggles on a switch. There are different toggles for each graphic (they're routes to bus lines) but all route graphics are being added to the same graphics layer. When a user toggles off a certain route, I want the route to be removed from the graphics layer, but keep the other routes on the graphics layers if they're toggled on. I have assigned the graphic being added a unique ID, however, using graphicsLayerName.remove(graphicID) isn't working for me.



//create graphic layer
app.routesGraphicsLayer = new GraphicsLayer({

//Add graphics layer
app.routesGraphicsLayer.hide() //will get shown when toggled on

//Create unique variable names for graphic IDs
var redRoute;

//When Toggled ON:
app.handleLayerVis = function (switchName, state) {
    require(["dojo/dom","dojo/dom-class","dojox/mobile/Switch"], function(dom,domClass,Switch){
      console.log(switchName + " -- " + state);
      if (state === "on") {

      //hide popup
      if (state === "off") {;

      switch (switchName) {
         case "redShuttleSwitch":
          if (state === "on") {
            //Turn off All Active Shuttles if its currently toggled on
                //Request Route
                app.esriRouteRequest(15).then(function (routeResponse) {
                  app.routesGraphicFunction("Red", routeResponse, app.routesGraphicsLayer, redRoute);
                  //Display route
          else {
            //when route gets toggled off, remove graphic from graphics layer

app.esriRouteRequest = function() {
    var routeRequest = new esriRequest({
      url: "",
      content: {
        apikey: "ride1791",
      handleAs: "json",
      callbackParamName: "method"
    routeRequest.then(app.routeRequestSucceeded, app.requestFailed);
    return routeRequest;
  app.routeRequestSucceeded = function(data) {
    routesArray = [];
    for (i = 0; i < data.length; i++) {
    return routesArray;

  app.routesGraphicFunction = function (colorDescription, data, graphicslayer, routeGraphic) {

    var routeObjects = data;
    for (i = 0; i < routeObjects.length; i++) {
      if (routeObjects[i].Description == colorDescription){
        //Create Symbology
        var routeSLS = new SimpleLineSymbol ().setStyle(SimpleLineSymbol.STYLE_SOLID).setWidth(4).setColor(new Color(routeObjects[i].MapLineColor));
        //Create InfoTemplate
        var routesInfoTemplate = new InfoTemplate (routeObjects[i].Description+" Line", "<b>Stops Schedule: </b>" + app.moreInfoHyperlink(routeObjects[i].StopTimesPDFLink))
        //Create Graphic
        routeGraphic = new Graphic(new Polyline(app.decodeGooglePolyline(routeObjects[i].EncodedPolyline),, routeSLS, routeObjects[i].StopTimesPDFLink, routesInfoTemplate);
    //Add graphics to graphics layer