I have a javascript file that is reading a text file containing cartesian coordinates. I can parse out the X,Y,Z values; however I need to convert these to lat/long in decimal format. These output values will be used for google map markers.
I have been researching and there are definitely lots of guides out there regarding the conversion of lat/long to cartesian, however I need this to work the opposite direction.
Tried something along the lines of:
//convert to lat long dec
var tmp = cartZ/6371;
//convert to radians
var tmpR = tmp * Math.PI / 180;
var lat = Math.asin(tmpR);
latFinal = lat * 180 / Math.PI;
alert (latFinal);
Usually this is pletely wrong... Not sure if I am even on the right track!
If anyone has done this before using javascript that would be great.
I have a javascript file that is reading a text file containing cartesian coordinates. I can parse out the X,Y,Z values; however I need to convert these to lat/long in decimal format. These output values will be used for google map markers.
I have been researching and there are definitely lots of guides out there regarding the conversion of lat/long to cartesian, however I need this to work the opposite direction.
Tried something along the lines of:
//convert to lat long dec
var tmp = cartZ/6371;
//convert to radians
var tmpR = tmp * Math.PI / 180;
var lat = Math.asin(tmpR);
latFinal = lat * 180 / Math.PI;
alert (latFinal);
Usually this is pletely wrong... Not sure if I am even on the right track!
If anyone has done this before using javascript that would be great.
Share Improve this question edited Nov 8, 2012 at 3:55 Joseph Quinsey 9,97210 gold badges57 silver badges80 bronze badges asked Oct 23, 2012 at 20:44 user1769528user1769528 411 silver badge5 bronze badges 1- This gmat.unsw.edu.au/snap/gps/clynch_pdfs/coordcvt.pdf has a straightforward formulation of conversion in both directions. – Igor Commented Oct 23, 2012 at 21:10
3 Answers
Reset to default 2I ended up using the following: javascript routines
Was exactly what I needed. Hopefully this post is helpful to others. Thanks!
You can try using the proj4js library to do the conversion for you. See this question - the projection is different but you should be able to figure it out.
There is this handy library on npmjs that will do it for you. Once you have installed it (using npm and browserify), you can use:
var projector = require('ecef-projector');
var xyz = projector.project(37.8043722, -122.2708026, 0.0);
console.log(xyz);
// output: [ -2694044.4111565403, -4266368.805493665, 3888310.602276871 ]
var gps = projector.unproject(xyz[0], xyz[1], xyz[2]);
console.log(gps);
// output: [ 37.8043722, -122.27080260000001, 0.0 ]
Using this you can project both ways.