If you want to tackle it head on, here is some code based on Kai and Borre’s “Linear algebra, Geodesy and GPS”. The routine converts xyz coordinates to lat/lon in radians. (The open source “gps toolkit” also has a version.)
“a” and “f” are constants describing the shape of the earth ellipsoid. For WGS84, they are set to 6378137.0 and 1.0/298.2572235630 respectively.
void XYZToGeod(double a, double f,
double x, double y, double z,
double& phi, double& lambda, double& h)
{
debug(“XYZToGeod a=%.3f f=%.6f x=%.3f y=%.3f z=%.3f\n”, a,f,x,y,z);
// set up some constants
double r = sqrt(xx + yy);
// Iterate. Converges quickly. Do a fixed number of iterations for now.
h = 0;
double sinphi = 0;
for (int i=0; i<10; i++) {
double N = a / sqrt(1 - f*(2-f)sinphisinphi);
phi = atan2(z, r*(1 - f*(2-f)*N/(N+h)));
h = r / cos(phi) - N;
sinphi = sin(phi);
debug(" N=%.3f h=%.3f phi=%.6f\n", N, h, RadToDeg(phi));
}
// calculate longitude
lambda = atan2(y,x);
debug(" lambda=%.3f phi=%.3f h=%.3f\n",
RadToDeg(lambda),RadToDeg(phi),h);
}