betsyjsg

Toggling visibility of KML layer

Discussion created by betsyjsg on Jan 4, 2012
Latest reply on Jan 6, 2012 by betsyjsg
Has anyone had any success with toggling the visibility of a KML layer?  I can get a KML layer to display on a map with no problem.  I've been able to show and hide other layers such as WMS layers or dynamic layers with no problem.  But when I try to hide the KML layer, it will not remove itself from the map and all subsequent calls to add other layers are stopped.  Here is a snippet of my code for showing and hiding my KML layer:

if (modis == "true")  {
  if (mapLayers.length == 0)  {
    var kmlUrl = "http://www.ncddc.noaa.gov/website/kmls/A20113632012004.1KM.GCOOS.7DAY.L3D.OCI.png.kml";
    var lyr = new esri.layers.KMLLayer(kmlUrl);
    lyr.name = "MODIS";
    mapLayers.push(lyr);
    map.addLayer(lyr);
  }
  else  {
    var foundIt = false;
    var lyrID;
    for (i=0;i<mapLayers.length;i++)  {
      if (mapLayers[i].name.indexOf("MODIS") != -1)  {
        foundIt = true;
        lyrID = i;
      }
    }
    if (!foundIt)  {
      var kmlUrl = "http://www.ncddc.noaa.gov/website/kmls/A20113632012004.1KM.GCOOS.7DAY.L3D.OCI.png.kml";
      var lyr = new esri.layers.KMLLayer(kmlUrl);
      lyr.name = "MODIS";
      mapLayers.push(lyr);
      map.addLayer(lyr);     
    }
    else  {
      mapLayers[lyrID].show();
    }
  } 
}
else  {
  if (mapLayers.length != 0)  {
    for (i=0;i<mapLayers.length;i++)  {
      if (mapLayers[i].name.indexOf("MODIS") != -1)  {
        mapLayers[i].hide();
      }
    }
  }
}

Any help would be greatly appreciated. TIA.

Outcomes