JS中的经纬度转X Y Z坐标位置无法正常工作

14
var phi   = (90-lat)*(Math.PI/180);
var theta = (lng+180)*(Math.PI/180);

marker_mesh.position.x = ((rad) * Math.sin(phi)*Math.cos(theta));
marker_mesh.position.z = ((rad) * Math.sin(phi)*Math.sin(theta));
marker_mesh.position.y = ((rad) * Math.cos(phi));

根据上述情况,我的标记在3D球体上的位置没有正确地转换...有什么想法吗?

它相对接近(在同一个大陆上),但就是这样的:

鉴于以下情况...它应该呈现在

纬度: 41.7307619 经度: -71.276195

我的地球仪有一个 boundRadius: 500px

函数的当前结果为

x: -119.7801015013779

y: 332.8157297895266

z: 353.3927238766871

输入图像描述


如果有人对完整的坐标转换感兴趣,请查看此链接:https://dev59.com/K3bZa4cB1Zd3GeqPJcGQ#54039559。 - Farid Alijani
1个回答

23

您的公式与大地测量到ECEF计算略有不同。请参阅Dr Math上的公式 Latitude and Longitude, GPS Conversion以及维基百科上的大地测量/ECEF坐标转换计算。这将在一个扁平化的球体上投影纬度和经度(即实际的地球并非完全球形)。

var cosLat = Math.cos(lat * Math.PI / 180.0);
var sinLat = Math.sin(lat * Math.PI / 180.0);
var cosLon = Math.cos(lon * Math.PI / 180.0);
var sinLon = Math.sin(lon * Math.PI / 180.0);
var rad = 6378137.0;
var f = 1.0 / 298.257224;
var C = 1.0 / Math.sqrt(cosLat * cosLat + (1 - f) * (1 - f) * sinLat * sinLat);
var S = (1.0 - f) * (1.0 - f) * C;
var h = 0.0;
marker_mesh.position.x = (rad * C + h) * cosLat * cosLon;
marker_mesh.position.y = (rad * C + h) * cosLat * sinLon;
marker_mesh.position.z = (rad * S + h) * sinLat;
在您的情况下,因为似乎您想要一个完美的球体,所以您需要将f = 0.0和rad = 500.0。这将导致C和S变为1.0,因此,该公式的简化版本如下:
var cosLat = Math.cos(lat * Math.PI / 180.0);
var sinLat = Math.sin(lat * Math.PI / 180.0);
var cosLon = Math.cos(lon * Math.PI / 180.0);
var sinLon = Math.sin(lon * Math.PI / 180.0);
var rad = 500.0;
marker_mesh.position.x = rad * cosLat * cosLon;
marker_mesh.position.y = rad * cosLat * sinLon;
marker_mesh.position.z = rad * sinLat;

注意:我没有验证Java代码示例的语法。


@samccone,根据您的要求,为您提供了两个Java代码片段。底部更简单的那个可能更符合您的喜好。 - Stephen Quan
感谢您的工作……我认为问题更大,与此相关……https://dev59.com/snE85IYBdhLWcg3wtVxT… - samccone
没错,在这种情况下,您可能指的是Web Mercator投影。http://www.gal-systems.com/2011/07/convert-coordinates-between-web.html - Stephen Quan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接