Select to view content in your preferred language

Toggling visibility of KML layer

1262
1
01-04-2012 12:20 PM
BetsySchenck-Gardner
Deactivated User
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.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.name.indexOf("MODIS") != -1)  {
        mapLayers.hide();
      }
    }
  }
}

Any help would be greatly appreciated. TIA.
0 Kudos
1 Reply
BetsySchenck-Gardner
Deactivated User
I was able to answer my own question.  Instead of creating a separate array in which I was keeping the name of the layer, I used .id of the Layer class to save the name and then used the getLayer function to pull the ids back out.  Once I did that, I was able to turn on and off any kml layers on the map.
0 Kudos