# issue with Viewer.flyTo on KML entity

**URL:** <https://community.cesium.com/t/issue-with-viewer-flyto-on-kml-entity/4597>\
**Category:** CesiumJS\
**Created:** [September 16, 2016, 1:03pm UTC](https://community.cesium.com/t/issue-with-viewer-flyto-on-kml-entity/4597 "2016-09-16T13:03:40Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![normfelarca](https://avatars.discourse-cdn.com/v4/letter/n/dfb087/32.png) [@normfelarca](https://community.cesium.com/u/normfelarca)\
**Post date:** [September 16, 2016, 1:03pm UTC](https://community.cesium.com/t/issue-with-viewer-flyto-on-kml-entity/4597/1 "2016-09-16T13:03:40Z")

</div>

Hi all,

I'm seeing an issue with Viewer.flyTo (also zoomTo) on entities derived from a KML file. To illustrate, here's a Sandcastle example that loads a KML file with a single placemark:

```javascript
var viewer = new Cesium.Viewer('cesiumContainer');
var options = {
    camera : viewer.scene.camera,
    canvas : viewer.scene.canvas
};
var zoomTarget;

var kmlFile = "https://raw.githubusercontent.com/googlemaps/kml-samples/gh-pages/kml/Placemark/placemark.kml";
var promise = Cesium.KmlDataSource.load(kmlFile, options);
Cesium.when(promise, function(dataSource) {
    viewer.dataSources.add(dataSource);
    zoomTarget = dataSource.entities.values[0];
});

Sandcastle.addToolbarButton('Fly to Placemark', function() {
    viewer.flyTo(zoomTarget);
}, 'toolbar');

```

Content of the KML file:

```xml
<kml xmlns="http://www.opengis.net/kml/2.2">
    <Placemark>
        <name>
            My office
        </name>
        <description>
            This is the location of my office.
        </description>
        <Point>
            <coordinates>
                -122.087461,37.422069
            </coordinates>
        </Point>
    </Placemark>
</kml>

```

When you click the button to fly to the placemark, the viewer zooms too far ending up below ground. Clicking the button again zooms out to the correct view.

Interestingly, the issue doesn't occur with non-KML entities. In the following example, an entity is created with the same coordinates as the KML placemark above, but in this case, the viewer zooms to the appropriate view on the first button click.

```javascript
var viewer = new Cesium.Viewer('cesiumContainer');
var options = {
    camera : viewer.scene.camera,
    canvas : viewer.scene.canvas
};

var pinBuilder = new Cesium.PinBuilder();
var entity = viewer.entities.add({
    position : Cesium.Cartesian3.fromDegrees(-122.087461,37.422069),
    label : {
        text : 'pin',
        verticalOrigin : Cesium.VerticalOrigin.TOP
    },
    billboard : {
        image : pinBuilder.fromColor(Cesium.Color.SALMON, 24).toDataURL(),
        verticalOrigin : Cesium.VerticalOrigin.BOTTOM,
    }
});

Sandcastle.addToolbarButton('Fly To Entity', function() {
    viewer.flyTo(entity);
}, 'toolbar');

```

The difference between the two examples appears to be the initial bounding sphere computed for the entity. For the second example, the center of the bounding sphere is identical to the entity's position. Not so with the KML example -- the z-component in particular is much smaller. After the second button click, the bounding sphere gets recalculated and the center becomes roughly the same.

Is there anything that can be done so users don't have to click twice to zoom to KML features?

Thanks,  
--Norm

---

<div class="post-metadata">

**Author:** ![hannah](https://sea2.discourse-cdn.com/cesium/user_avatar/community.cesium.com/hannah/32/4509_2.png) [@hannah](https://community.cesium.com/u/hannah)\
**Post date:** [September 16, 2016, 2:41pm UTC](https://community.cesium.com/t/issue-with-viewer-flyto-on-kml-entity/4597/2 "2016-09-16T14:41:32Z")

</div>

Hello Norm,

Thanks for looking into this! I’ve filed an issue on our GitHub for us to look into it: [https://github.com/AnalyticalGraphicsInc/cesium/issues/4327](https://github.com/AnalyticalGraphicsInc/cesium/issues/4327)

viewer.flyTo polls to get the bounding sphere for the entity and isn’t supposed to fly to it until the bounding sphere is ready. I’m guessing flyTo is being triggered before the bounding sphere is ready

We have a bug bash coming up in the middle of October, and I’ve tagged this issue for us to look into it then. Of course, we’d gladly accept a contribution if you would like to look into fixing the problem before then =)

Here’s the function in viewer that computes the bounding sphere to fly to: [https://github.com/AnalyticalGraphicsInc/cesium/blob/master/Source/Widgets/Viewer/Viewer.js#L1811-L1819](https://github.com/AnalyticalGraphicsInc/cesium/blob/master/Source/Widgets/Viewer/Viewer.js#L1811-L1819)

And here is where the data source display gets the bounding spheres from the entity visualizers: [https://github.com/AnalyticalGraphicsInc/cesium/blob/master/Source/DataSources/DataSourceDisplay.js#L337](https://github.com/AnalyticalGraphicsInc/cesium/blob/master/Source/DataSources/DataSourceDisplay.js#L337)

You can check out our contributors documentation for how to download and build the code base: [https://github.com/AnalyticalGraphicsInc/cesium/tree/master/Documentation/Contributors/BuildGuide](https://github.com/AnalyticalGraphicsInc/cesium/tree/master/Documentation/Contributors/BuildGuide)

Sorry I don’t have any good workarounds.

Thanks again,

Hannah

---

<div class="post-metadata">

**Author:** ![hannah](https://sea2.discourse-cdn.com/cesium/user_avatar/community.cesium.com/hannah/32/4509_2.png) [@hannah](https://community.cesium.com/u/hannah)\
**Post date:** [October 20, 2016, 8:27pm UTC](https://community.cesium.com/t/issue-with-viewer-flyto-on-kml-entity/4597/3 "2016-10-20T20:27:28Z")

</div>

Hello,

This issue has been fixed and will be available in the Cesium 1.27 release available on November 1st.

Best,

Hannah

---

<div class="post-metadata">

**Author:** ![a.in.the.k](https://avatars.discourse-cdn.com/v4/letter/a/90db22/32.png) [@a.in.the.k](https://community.cesium.com/u/a.in.the.k)\
**Post date:** [March 22, 2020, 4:59pm UTC](https://community.cesium.com/t/issue-with-viewer-flyto-on-kml-entity/4597/4 "2020-03-22T16:59:01Z")

</div>

I believe this is not fixed. I have just tested with original code and all my labels and pins from KML are still underground.  
I have posted also to original [https://github.com/AnalyticalGraphicsInc/cesium/issues/4327](https://github.com/AnalyticalGraphicsInc/cesium/issues/4327)

So if anyone has a working examle of KML loading and flyTo I would be happy.
