How to get coordinates (longitude, latitude, altitude) on mouse click (can't get altitude)

3,821 views
Skip to first unread message

Vitali Pasichnyk

unread,
May 29, 2015, 8:27:50 AM5/29/15
to cesiu...@googlegroups.com
Hi all!
I am trying to get coordinates on mouse click. My code:

//get position from globe and set model
function getPositionFromGlobe() {
    var viewer = Earth.variables.cesiumViewer;
    var scene = Earth.variables.cesiumScene;

    var ellipsoid = scene.globe.ellipsoid;

    var cartographic;
    var longitudeString;
    var latitudeString;
    var heightString;

    var handler = new Cesium.ScreenSpaceEventHandler(scene.canvas),
        onEarthClick = function (e) {
            if (e.which == 3) {//right button
                $(this).unbind('mousedown', onEarthClick);

                Earth.variables.currentModel.longitude = parseFloat(longitudeString);
                Earth.variables.currentModel.latitude = parseFloat(latitudeString);
                Earth.variables.currentModel.altitude = parseFloat(heightString);
   
                setModelToCesium(Earth.variables.currentModel);
            }
        };

    handler.setInputAction(function (movement) {
        var cartesian = viewer.camera.pickEllipsoid(movement.endPosition, ellipsoid);
        if (cartesian) {
            cartographic = ellipsoid.cartesianToCartographic(cartesian);
            longitudeString = Cesium.Math.toDegrees(cartographic.longitude).toFixed(15);
            latitudeString = Cesium.Math.toDegrees(cartographic.latitude).toFixed(15);
            heightString = Cesium.Math.toDegrees(cartographic.height).toFixed(15);
        }
    }, Cesium.ScreenSpaceEventType.MOUSE_MOVE);

    $("#map3d").mousedown(onEarthClick);
}


If I use with standart terrain - I can get longitude and latitude, but altitude is always  = 5 or 10.
When I use smallterrain (http://cesiumjs.org/tilesets/terrain/smallterrain) or STK World terrain meshes, I get altitude = 5 or 10 too.
It`s important for me to get altitude from clicked point. I can not put model correctly on the hills or mountains.
Anyone can tell me the way to do this?
Regards.

Hannah Pinkos

unread,
May 29, 2015, 10:14:08 AM5/29/15
to cesiu...@googlegroups.com
Hello!

You can use camera.getPickRay to get the height:

var ray = viewer.camera.getPickRay(movement.endPosition);
var position = viewer.scene.globe.pick(ray, viewer.scene);
if (Cesium.defined(position)) {
    var cartographic = Cesium.Ellipsoid.WGS84.cartesianToCartographic(position);
    var height = cartographic.height
}

You don't have to use Cesium.Math.toDegrees on the height, it is given in meters.

You can also use the ScreenSpaceEventHandler to handle the click event directly. 
Use ScreenSpaceEventType.LEFT_CLICK.  Then, replace movement.endPosition with movement.position in the handler function.

Best, 

-Hannah
Reply all
Reply to author
Forward
0 new messages