GuLiMmo
2024-03-01 d87242f609a6ebaba6f2b45b2c0b41f961e92d4b
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
import * as Cesium from 'cesium'
 
export function getLngLatDistance (
  lat1: number,
  lng1: number,
  lat2: number,
  lng2: number,
) {
  const radLat1 = (lat1 * Math.PI) / 180.0
  const radLat2 = (lat2 * Math.PI) / 180.0
  const a = radLat1 - radLat2
  const b = (lng1 * Math.PI) / 180.0 - (lng2 * Math.PI) / 180.0
  let s =
    2 *
    Math.asin(
      Math.sqrt(
        Math.pow(Math.sin(a / 2), 2) +
          Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2),
      ),
    )
  s = s * 6378.137 // EARTH_RADIUS;
  s = Math.round(s * 10000) / 10000
  return s * 1000
}
 
// 获取限polyline长度
export function getPolylineLength (entity: any) {
  let length = 0
 
  // 获取Polyline的所有顶点位置
  const positions = entity.polyline.positions.getValue()
 
  for (let i = 0; i < positions.length - 1; ++i) {
    const startPosition = positions[i]
    const endPosition = positions[i + 1]
 
    // 使用Cesium提供的distanceBetween函数计算两个顶点之间的距离
    const distance = Cesium.Cartesian3.distance(startPosition, endPosition)
 
    // 将每个顶点之间的距离相加得到总长度
    length += distance
  }
 
  return length
}