How to ZOOM to KML ?


I need to zoom to my loaded KML in javascript.

This is my source code inside my index.html

// Load my QGIS published KML -----------------------------

    var viewer = new Cesium.Viewer('cesiumContainer');

// How to FlyTo or ZoomTo the extents of a KML file? Something like:

What is the correct syntax ?

Thank you for any help


var viewer = new Cesium.Viewer(‘cesiumContainer’);

var promise = viewer.dataSources.add(Cesium.KmlDataSource.load(‘gaetaDoc.kml’));

// when the DS done loading datam fly to it (flyTo would get the resolved value which is the filled data source)

Cesium.when(promise, viewer.flyTo);


I changed your code and now it works :

var promise = viewer.dataSources.add(Cesium.KmlDataSource.load('gaetaDoc.kml'));

Correct ?

Thank you for the suggestion


Yea. I forgot that flyTo also accepts a promise. Cheers :slight_smile:

You don’t need the Cesium.when either. The below works fine.

var promise = viewer.dataSources.add(Cesium.KmlDataSource.load(‘gaetaDoc.kml’));

Yes, thanks. But, "Cesium.when(viewer.flyTo(promise));" zoom only
after fully loaded KML.
In my case it works better.

Great !



They are both supposed to run after the KML fully loaded. The
"Cesium.when(viewer.flyTo(promise)" is supposed to give you the ability to
do something after flight was over -
Cesium.when(viewer.flyTo(promise, someMethodAfterFlight)
Haven't tried either, but according to docs it's supposed to work :slight_smile: