2

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 completely wrong... Not sure if I am even on the right track!

If anyone has done this before using javascript that would be great.

Joseph Quinsey
  • 9,553
  • 10
  • 54
  • 77
user1769528
  • 41
  • 1
  • 5
  • This http://www.gmat.unsw.edu.au/snap/gps/clynch_pdfs/coordcvt.pdf has a straightforward formulation of conversion in both directions. – Igor Oct 23 '12 at 21:10

3 Answers3

2

I ended up using the following: javascript routines

Was exactly what I needed. Hopefully this post is helpful to others. Thanks!

user1769528
  • 41
  • 1
  • 5
1

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.

Community
  • 1
  • 1
Graham
  • 6,484
  • 2
  • 35
  • 39
1

There is this handy library on npmjs.org 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.

psiphi75
  • 1,985
  • 1
  • 24
  • 36