zoom in to mouse point

Hi,

I came across this code for zooming to mouse point, instead of the map center point:
https://gist.github.com/scothis/c9f2695f03380ca5dd96

It works fine for 3D but not for 2D. you can check it in sandcastle.

Can you please tell me what should I do to get this work in 2D?
If you have a better way for zooming to mouse point in 2D,
I would be very grateful to know about it.

Thanks!

I don’t have a complete code sample handy, but here is some pseudocode where you need to modify

eventHandler.setInputAction(function (wheelZoomAmount) {
var cameraHeight, directionToZoom, zoomAmount;
if (mousePosition) {
cameraHeight = viewer.scene.globe.ellipsoid.cartesianToCartographic(camera.position).height || Number.MAX_VALUE;
zoomAmount = wheelZoomAmount * cameraHeight / 1000;

    if (scene.mode !== Cesium.SceneMode.SCENE3D) {
        //Determine Camera position https://groups.google.com/d/msg/cesium-dev/QSFf3RxNRfE/XotNBSBdFQgJ

        //calculate the vector between camera ground position and mouse ground position so you can calculate a
        //moveLeft amount and a moveForward amount;  This will need probably have a scaling factor based on height
        //similar to the zoomAmount;

        camera.zoomIn (zoomAmount);
        camera.moveLeft(leftAmount);
        camera.moveForward(forwardAmount);
    } else {
        directionToZoom = camera.getPickRay(mousePosition).direction;
        camera.move(directionToZoom, zoomAmount);
    }
}

}, Cesium.ScreenSpaceEventType.WHEEL);

``

I finally wrote something that works in 2D

here is a link to that code https://github.com/erezy/cesium-2D-zoom-in/blob/master/src/zoom-in.js

you can copy&paste it in Sandcastle.

Don’t forget to change the map to 2D.

Thanks for sharing the code sample.

FYI to people interested in this feature, a pull request is open to do this official and make it the default zoom behavior: https://github.com/AnalyticalGraphicsInc/cesium/pull/2810

This feature (#2810) was just merged into master and will be in Cesium 1.11 on July 1.

If you have the same experience I did, you will quickly get use to it and will not want to go back to the old zoom.

Patrick

Any chance there will ever be zoom out on mouse position?

Is July first soon enough?

Cheers,

-Mike

Mike, the July 1st release does not have zoom out on mouse position, though I agree with Denver in that we should absolutely support it.

There is already an issue open for it here: https://github.com/AnalyticalGraphicsInc/cesium/issues/2834

Depending on how hard it is, it might go into July 1st, but I have to refer to Dan, since he’s the one that knows this area of the code.