Cesium开源

cesium从terrain数据中获取地表高程

2018-12-31  本文已影响2人  WebGiser

功能:在cesium三维球上加载terrain高程数据,并获取鼠标单击处的经度、纬度、高程。
参考:http://blog.sina.com.cn/s/blog_15e866bbe0102xoo7.html

var viewer = new Cesium.Viewer('cesiumContainer');

//加载terrain高程地形数据
var terrainProvider = new Cesium.CesiumTerrainProvider({
         url: "http://localhost:8080/wzf_terrain", 
         requestVertexNormals : true
});
viewer.scene.terrainProvider = terrainProvider;

//设置初始位置
viewer.camera.setView({
    destination:Cesium.Cartesian3.fromDegrees(80, 30, 10000)
});


//获取鼠标单击处的经度、纬度、高程
var CesiumEventHandler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);
    var ellipsoid = viewer.scene.globe.ellipsoid;   //得到当前三维场景的椭球体
CesiumEventHandler.setInputAction(function (movement) {
    var cartesian = viewer.camera.pickEllipsoid(movement.position, ellipsoid);
    if (cartesian) {
        var cartographic = Cesium.Cartographic.fromCartesian(cartesian);  //将笛卡尔坐标转换为地理坐标
        var longitudeDegree = Cesium.Math.toDegrees(cartographic.longitude);
        var latitudeDegree = Cesium.Math.toDegrees(cartographic.latitude);
        var cartographic2 = Cesium.Cartographic.fromDegrees(longitudeDegree, latitudeDegree);
        var terrainLevel = 14;
        var promise = Cesium.sampleTerrain(terrainProvider, terrainLevel, [cartographic2]);
        Cesium.when(promise, function (updatedPositions) {
                if (updatedPositions.length > 0) {
                        console.log(
                            'lng:' + Cesium.Math.toDegrees(updatedPositions[0].longitude)+
                            ',lat:' + Cesium.Math.toDegrees(updatedPositions[0].latitude) +
                            ',height:' +  (updatedPositions[0].height ? updatedPositions[0].height : 0));
                } else {
                    console.log('无法获取高程');
                }
        });
    }  
}, Cesium.ScreenSpaceEventType.LEFT_CLICK);
image.png
上一篇下一篇

猜你喜欢

热点阅读