Cesium 鼠标点击获取地理坐标

Cesium中要获取鼠标点击位置的实际地理坐标,思路可以这样:鼠标点击获取屏幕的坐标,即X、Y;然后通过camera中的方法转换为地理坐标。

要获取屏幕坐标,肯定和camera有关,所以直接在camera中找方法,结果找到了以下两个可用的方法:

getPickRay(windowPosition, result) → Ray

Create a ray from the camera position through the pixel at windowPosition in world coordinates.

NameTypeDescription
windowPositionCartesian2The x and y coordinates of a pixel.
resultRayoptionalThe object onto which to store the result.
Returns:

Returns the Cartesian3 position and direction of the ray.

pickEllipsoid(windowPosition, ellipsoid, result) → Cartesian3

Pick an ellipsoid or map.

NameTypeDefaultDescription
windowPositionCartesian2The x and y coordinates of a pixel.
ellipsoidEllipsoidEllipsoid.WGS84optionalThe ellipsoid to pick.
resultCartesian3optionalThe object onto which to store the result.
Returns:

If the ellipsoid or map was picked, returns the point on the surface of the ellipsoid or map in world coordinates. If the ellipsoid or map was not picked, returns undefined.


下面列出两种方法的具体使用:

 getPickRay:

 var handler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);
        handler.setInputAction(function (movement) {
            var windowPosition = viewer.camera.getPickRay(movement.position);
            var cartesianCoordinates = viewer.scene.globe.pick(windowPosition, viewer.scene);
            alert(Cesium.Math.toDegrees(cartoCoordinates.longitude)
                + "," +
                Cesium.Math.toDegrees(cartoCoordinates.latitude)
                + "," +
                Cesium.Math.toDegrees(cartoCoordinates.height) 
        }, Cesium.ScreenSpaceEventType.LEFT_CLICK);

 pickEllipsoid:

            var cartoCoordinates = viewer.scene.globe.ellipsoid.cartesianToCartographic(cartesianCoordinates);
            var cartesian2 = viewer.camera.pickEllipsoid(movement.position, scene.globe.ellipsoid);
            var carto2 = viewer.scene.globe.ellipsoid.cartesianToCartographic(cartesian2);

104231_VNuY_2667750.jpg

但是两个方法得到的结果不是相等,存在误差,暂时不明原因

转载于:https://my.oschina.net/nuistmyx/blog/646439

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值