# API to Zoom level on TrackedEntity?

**URL:** <https://community.cesium.com/t/api-to-zoom-level-on-trackedentity/6333>\
**Category:** CesiumJS\
**Created:** [December 6, 2017, 10:44am UTC](https://community.cesium.com/t/api-to-zoom-level-on-trackedentity/6333 "2017-12-06T10:44:39Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![staffan.rolfsson](https://avatars.discourse-cdn.com/v4/letter/s/90db22/32.png) [@staffan.rolfsson](https://community.cesium.com/u/staffan.rolfsson)\
**Post date:** [December 6, 2017, 10:44am UTC](https://community.cesium.com/t/api-to-zoom-level-on-trackedentity/6333/1 "2017-12-06T10:44:39Z")

</div>

1. A concise explanation of the problem you're experiencing.  
(Not a bug, a question on how to access and set zoom level)  
When you select a new tracked entity, the viewer is set to a fixed zoom level that it keeps while tracking the moving entity.  
Using the scroll wheel on the mouse you can change this zoom level for the current tracking sequence. However if I try to do this by code "viewer.camera.Zoomout(1000);", this zoom-out is quickly replaced with the previous zoom-level on the next frame.

How can I access current zoom level, and how can I adjust zoom level with a similar result as when I scroll the mouse wheel?

2. A minimal code example. If you've found a bug, this helps us reproduce and repair it.  
(Not a bug, a question on how to access and set zoom level)

3. Context. Why do you need to do this? We might know a better way to accomplish your goal.

Check this as an example: [http://www.paralogg.se/cesium/0F300687F300491](http://www.paralogg.se/cesium/0F300687F300491)  
(default Zoom level is too close to start with, and when you have multiple entities and switches entity to follow, the zoom levels gets too close again...)

4. The Cesium version you're using, your operating system and browser.  
Current Cesium version, running on Google Cloud (Appengine), and you choose your browser... 🙂

---

<div class="post-metadata">

**Author:** ![staffan.rolfsson](https://avatars.discourse-cdn.com/v4/letter/s/90db22/32.png) [@staffan.rolfsson](https://community.cesium.com/u/staffan.rolfsson)\
**Post date:** [October 2, 2018, 8:16am UTC](https://community.cesium.com/t/api-to-zoom-level-on-trackedentity/6333/2 "2018-10-02T08:16:27Z")

</div>

Replying to my own question, to help others having the same problem:  
"viewer.camera.position.clone()" and "viewer.camera.position" did the trick.

By cloning current viewer camera position, it could be applied to the following of another entity. My code below (on toggle of entity to follow):

function toggleFollow(flNumber) {

&nbsp;&nbsp;&nbsp;&nbsp;if (follow==flNumber) {  
&nbsp;&nbsp;&nbsp;&nbsp;follow = "0"  
&nbsp;&nbsp;&nbsp;&nbsp;savedCamera = viewer.camera.position.clone(); // save camera offset before we unfollow all (else we would loose this)  
&nbsp;&nbsp;&nbsp;&nbsp;viewer.trackedEntity = undefined;  
&nbsp;&nbsp;&nbsp;&nbsp;[//console.log](https://console.log)(savedCamera);  
&nbsp;&nbsp;&nbsp;&nbsp;showPPCfor('');  
&nbsp;&nbsp;&nbsp;&nbsp;} else {  
&nbsp;&nbsp;&nbsp;&nbsp;follow = flNumber;  
&nbsp;&nbsp;&nbsp;&nbsp;viewer.trackedEntity = wingEntity[flNumber];  
&nbsp;&nbsp;&nbsp;&nbsp;// if we unfollowed all, no camera offset is available, so use the saved one  
&nbsp;&nbsp;&nbsp;&nbsp;if (Math.abs(viewer.camera.position['x'])+Math.abs(viewer.camera.position['y'])+Math.abs(viewer.camera.position['z']) \> 100000) {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;wingEntity[flNumber].viewFrom= savedCamera;  
&nbsp;&nbsp;&nbsp;&nbsp;} else { // use the latest if available  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;wingEntity[flNumber].viewFrom= viewer.camera.position;  
&nbsp;&nbsp;&nbsp;&nbsp;}

---

<div class="post-metadata">

**Author:** ![11118](https://sea2.discourse-cdn.com/cesium/user_avatar/community.cesium.com/11118/32/2377_2.png) [@11118](https://community.cesium.com/u/11118)\
**Post date:** [November 21, 2018, 3:27am UTC](https://community.cesium.com/t/api-to-zoom-level-on-trackedentity/6333/3 "2018-11-21T03:27:32Z")

</div>

If I used viewForm, It is invalid when I modify again

---

<div class="post-metadata">

**Author:** ![omar](https://sea2.discourse-cdn.com/cesium/user_avatar/community.cesium.com/omar/32/1999_2.png) [@omar](https://community.cesium.com/u/omar)\
**Post date:** [November 21, 2018, 12:45pm UTC](https://community.cesium.com/t/api-to-zoom-level-on-trackedentity/6333/4 "2018-11-21T12:45:42Z")

</div>

I’m not sure what you mean. Can you post your code or ideally a Sandcastle example so I can try it out?

You might also try some of these suggestions in this thread:

[https://groups.google.com/d/msg/cesium-dev/rOaa0L8\_SAg/x5WSAzGOBQAJ](https://groups.google.com/d/msg/cesium-dev/rOaa0L8_SAg/x5WSAzGOBQAJ)

If you find something that works please do post what it was here.

---

<div class="post-metadata">

**Author:** ![dihand](https://sea2.discourse-cdn.com/cesium/user_avatar/community.cesium.com/dihand/32/6622_2.png) [@dihand](https://community.cesium.com/u/dihand)\
**Post date:** [November 18, 2021, 3:39pm UTC](https://community.cesium.com/t/api-to-zoom-level-on-trackedentity/6333/5 "2021-11-18T15:39:07Z")

</div>

Thanks from 2021, I spent a couple of days on my different solutions. But it didn’t work - I had to go back to the standard viewer.trackedEntity and your answer helped me. Have a nice day.

---

<div class="post-metadata">

**Author:** ![staffan.rolfsson](https://avatars.discourse-cdn.com/v4/letter/s/90db22/32.png) [@staffan.rolfsson](https://community.cesium.com/u/staffan.rolfsson)\
**Post date:** [November 21, 2021, 8:21pm UTC](https://community.cesium.com/t/api-to-zoom-level-on-trackedentity/6333/6 "2021-11-21T20:21:13Z")

</div>

Glad to hear!

/Staffan
