Cesium笔记 获取实体位置并转化为经纬度坐标

/**
   * 获取实体的位置 并转换成string
   * @param {\} entity 多边形 矩形 点 圆形 多段线
   * @returns
   */
  getPositionsByEnity(entity) {
    if (entity) {
      if (entity.polygon) {
        return JSON.stringify(
          this.getLngLatByCartesian3(
            entity.polygon.hierarchy.getValue().positions
          )
        );
      } else if (entity.rectangle) {
        let rectangle = entity.rectangle.coordinates.getValue();
        let result = [];
        Cesium.Rectangle.subsample(
          rectangle,
          Cesium.Ellipsoid.WGS84,
          rectangle.height,
          result
        );
        return JSON.stringify(this.getLngLatByCartesian3(result));
      } else if (entity.point) {
        return JSON.stringify(
          this.getLngLatByCartesian3(entity.position._value)
        );
      } else if (entity.ellipse) {
        let res = {
          center: this.getLngLatByCartesian3(entity.position._value),
          radius: entity.ellipse.semiMajorAxis.getValue(),
        };

        return JSON.stringify(res);
      } else if (entity.polyline) {
        return this.getLngLatByCartesian3(entity.polyline.positions.getValue(), 'polyline')
      }
    }
    return undefined;
  },


/**
   * 笛卡尔坐标转换经纬度坐标
   * @param {*} car3_ps
   * @returns
   */
  getLngLatByCartesian3(car3_ps, type) {
    let result = null;
    if (car3_ps instanceof Cesium.Cartesian3) {
      let _cartographic = Cesium.Cartographic.fromCartesian(car3_ps);
      let _lat = Cesium.Math.toDegrees(_cartographic.latitude);
      let _lng = Cesium.Math.toDegrees(_cartographic.longitude);
      let _alt = _cartographic.height;
      if (type == 'polyline') {
        result = { longitude: _lng, latitude: _lat, elevation: _alt }
      } else {
        result = { lng: _lng, lat: _lat, alt: _alt }
      }
      return result;
    } else if (car3_ps instanceof Array) {
      let res = [];
      for (let i = 0; i < car3_ps.length; i++) {
        let _cartographic = Cesium.Cartographic.fromCartesian(car3_ps[i]);
        let _lat = Cesium.Math.toDegrees(_cartographic.latitude);
        let _lng = Cesium.Math.toDegrees(_cartographic.longitude);
        let _alt = _cartographic.height;
        if (type == 'polyline') {
          res.push({ longitude: _lng, latitude: _lat, elevation: _alt })
        } else {
          res.push({ lng: _lng, lat: _lat, alt: _alt })
        }
      }
      return res;
    }
  },

你可能感兴趣的:(cesium,javascript,开发语言,ecmascript)