How to FlyTo or ZoomTo the extents of a KML file?

I have a large variety of KML files in our library.

Most run over a fairly small area (1-3km square) but many are over much larger areas.

Here’s an example of one of the large area KML files presented on our webpage via Cesium. You can’t see the track when the zoom is finished because we are zoomed too far in:

http://www.ka72.com/Track/t/153938

Here’s an example of a smaller area KML file. This one looks fine when viewed presently:

http://www.ka72.com/Track/t/154427

Here’s part of my code from the page:

var terrainProvider = new Cesium.CesiumTerrainProvider({
url: ‘//assets.agi.com/stk-terrain/world’,
requestWaterMask: false
});
viewer.terrainProvider = terrainProvider;
viewer.dataSources.add(datasource.load(‘http://www.ka72.com/DesktopModules/ka72/TrackDetails/kml.asmx/GetFile?tid=153938’)).then(function(datasource) {
viewer.clock.shouldAnimate = false;

viewer.clock.multiplier = 30;
viewer.flyTo(datasource, { duration: 4.0, offset: { heading: 0, pitch: Cesium.Math.toRadians(-45), range: 3000 } }).then(function() {
viewer.clock.clockRange = Cesium.ClockRange.CLAMPED;
viewer.clock.shouldAnimate = true;
}
);

});

As you can see I am using a range of 3000 for the final view, but I’d rather the widget zoomed to the extent of the actual KML file.

The files all consist of:

a gx:track element that defines the whole of the track and includes times for replay purposes.

Several elements that define specific subfeatures of the track.

Previously I had a KML file with only LineString elements, and Cesium seemed to be able to zoom to the extents of that fine, but I can’t get it to work with a gx:track element.

You can obtain the raw KML files for each of the samples above from these links:

http://www.ka72.com/DesktopModules/ka72/TrackDetails/kml.asmx/GetFile?tid=153938

http://www.ka72.com/DesktopModules/ka72/TrackDetails/kml.asmx/GetFile?tid=154427

Dylan.

I believe the issue is that you’re passing the entire dataSource object to the viewer.flyTo method. If you want to have the viewer focus on a specific object from within your KML file you will need to get the specific Entity from within the dataSource.entities, an EntityCollection, and pass only that entity to flyTo.

Let me know if that solves the problem.

This is actually a problem for any DataSource that contains temporal data because of the way we are currently computing the extent. In your example, Cesium is only using the extent of the track at the current time, rather than the extent of the track over all. I think the correct solution is for you to continue to use flyTo(dataSource) and us to make it work better. I’ve submitted https://github.com/AnalyticalGraphicsInc/cesium/issues/2812 for this and it should be simple enough to do for the next release.