# 前言
Cesium 中的三维坐标可分为三种情况:椭球表面、地形和模型。
# 获取椭球体表面的经纬度坐标(方法一)
| var handler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas); |
| handler.setInputAction(function(evt) { |
| var cartesian = viewer.camera.pickEllipsoid(evt.position, viewer.scene.globe.ellipsoid); |
| var cartographic = Cesium.Cartographic.fromCartesian(cartesian); |
| var lng = Cesium.Math.toDegrees(cartographic.longitude); |
| var lat = Cesium.Math.toDegrees(cartographic.latitude); |
| var mapPosition = { |
| x: lng, |
| y: lat, |
| z: cartographic.height |
| }; |
| }, Cesium.ScreenSpaceEventType.LEFT_CLICK); |
获取相机高度可以通过此法获得:
| |
| height = Math.ceil(viewer.camera.positionCartographic.height); |
# 获取椭球体表面的经纬度坐标(方法二)
| var handler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas); |
| |
| handler.setInputAction(function(evt) { |
| |
| var ellipsoid = scene.globe.ellipsoid; |
| |
| cartesian = viewer.camera.pickEllipsoid(evt.endPosition, ellipsoid); |
| |
| var cartographic = ellipsoid.cartesianToCartographic(cartesian); |
| |
| lng = Cesium.Math.toDegrees(cartographic.longitude); |
| lat = Cesium.Math.toDegrees(cartographic.latitude); |
| |
| }, Cesium.ScreenSpaceEventType.MOUSE_MOVE) |
# 获取地形表面的经纬度高程坐标(方法一)
| var handler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas); |
| handler.setInputAction(function(evt) { |
| var ray = viewer.camera.getPickRay(evt.position); |
| var cartesian = viewer.scene.globe.pick(ray, viewer.scene); |
| var cartographic = Cesium.Cartographic.fromCartesian(cartesian); |
| var lng = Cesium.Math.toDegrees(cartographic.longitude); |
| var lat = Cesium.Math.toDegrees(cartographic.latitude); |
| var mapPosition = { |
| x: lng, |
| y: lat, |
| z: cartographic.height |
| }; |
| }, Cesium.ScreenSpaceEventType.LEFT_CLICK); |
# 获取地形表面的经纬度高程坐标(方法二)
| var handler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas); |
| handler.setInputAction(function(evt) { |
| var ray = viewer.camera.getPickRay(evt.position); |
| var cartesian = viewer.scene.globe.pick(ray, viewer.scene); |
| var cartographic = Cesium.Cartographic.fromCartesian(cartesian); |
| var lng = Cesium.Math.toDegrees(cartographic.longitude); |
| var lat = Cesium.Math.toDegrees(cartographic.latitude); |
| |
| var height = viewer.scene.globe.getHeight(cartographic); |
| var mapPosition = { |
| x: lng, |
| y: lat, |
| z: height.height |
| }; |
| }, Cesium.ScreenSpaceEventType.LEFT_CLICK); |
# 获取模型表面的经纬度高程坐标(此方法借鉴于官方示例)
| var handler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas); |
| handler.setInputAction(function(evt) { |
| var scene = viewer.scene; |
| if (scene.mode !== Cesium.SceneMode.MORPHING) { |
| var pickedObject = scene.pick(evt.position); |
| if (scene.pickPositionSupported && Cesium.defined(pickedObject) && pickedObject.node) { |
| var cartesian = viewer.scene.pickPosition(evt.position); |
| if (Cesium.defined(cartesian)) { |
| var cartographic = Cesium.Cartographic.fromCartesian(cartesian); |
| var lng = Cesium.Math.toDegrees(cartographic.longitude); |
| var lat = Cesium.Math.toDegrees(cartographic.latitude); |
| var height = cartographic.height; |
| mapPosition = { |
| x: lng, |
| y: lat, |
| z: height |
| } |
| } |
| } |
| } |
| }, Cesium.ScreenSpaceEventType.LEFT_CLICK); |