Search code examples
javascriptkmlarcgis

ArcGIS interactive KML layer


I am working on an ArcGIS map. I need to be able to interact with KML layers.

Here is a minimal version of my current code:

map = new Map("map", {
  basemap: "topo",
  center: [-108.663, 42.68],
  zoom: 6
});
parser.parse();

var kmlUrl = "https://dl.dropboxusercontent.com/u/2142726/esrijs-samples/Wyoming.kml";
var kml = new KMLLayer(kmlUrl);
map.addLayer(kml);
kml.on("load", function() {
  console.log("done");
});

Here is a fiddle

I'm looking to achieve something more like this map, which outlines the layer on hover. (This example is from the FeatureLayer class, but my KML is dynamically generated. Is it possible to create a featurelayer dynamically from KML data?)

How can I listen for mouseover on a KML shape?


Solution

  • I figured it out...

    var kmlUrl = "https://dl.dropboxusercontent.com/u/2142726/esrijs-samples/Wyoming.kml";
    var kml = new KMLLayer(kmlUrl);
    map.addLayer(kml);
    kml.on("load", function() {
      var layers = kml.getLayers()  
      layers[0].on("mouse-over", function () {
                alert("test");
            });
    });
    

    Turns out the KML layer is actually composed of FeatureLayers. The solution is to get the Feature Layers from the KMLLayer wi the getLayers() method.