【超图+CESIUM】【基础API使用示例】13、超图|CESIUM - 通过点击获取当前场景的视角和当前点击的位置的坐标

前言

	缺少前置学习使用资料,请自行查阅:[https://blog.csdn.net/weixin_44402694/article/details/123110136](https://blog.csdn.net/weixin_44402694/article/details/123110136)
	以下示例使用到的公共静态资料,不建议下载,建议官网自行下载超图Build资源,示例所涉及图片会在示例使用到时提供出来。如有需要可下载:[https://download.csdn.net/download/weixin_44402694/82180350](https://download.csdn.net/download/weixin_44402694/82180350)。
	通过点击获取当前场景的视角和当前点击的位置的坐标。

使用

  • 效果
    在这里插入图片描述

  • 完整代码

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>获取场景当前的视角参数</title>
    <link href="./public/Build/Cesium/Widgets/widgets.css" rel="stylesheet" />
    <script
      type="text/javascript"
      src="./public/Build/Cesium/Cesium.js"
    ></script>
    <style>
      * {
        margin: 0;
        padding: 0;
      }
      html,
      body,
      #cesium-container {
        width: 100%;
        height: 100%;
      }
      .cesium-viewer-navigationContainer {
        display: none;
      }
      .panel {
        width: 300px;
        position: fixed;
        left: 10px;
        right: 10px;
        z-index: 1;
        box-sizing: border-box;
        padding: 10px;
        display: flex;
        flex-direction: column;
        background-color: #fff;
      }
      .panel span {
        color: blueviolet;
      }
    </style>
  </head>
  <body>
    <div id="cesium-container" />
    <div class="panel">
      <div class="current-clicked-position">
        <h2>当前点击的位置(点击场景触发)</h2>
        <p>经度 <span></span></p>
        <p>纬度 <span></span></p>
        <p>高度 <span></span></p>
      </div>
      <div class="scene-camera-params">
        <h2>当前场景视角参数</h2>
        <p>x <span></span></p>
        <p>y <span></span></p>
        <p>z <span></span></p>
        <p>heading <span></span></p>
        <p>pitch <span></span></p>
        <p>roll <span></span></p>
      </div>
    </div>
    <script>
      // new Cesium.Cartesian3.fromDegrees 经纬度坐标
      // new Cesium.Cartesian3 投影坐标
      let viewer = null, scene = null
      window.onload = function () {
        viewer = new Cesium.Viewer('cesium-container')
        scene = viewer.scene
        getSceneCurrentCameraParamsHandler()
      }
      // 获取场景当前的视角参数|获取当前点击的位置
      function getSceneCurrentCameraParamsHandler() {
        const handler = new Cesium.ScreenSpaceEventHandler(scene.canvas) // event事件处理程序
        handler.setInputAction((e) => {
          const { longitude, latitude, height } = getCurrentClickedPosHandler(e)
          addPointAtCurrentClickedPosHandler(longitude, latitude, height)
          getCameraParamsHandler()
        }, Cesium.ScreenSpaceEventType.LEFT_CLICK)
      }
      // 获取当前点击的位置的经纬度高度
      function getCurrentClickedPosHandler(e) {
        //获取点击位置笛卡尔坐标
        const position = scene.pickPosition(e.position)
        //将笛卡尔坐标转化为经纬度坐标
        const cartographic = Cesium.Cartographic.fromCartesian(position)
        const longitude = Cesium.Math.toDegrees(cartographic.longitude)
        const latitude = Cesium.Math.toDegrees(cartographic.latitude)
        let height = cartographic.height
        if (height < 0) {
          height = 0
        }
        const ps = document.querySelectorAll('.current-clicked-position p')
        const lon = ps[0].querySelector('span') // 经度
        lon.innerHTML = longitude
        const lat = ps[1].querySelector('span') // 纬度
        lat.innerHTML = latitude
        const hei = ps[2].querySelector('span') // 高度
        hei.innerHTML = height
        return {
          longitude,
          latitude,
          height,
        }
      }
      // 在当前点击的位置删除之前添加的点位并进行当前点位的添加
      function addPointAtCurrentClickedPosHandler(longitude, latitude, height) {
        //首先移除之前添加的点
        viewer.entities.removeAll()
        //在点击位置添加对应点
        viewer.entities.add(
          new Cesium.Entity({
            point: new Cesium.PointGraphics({
              color: new Cesium.Color(1, 1, 0),
              pixelSize: 5,
              outlineColor: new Cesium.Color(0, 1, 1),
            }),
            position: Cesium.Cartesian3.fromDegrees(
              longitude,
              latitude,
              height + 1
            ),
          })
        )
      }
      // 获取当前场景的视角参数 - 笛卡尔坐标的xyz
      function getCameraParamsHandler() {
        const { position, heading, pitch, roll } = scene.camera
        const { x, y, z } = position
        const ps = document.querySelectorAll('.scene-camera-params p')
        const xSpan = ps[0].querySelector('span') // x
        xSpan.innerHTML = x
        const ySpan = ps[1].querySelector('span') // y
        ySpan.innerHTML = y
        const zSpan = ps[2].querySelector('span') // z
        zSpan.innerHTML = z
        const headingSpan = ps[3].querySelector('span') // heading
        headingSpan.innerHTML = heading
        const pitchSpan = ps[4].querySelector('span') // pitch
        pitchSpan.innerHTML = pitch
        const rollSpan = ps[5].querySelector('span') // roll
        rollSpan.innerHTML = roll
      }
    </script>
  </body>
</html>

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值