javascript JS 中的经纬度到 XYZ 位置 .. 不工作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8981943/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Lat Long to X Y Z position in JS .. not working
提问by samccone
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));
given the above my marker is not translating into the correct position on a 3D sphere ... thoughts?
鉴于上述情况,我的标记没有转换为 3D 球体上的正确位置......想法?
It is relatively close (on the same continent) but that close :\
它相对接近(在同一大陆上)但接近:\
given below ... it should be rendering in at
下面给出......它应该在
lat: 41.7307619 long: -71.276195
纬度:41.7307619长:-71.276195
my globe has a boundRadius: 500px
我的地球有一个boundRadius: 500px
the current result of the function is
该函数的当前结果是
x:-119.7801015013779
x:-119.7801015013779
y:332.8157297895266
y:332.8157297895266
z:353.3927238766871
z:353.3927238766871
回答by Stephen Quan
Your formula differs slightly from the geodetic to ECEF calculation. Refer to the formulas on Dr Math Latitude and Longitude, GPS Conversionand Wikipedia Geodetic to/from ECEF coordinates. This projects the latitude, longitude to a flattened sphere (i.e. the real Earth is not perfectly spherical).
您的公式与大地测量到 ECEF 计算略有不同。请参阅数学博士纬度和经度、GPS 转换和维基百科大地测量与 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;
In your scenario, because it seems you're gunning for a perfect sphere, you will need to put f = 0.0 and rad = 500.0 instead. This will cause C and S to become 1.0, so, the simplified version of the formula reduces to:
在您的场景中,因为您似乎正在寻找一个完美的球体,所以您需要将 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;
N.B. I have not validated the syntax of the Java code examples.
注意我没有验证 Java 代码示例的语法。