This is not a dupe of Converting from longitude\latitude to Cartesian coordinates.
I want to have functions that will translate back and forth between X, Y, Z coordinates and Latitude and Longitude. The things is the application already has this function to go from latitude/longitude to XYZ (this function is not changeable)...
export function getXYZ(lat, lng, radius) {
radius = radius || 200;
var gamma = ( 90 - lat) * Math.PI / 180;
var theta = (180 - lng) * Math.PI / 180;
var x = radius * Math.sin(gamma) * Math.cos(theta);
var y = radius * Math.cos(gamma);
var z = radius * Math.sin(gamma) * Math.sin(theta);
return {x: x, y: y, z: z};
}
I tried to create a paired function to go back the other way, but there's something not right.
export function getLatLng(vector, radius) {
radius = radius || 200;
var lat, latRads, lng, lngRads;
latRads = Math.acos(vector.y / radius);
lngRads = Math.atan2(vector.z, vector.x);
lat = (Math.PI / 2 - latRads) * 180 / Math.PI;
lng = (Math.PI - lngRads) * 180 / Math.PI;
return [lat, lng];
}
Any ideas?