cesium三維WebGL應(yīng)用開發(fā)過程中需要精確定位初始化場景位置時工窍,可采用以下思路
首先粗略定位場景位置衡蚂,參見http://www.reibang.com/p/7736e0cf97d1
其次鼠標移動場景到最佳視角,假設(shè)該視角就是需要精確定位的視角莫秆。
然后讓鼠標左鍵單擊場景時輸出6個視角參數(shù)到控制臺凭语,把這6個參數(shù)反填會代碼里即可掀亥。
回顧一下視角設(shè)置代碼,如下
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個視角參數(shù)到控制臺的關(guān)鍵代碼
//獲取相機視角
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個視角參數(shù)到控制臺的完整代碼(可運行)
該代碼基于SuperMap版cesium框架鸦列,可在SuperMap的在線沙箱運行租冠。點擊以下示例中的源碼按鈕,把本文下面的代碼全部復(fù)制到沙箱里覆蓋薯嗤,點擊運行顽爹,F(xiàn)12觀察瀏覽器控制臺,已輸出6個對應(yīng)的參數(shù)骆姐。
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', {
//創(chuàng)建地形服務(wù)提供者的實例镜粤,url為SuperMap iServer發(fā)布的TIN地形服務(wù)
terrainProvider: new Cesium.CesiumTerrainProvider({
url: URL_CONFIG.SiChuan_TERRAIN,
isSct: true//地形服務(wù)源自SuperMap iServer發(fā)布時需設(shè)置isSct為true
}),
});
//添加SuperMap iServer發(fā)布的影像服務(wù)
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切換到特定視角,也可以通過該種方法獲取地物的最佳視角玻褪,然后把視角參數(shù)寫到視角切換函數(shù)中肉渴。