## Converting Radians in Degrees Latitude and Longitude

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);

}

Hi Pete,

Just noticed a typo in your intro: “latitudes and longitudes are usually expressed in the range -90 to 90 for longitude and -180 to 180 for latitudes”

Should be -90 to 90 for LATITUDE not LONGITUDE.

Hi, good article, I think you made minor mistake in the first paragraph:

latitudes and longitudes are usually expressed in the range -90 to 90 for longitude and -180 to 180 for latitudes.