## 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.