cesium 模型随地图缩放
时间: 2023-12-13 07:32:45 浏览: 277
以下是cesium模型随地图缩放的代码实现:
```javascript
// 获取当前场景
var scene = viewer.scene;
// 获取当前相机
var camera = viewer.camera;
// 获取当前地图中心点
var center = camera.pickEllipsoid(new Cesium.Cartesian2(viewer.canvas.clientWidth / 2, viewer.canvas.clientHeight / 2));
// 监听地图缩放事件
viewer.scene.postRender.addEventListener(function() {
// 获取当前地图中心点
var newCenter = camera.pickEllipsoid(new Cesium.Cartesian2(viewer.canvas.clientWidth / 2, viewer.canvas.clientHeight / 2));
if (newCenter) {
// 计算缩放比例
var height = scene.globe.getHeight(newCenter);
var distance = Cesium.Cartesian3.distance(camera.position, newCenter);
var pixelSize = scene.globe.ellipsoid.cartesianToCartographic(newCenter).height / viewer.canvas.clientHeight;
var scale = distance / pixelSize / height;
// 计算缩放中心点
var centerScratch = new Cesium.Cartesian3();
var centerRay = camera.getPickRay(new Cesium.Cartesian2(viewer.canvas.clientWidth / 2, viewer.canvas.clientHeight / 2));
var centerPosition = scene.globe.pick(centerRay, scene);
if (centerPosition) {
Cesium.Cartesian3.fromRadians(centerPosition.longitude, centerPosition.latitude, height, scene.globe.ellipsoid, centerScratch);
}
// 设置缩放中心点和缩放比例
camera.lookAt(centerScratch, new Cesium.HeadingPitchRange(camera.heading, camera.pitch, scale));
}
});
```
以上代码实现了监听地图缩放事件,获取当前地图中心点,计算缩放比例,计算缩放中心点,设置缩放中心点和缩放比例的功能,从而实现了cesium模型随地图缩放的效果。
阅读全文