cesium三维WebGL应用开发过程中需要精确定位初始化场景位置时,可采用以下思路
首先粗略定位场景位置,参见https://www.jianshu.com/p/7736e0cf97d1
其次鼠标移动场景到最佳视角,假设该视角就是需要精确定位的视角。
然后让鼠标左键单击场景时输出6个视角参数到控制台,把这6个参数反填会代码里即可。
回顾一下视角设置代码,如下
viewer.scene.camera.setView({
destination: new Cesium.Cartesian3(-1206939.1925299785, 5337998.241228442, 3286279.2424502545),
orientation: {
heading: 1.4059101895600987,
pitch: -0.20917672793046682,
roll: 2.708944180085382e-13
}
});
1.输出6个视角参数到控制台的关键代码
//获取相机视角
var handler1 = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);
handler1.setInputAction(function (movement) {
console.log(viewer.scene.camera.position.x + "," + viewer.scene.camera.position.y + "," + viewer.scene.camera.position.z);//相机XYZ
console.log("换行输出:"+"\n"+
"heading:" + viewer.scene.camera.heading + ","+"\n"+
"pitch:" + viewer.scene.camera.pitch + ","+"\n"+
"roll:" + viewer.scene.camera.roll
);
}, Cesium.ScreenSpaceEventType.LEFT_CLICK);
2.左键单击输出6个视角参数到控制台的完整代码(可运行)
该代码基于SuperMap版cesium框架,可在SuperMap的在线沙箱运行。点击以下示例中的源码按钮,把本文下面的代码全部复制到沙箱里覆盖,点击运行,F12观察浏览器控制台,已输出6个对应的参数。
http://support.supermap.com.cn:8090/webgl/examples/editor.html#terrainAndImagery
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1">
<meta name="viewport"
content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no">
<title>地形影像</title>
<link href="../Build/Cesium/Widgets/widgets.css" rel="stylesheet">
<link href="./css/pretty.css" rel="stylesheet">
<script src="./js/jquery.min.js"></script>
<script src="./js/spectrum.js"></script>
<script type="text/javascript" src="./js/require.min.js" data-main="js/main"></script>
<script src="./js/config.js"></script>
</head>
<body>
<div id="cesiumContainer"></div>
<div id='loadingbar' class="spinner">
<div class="spinner-container container1">
<div class="circle1"></div>
<div class="circle2"></div>
<div class="circle3"></div>
<div class="circle4"></div>
</div>
<div class="spinner-container container2">
<div class="circle1"></div>
<div class="circle2"></div>
<div class="circle3"></div>
<div class="circle4"></div>
</div>
<div class="spinner-container container3">
<div class="circle1"></div>
<div class="circle2"></div>
<div class="circle3"></div>
<div class="circle4"></div>
</div>
</div>
<script>
function onload(Cesium) {
var viewer = new Cesium.Viewer('cesiumContainer', {
//创建地形服务提供者的实例,url为SuperMap iServer发布的TIN地形服务
terrainProvider: new Cesium.CesiumTerrainProvider({
url: URL_CONFIG.SiChuan_TERRAIN,
isSct: true//地形服务源自SuperMap iServer发布时需设置isSct为true
}),
});
//添加SuperMap iServer发布的影像服务
var layer = viewer.imageryLayers.addImageryProvider(new Cesium.SuperMapImageryProvider({
url: URL_CONFIG.SiChuan_IMG
}));
viewer.scene.camera.setView({
destination: new Cesium.Cartesian3(-1206939.1925299785, 5337998.241228442, 3286279.2424502545),
orientation: {
heading: 1.4059101895600987,
pitch: -0.20917672793046682,
roll: 2.708944180085382e-13
}
});
$('#loadingbar').remove();
$('#toolbar').show();
//获取相机视角
var handler1 = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);
handler1.setInputAction(function (movement) {
console.log(viewer.scene.camera.position.x + "," + viewer.scene.camera.position.y + "," + viewer.scene.camera.position.z);//相机XYZ
console.log("换行输出:"+"\n"+
"heading:" + viewer.scene.camera.heading + ","+"\n"+
"pitch:" + viewer.scene.camera.pitch + ","+"\n"+
"roll:" + viewer.scene.camera.roll
);
}, Cesium.ScreenSpaceEventType.LEFT_CLICK);
}
</script>
</body>
</html>
如果需要点击某一按钮div切换到特定视角,也可以通过该种方法获取地物的最佳视角,然后把视角参数写到视角切换函数中。