basemap = new esri.layers.ArcGISTiledMapServiceLayer(basemapURL); map.addLayer(basemap); kml = new esri.layers.KMLLayer(kmlUrl); map.addLayer(kml); dataLayer = new esri.layers.GraphicsLayer(); map.addLayer(dataLayer);
Solved! Go to Solution.
Try explicitly setting the layer index using the optional 'index' property in the addLayer(layer, index?) method.
http://help.arcgis.com/en/webapi/javascript/arcgis/help/jsapi/map.htm#addLayer
-Andy
Esri Developer Network
It seems no matter what I do the kml layer stays on top. I tried using the index and it still had the kml layer on top. I added another graphics layer and was able to reorder them with reorderLayer fine. When I try to reorder the kml layer it refuses to change position. I added another dynamictiled layer and that reordered with the graphics layers just fine, but still the KML layer was on top and would not move. Is this a bug with the kml layer perhaps?
Depending on what's in your KML file, the resulting KML Layer can contain one or more additional layers. We touch on this in the Working with KML Layers conceptual help topic.
If you need to reorder layers from a KML Layer, use the KMLLayer's getLayers method. Once you have the layers that make up your KMLLayer, you can reorder them using map.reorderLayer().
console.log(kml.getLayers);
var kmlLayers = kml.getLayers(); console.log("KML Array Length ="+kmlLayers.length);
console.log(kml.getLayers());
dojo.connect(kml, "onLoad", function(kmlLayer) { console.log("Number of layers in kml layer: ", kmlLayer.getLayers().length); });
Are you running the console statements after the kml layer loads? Try something like:dojo.connect(kml, "onLoad", function(kmlLayer) { console.log("Number of layers in kml layer: ", kmlLayer.getLayers().length); });
dojo.connect(kml, 'onLoad', function(kmlLayers) { console.log("DEBUG: Loaded KML File"); console.log("Number of Kml Layers "+kmlLayers.getLayers().length+" Layer ID = "+kmlLayers.id); console.log(kmlLayers.getLayers()); map.reorderLayer(kmlLayers, 0); console.log("Graphics Layers "+map.graphicsLayerIds); console.log("Layers "+map.layerIds); });
You're passing your kmlLayer object to map.reorderLayer(). To make this work, you have to pass the actual graphics layer or feature layer to reorderLayer. Here's a working example: http://jsfiddle.net/swingley/BuPYY/
We need to wait for onUpdate because that fires once all the graphics are present.