jihongshun
7 天以前 bbdf7b9bfb33552c83bc4ddee2c36893fd43781b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<template>
  <div class="cesium-compass" ref="compass" @mousedown.prevent="startDrag">
    <!-- 指针 -->
    <div class="needle" :style="needleStyle"></div>
 
    <!-- 方向标 -->
    <div class="dir-label dir-n">N</div>
    <div class="dir-label dir-e">E</div>
    <div class="dir-label dir-s">S</div>
    <div class="dir-label dir-w">W</div>
 
    <!-- 当前度数 -->
    <div class="heading-label">{{ headingDegRounded }}°</div>
  </div>
</template>
 
<script>
export default {
  name: 'CesiumCompass',
  props: {
    viewer: { required: true } // 传入 Cesium.Viewer 实例
  },
  data() {
    return {
      headingDeg: 0,
      dragging: false,
      dragStartX: 0,
      startHeading: 0,
      rafId: null
    };
  },
  computed: {
    needleStyle() {
      return {
        transform: `rotate(${this.headingDeg}deg)`
      };
    },
    headingDegRounded() {
      // 保证角度在0~360
      let deg = (this.headingDeg % 360 + 360) % 360;
      return deg.toFixed(0);
    }
  },
  mounted() {
    const update = () => {
      if (this.viewer && this.viewer.camera) {
        try {
          const rad = this.viewer.camera.heading;
          if (typeof rad === 'number') {
            this.headingDeg = -Cesium.Math.toDegrees(rad);
          }
        } catch (e) {}
      }
      this.rafId = requestAnimationFrame(update);
    };
    this.rafId = requestAnimationFrame(update);
 
    window.addEventListener('mouseup', this.stopDrag);
    window.addEventListener('mousemove', this.onMouseMove);
  },
  beforeDestroy() {
    if (this.rafId) cancelAnimationFrame(this.rafId);
    window.removeEventListener('mouseup', this.stopDrag);
    window.removeEventListener('mousemove', this.onMouseMove);
  },
  methods: {
    startDrag(e) {
      this.dragging = true;
      this.dragStartX = e.clientX;
      this.startHeading = this.headingDeg;
      if (this.viewer && this.viewer.scene && this.viewer.scene.screenSpaceCameraController) {
        this.viewer.scene.screenSpaceCameraController.enableRotate = false;
      }
    },
    onMouseMove(e) {
      if (!this.dragging) return;
      const dx = e.clientX - this.dragStartX;
      const newHeading = this.startHeading + dx;
      this.setCameraHeading(-newHeading);
    },
    stopDrag() {
      if (!this.dragging) return;
      this.dragging = false;
      if (this.viewer && this.viewer.scene && this.viewer.scene.screenSpaceCameraController) {
        this.viewer.scene.screenSpaceCameraController.enableRotate = true;
      }
    },
    setCameraHeading(deg) {
      if (!this.viewer) return;
      const rad = Cesium.Math.toRadians(deg);
      const camera = this.viewer.camera;
      const orientation = {
        heading: rad,
        pitch: camera.pitch,
        roll: camera.roll
      };
      this.viewer.camera.setView({ orientation });
      this.headingDeg = -Cesium.Math.toDegrees(this.viewer.camera.heading || 0);
    }
  }
};
</script>
 
<style scoped>
.cesium-compass {
  position: absolute;
  top: 100px;
  right: 50px;
  width: 80px;
  height: 80px;
  border-radius: 50%;
  background: rgba(255,255,255,0.9);
  box-shadow: 0 2px 6px rgba(0,0,0,0.25);
  display:flex;
  align-items:center;
  justify-content:center;
  cursor: grab;
  z-index: 9999;
  font-family: sans-serif;
}
.cesium-compass:active { cursor: grabbing; }
 
/* 指针 */
.needle {
  width: 2px;
  height: 30px;
  background: red;
  transform-origin: 50% 80%;
  position: absolute;
  top: 15px;
  transition: transform 0.05s linear;
  border-radius: 1px;
}
 
/* 方向文字 */
.dir-label {
  position: absolute;
  font-weight: bold;
  font-size: 12px;
  color: black;
  user-select: none;
}
.dir-n { top: 4px; left: 50%; transform: translateX(-50%); }
.dir-e { right: 4px; top: 50%; transform: translateY(-50%); }
.dir-s { bottom: 4px; left: 50%; transform: translateX(-50%); }
.dir-w { left: 4px; top: 50%; transform: translateY(-50%); }
 
/* 当前角度文字 */
.heading-label {
  position: absolute;
  bottom: 4px;
  left: 50%;
  transform: translateX(-50%);
  font-size: 12px;
  font-weight: bold;
  color: black;
}
</style>