极坐标到笛卡尔坐标的函数未输出正确的数据

Polar to Cartesian coordinates function not outputting the correct data

我有一条飞行路径 lat/long 和高度,我需要将其转换为直角坐标 X、Y、Z 以用于 cesium.js。 我 运行 撞墙试图转换它,因为我似乎没有从我的函数中得到正确的结果。

var R = 6371;
function polarToCartesian(latitude, longitude, elevation){
    x = (R+elevation) * math.cos(latitude) * math.cos(longitude);
    y = (R+elevation) * math.cos(latitude) * math.sin(longitude);
    z = (R+elevation) * math.sin(latitude);

    var ar = [x,y,z];
    return  ar;
}

我一定没有正确的极坐标到笛卡尔坐标的公式,或者我没有正确的地球半径。我在某处发现我的半径应该是 6371,但似乎找不到相同的 SO 问题以供参考。

我正在部分检查我的代码是否正确,方法是手动将给定位置的地球半径 + 飞行路径的高度相加,看看这是否等于我的 x、y、z 向量的长度。

例如:x,y,z (3689.2472215653725,3183.2401988117012,13306.90338789763) 当我给我的函数这个

时输出
-93.028,44.6942,7800

纬度、经度、海拔

有人可以指点我找到正确的 js 代码来完成此转换吗?

Yavascript本身没有任何问题。但是,您的方程式不正确。您希望从 Lat/Long/Alt 转换为球形(又名笛卡尔),which was answered here.

因此您可以将上面的内容重写为:

function polarToCartesian(latitude, longitude, elevation){
    const x = math.cos(latitude) * math.cos(longitude) * elevation;
    const y = math.cos(latitude) * math.sin(longitude) * elevation;
    const z = math.sin(latitude) * elevation;

    return  [x, y, z];
}

你应该为此使用 Cesium 的内置函数。参见 Cartesian3.fromDegrees and Cartesian3.fromDegreesArray

例如:

var result = Cesium.Cartesian3.fromDegrees(latitude, longitude, elevation);

请注意,结果将与 Cesium 预期的一样:以米为单位,而不是千米。这也考虑了椭圆体的形状,默认值为 WGS84(地球不是一个完美的球体,正如您的函数所假设的那样)。