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.
Name | Type | Description |
---|---|---|
windowPosition | Cartesian2 | The x and y coordinates of a pixel. |
result | Ray | optionalThe 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.
Name | Type | Default | Description |
---|---|---|---|
windowPosition | Cartesian2 | The x and y coordinates of a pixel. | |
ellipsoid | Ellipsoid | Ellipsoid.WGS84 | optionalThe ellipsoid to pick. |
result | Cartesian3 | optionalThe 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);
但是两个方法得到的结果不是相等,存在误差,暂时不明原因