极坐标至笛卡尔坐标function不输出正确的数据

我有一个纬度/经度和海拔的飞行路线,我需要将它转换为cartesin X,Y,Z for cesium.js。 我正在跑到墙上试图转换这个,因为我似乎没有从我的function得到正确的结果。

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,但似乎无法find相同的问题作为参考。

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

例如:x,y,z (3689.2472215653725,3183.2401988117012,13306.90338789763)是我输出的时候输出的

 -93.028,44.6942,7800 

纬度,经度,海拔

有人可以指出我find正确的JS代码来完成这个转换?

您应该使用Cesium的内置函数。 请参见Cartesian3.fromDegreesCartesian3.fromDegreesArray

例如:

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

注意结果将如Cesium所期望的那样:以米为单位而不是以公里为单位。 这也考虑了椭球的形状,默认是WGS84 (地球不是一个完美的球体,因为你的function假设)。

Yavascript 本身没有错。 但是,你的方程是不正确的。 你正在寻找从经纬度/长/转换为球面(又名笛卡尔), 这是在这里回答 。

所以你可以重写上面的:

 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]; }