Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am using equations that look like the following to get x, y, and z given latitude, longitude, and altitude.

x = (6378137 + alt) * cos(lat) * cos(lon);
y = (6378137 + alt) * cos(lat) * sin(lon);
z = (6378137 + alt) * sin(lat);      

If I already know x,y,z, I want to solve for lat, lon, and alt.

For the purposes of the solution it's just a problem of the form:


I know x, y, and z. I want to solve for A, B, and C.

Normally this should be easy. I've got 3 unknowns and 3 equations. I'd just solve for one variable and substitute it in the second equation, etc. But I'm having trouble doing this with the trig functions.

For example, if I try to solve them I end up with

C = atan(y/x)
B = acos( x / (A*cos( atan(y/x) )) )
A = z / sin( acos( x / (A*cos( atan(y/x) )) ) )

I don't know how to solve that last one for A.

EDIT: The following works. Thanks!

    double alt = Math.sqrt(x*x + y*y + z*z) - a;
    double lon = Math.atan2(y,x) * MathUtils.num180DivPI;
    double lat = Math.asin(z / (alt+a)) * MathUtils.num180DivPI;
share|cite|improve this question
$A=\sqrt{x^2+y^2+z^2}$... more or less, look up the conversion formulae from spherical to Cartesian coordinates. You will want to use the two-argument arctangent for this application instead of the usual arctangent. – J. M. Aug 29 '11 at 1:51
If this is for a real life app, you really need to take into account that the Earth's radius is not a constant 6378137 meters. – Jyrki Lahtonen Aug 29 '11 at 9:25
up vote 0 down vote accepted

"A" should be the distance of of (x,y,z) from the origin, or $$\sqrt{x^2+y^2+z^2} $$ as J.M. commented.

share|cite|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.