The task of converting radians to degrees is reasonably trivial. However, latitudes and longitudes are usually expressed in the range -90 to 90 for longitude and -180 to 180 for latitudes.
This is only a convention – latitudes above 90 have real unique meaning, and they should not create any problems in Google Maps etc. However, when displaying latitude, longitudes as text it is best to use this convention.
Angles Greater than 360
We just need to take of 360ยฐ – for example 390ยฐ is the same as 30ร. We may also have to handle angles such as 1000ยฐ.
Latitude
Everything from 0 to 90ยฐ is as it is. However, 120ยฐ is the same as 60ยฐ and 150ยฐ is the same as 30ยฐ and 180ยฐ is the same as 0ยฐ. Anything above 180ยฐ is expressed as a negative angle i.e. 190ยฐ is -10ยฐ etc.
Coordinates 0 to -90ยฐ are as they are. However, -120ยฐ is the same as -60ยฐ and -150ยฐ is the same as -30ยฐ.
Longitude
Angles are usually expressed in the range -180ยฐ to 180ยฐ. 190ยฐ is the same as -170ยฐ, 210ร is the same as -150ยฐ until 380ยฐ which is the same as 0ยฐ.
The JavaScript Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48 // 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);
}
Leave a Reply