首页 > 建站教程 > WebGL教程 Threejs教程 >  cesium 根据entity定位调整高度正文

cesium 根据entity定位调整高度

在cesium开发中,在不同entity之间跳转,如果直接把高度写死,会出现有的视角太高,有的太低,这是因为高度固定,由于海拔不同,entity高度也不同。所以需要获取entity的高度再加上固定高度,最后定位过去,就可以了:

const entity = props.viewer.entities.getById('matrix-'+id)
if(entity) {
  // 初始的高度
  let height = 3500
  const boundingSphereScratch = new Cesium.BoundingSphere()
  const state = viewer.dataSourceDisplay.getBoundingSphere(
    entity,
    false,
    boundingSphereScratch
  )
  if (state !== Cesium.BoundingSphereState.FAILED) {
    const cartographic = Cesium.Cartographic.fromCartesian(boundingSphereScratch.center);
    // const longitude = Cesium.Math.toDegrees(cartographic.longitude);
    // const latitude = Cesium.Math.toDegrees(cartographic.latitude);
    // 初始高度+endity的高度
    height = height + cartographic.height;
  }
  viewer.flyTo(entity, {
    duration: 1,
    offset: new Cesium.HeadingPitchRange(
      Cesium.Math.toRadians(0),
      Cesium.Math.toRadians(-90),
      height
    )
  })
}