## Calculates the bearing

Put your problem here if it does not fit any of the other categories.

### Calculates the bearing

Hi all,
I am trying to calculate angle between two locations, i tried the following code by plusminus but i am not getting correct result. can any one tell me how to convert the location to point (location2Point) in the below code. i am using arrow image, please tell me where should be the head of image(arrow).

Using java Syntax Highlighting
1.      /**
2.
3.       * Calculates the bearing of the two Locations supplied and returns the
4.
5.       * Angle in the following (GPS-likely) manner: <br />
6.
7.       * <code>N:0°, E:90°, S:180°, W:270°</code>
8.
9.       */
10.
11.      public static float calculateBearing(Location before, Location after) {
12.
13.           Point pBefore = location2Point(before);
14.
15.           Point pAfter = location2Point(after);
16.
17.
18.
19.           float res = -(float) (Math.atan2(pAfter.y - pBefore.y, pAfter.x
20.
21.                     - pBefore.x) * 180 / PI) + 90.0f;
22.
23.           Log.d(Constants.DEBUGTAG, "Bearing: " + res);
24.
25.           if (res < 0)
26.
27.                return res + 360.0f;
28.
29.           else
30.
31.                return res;
32.
33.      }
34.
35.
Parsed in 0.015 seconds, using GeSHi 1.0.8.4

deep
Freshman

Posts: 3
Joined: Wed Apr 02, 2008 4:47 pm

Hello deep,

I forgot to attach that function

Using java Syntax Highlighting
1.         /** Converts an <code>android.location.Location</code> to an <code>android.graphics.Point</code>. */
2.
3.         public static Point location2Point(Location aLocation){
4.
5.                 return new Point((int) (aLocation.getLongitude() * 1E6),
6.
7.                                                 (int) (aLocation.getLatitude() * 1E6));
8.
9.         }
Parsed in 0.011 seconds, using GeSHi 1.0.8.4

My arrow points north/up.

And this is the Matrix I use to rotate the image in the overlay:
Using java Syntax Highlighting
1. /* 15.5f, 15.5f ;  to turn around the center of the image. */this.directionRotaterMatrix.setRotate(directionBearing, 15.5f, 15.5f);
Parsed in 0.010 seconds, using GeSHi 1.0.8.4

Regards,
plusminus

| Android Development Community / Tutorials

plusminus

Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

### Calculate the bearing

Hi,

now i am getting correct angle, and everything working fine.

plusminus your tutorials really helped me a lot thanks a lot.

Thank u.

deep
Freshman

Posts: 3
Joined: Wed Apr 02, 2008 4:47 pm

Hello plusminus

thanks for quick reply. i tried with different logic

Using java Syntax Highlighting
1.          /**
2.
3.      * Calculates the bearing of the two Locations supplied and returns the
4.
5.      * Angle in the following (GPS-likely) manner: <br />
6.
7.      * <code>N:0°, E:90°, S:180°, W:270°</code>
8.
9.      */
10.
11.     public static float calculateBearing(MapPoint  before, MapPoint after)
12.
13.     {
14.
15.
16.
17.         //return before.bearingTo(after);
18.
19.         Point p1 = new Point(before.getLatitude(),before.getLongitude());
20.
21.         Point p2 = new Point(after.getLatitude(),after.getLongitude());
22.
23.
24.
26.
28.
29.
30.
31.         pBefore.setLocation(p1.getLatitudeE6(), p1.getLongitudeE6());
32.
33.         pAfter.setLocation(p2.getLatitudeE6(), p2.getLongitudeE6());
34.
35.
36.
37.          float res = -(float) (Math.atan2(pAfter.y - pBefore.y, pAfter.x
38.
39.                    - pBefore.x) * 180 / 3.142) + 90.0f;
40.
41.          if (res < 0)
42.
43.               return res + 360.0f;
44.
45.          else
46.
47.               return res;
48.
49.
50.
51.     }
Parsed in 0.012 seconds, using GeSHi 1.0.8.4

it works fine but yours is small and really nice thank u for you great help.

Thank you.

deep
Freshman

Posts: 3
Joined: Wed Apr 02, 2008 4:47 pm

no problem

Regards,
plusminus

| Android Development Community / Tutorials

plusminus