# 360 degrees heading calculation between two points

I'm using Leaflet + Leaflet's rotate-branch to rotate my map application.

``````map.setBearing(angle_deg);
``````

is the function to do so. `angle_deg` has to be between 0 and 359 to simulate any desired angle. I want the map to follow and rotate in relation to the users current position and walking direction.

In order to avoid math, I wanted to use Leaflet.GeometryUtil to do the calculations for me, but only was able to find the functions `bearing()` and `computeAngle()`, which yield the same result: horizontal heading between two coordinates (0 up to 180 clockwise and back to 0 again in axial symmetry with a minus sign).

How can I transform this into a full 360 degree value?

To further illustrate:

``````      0                   360/0
-90   90       to     270   90
+/-180                 180
``````

Could I just add 360 to all negative values and be fine?

In order to get a 360° value, I just had to substract the horizontal angle from 360.

``````angle_degree = 360-angle
``````

Now the map is rotating in the direction the user is heading to.

• Are you able to mark your answer as the solution? :) Oct 2, 2018 at 12:34

The code should look like this:

``````let heading = (360 + angle) % 360;
``````

resulting in:

``````(360 + 45)    % 360 // => 405 % 360 = 45
(360 + (-160)) % 360 // => 200 % 360 = 200
``````