Select to view content in your preferred language

How to zoom to a KMLLayer?

2618
6
Jump to solution
08-16-2016 04:57 AM
davidoloughlin
Occasional Contributor

I've created a new KMLLayer, added it to my map, and I can see it once I manually pan and zoom to it. I can't seem to figure out how to programmatically zoom to the new layer. I try to change the map extent to the new layer's extent (e.g., kmlLayer.fullExtent) but KMLLayer appears to only implement initialExtent, which is huge.

0 Kudos
1 Solution

Accepted Solutions
FC_Basson
MVP Regular Contributor

In the sandbox example, change the kml variable scope to a global variable (just remove the var declaration).

Then in the Chrome console, change the console output to log the sandbox's.

Then you can run kml.getLayers() to view the kml layers.

View solution in original post

0 Kudos
6 Replies
FC_Basson
MVP Regular Contributor

It seems possible for a vector KML.  The API reference page for KML layers (KMLLayer | API Reference | ArcGIS API for JavaScript 3.17 ) shows how to do it (near the top).

From the API reference page:

The following code snippet shows how to get a KML layer's layers, checks that a layer has graphics, unions the layer extents and then zooms to the new extent:

  var kmlExtent, layers = kml.getLayers();
  dojo.forEach(layers, function(lyr) {
    if ( lyr.graphics && lyr.graphics.length > 0 ) {
      var lyrExtent = esri.geometry.geographicToWebMercator(
        esri.graphicsExtent(lyr.graphics)
      );
      if ( globals.kmlExtent ) {
        kmlExtent = kmlExtent.union(lyrExtent);
      } else {
        kmlExtent = lyrExtent;
      }
  });
  map.setExtent(kmlExtent);

Is your KML layer a ground overlay?

davidoloughlin
Occasional Contributor

Thanks very much for the code snippet. I've tried using ground overlays but also just a simple polygon, Wyoming.kml used here. In both cases, kml.getLayers() returns [],  no layers to iterate through. 

0 Kudos
FC_Basson
MVP Regular Contributor

Have you tried your KML source in the sandbox example?  That kml returns an array with a single layer object.

0 Kudos
davidoloughlin
Occasional Contributor

Thanks very much for your patience. I'm obviously missing something but how can you tell? I modify this sandbox example, adding the line 'console.log(kml.getLayers())' below the line 'map.addLayer(kml);', then run it. Looking at the console (in chrome anyway), I just see '[]'.

0 Kudos
FC_Basson
MVP Regular Contributor

In the sandbox example, change the kml variable scope to a global variable (just remove the var declaration).

Then in the Chrome console, change the console output to log the sandbox's.

Then you can run kml.getLayers() to view the kml layers.

0 Kudos
davidoloughlin
Occasional Contributor

Thanks. That explains it. Needed to move my kml.getLayers() loop into the "load" event handler function.

0 Kudos