Skip to content

Instantly share code, notes, and snippets.

@mihdan
Created January 23, 2015 22:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mihdan/19a0ddcd0bb49ecaabc5 to your computer and use it in GitHub Desktop.
Save mihdan/19a0ddcd0bb49ecaabc5 to your computer and use it in GitHub Desktop.
Перевод координат из радиан на JavaScript
// convert radians into latitude
// 90 to -90
function rad2lat(rad) {
// first of all get everthing into the range -2pi to 2pi
rad = rad % (Math.PI*2);
// convert negatives to equivalent positive angle
if (rad < 0)
rad = 2*Math.PI + rad;
// restict to 0 - 180
var rad180 = rad % (Math.PI);
// anything above 90 is subtracted from 180
if (rad180 > Math.PI/2)
rad180 = Math.PI - rad180;
// if it is greater than 180 then make negative
if (rad > Math.PI)
rad = -rad180;
else
rad = rad180;
return(rad/Math.PI*180);
}
// convert radians into longitude
// 180 to -180
function rad2lng(rad) {
// first of all get everthing into the range -2pi to 2pi
rad = rad % (Math.PI*2);
if (rad < 0)
rad = 2*Math.PI + rad;
// convert negatives to equivalent positive angle
var rad360 = rad % (Math.PI*2);
// anything above 90 is subtracted from 360
if (rad360 > Math.PI)
rad360 = Math.PI*2 - rad360;
// if it is greater than 180 then make negative
if (rad > Math.PI)
rad = -rad360;
else
rad = rad360;
return(rad/Math.PI*180);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment