Calculates the bearing

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

Calculates the bearing

Postby deep » Wed Apr 02, 2008 5:01 pm

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

Syntax: [ Download ] [ Hide ]
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.032 seconds, using GeSHi 1.0.8.4

Thanks in advance...

pradeep.
deep
Freshman
Freshman
 
Posts: 3
Joined: Wed Apr 02, 2008 4:47 pm

Top

Postby plusminus » Thu Apr 03, 2008 7:27 am

Hello deep,

:oops: I forgot to attach that function :)

Syntax: [ Download ] [ Hide ]
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.031 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:
Syntax: [ Download ] [ Hide ]
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.034 seconds, using GeSHi 1.0.8.4


Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Calculate the bearing

Postby deep » Thu Apr 03, 2008 7:42 am

Hi,
:D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D

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

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


Thank u.

Pradeep.
deep
Freshman
Freshman
 
Posts: 3
Joined: Wed Apr 02, 2008 4:47 pm

Postby deep » Thu Apr 03, 2008 7:47 am

Hello plusminus

thanks for quick reply. i tried with different logic


Syntax: [ Download ] [ Hide ]
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.  
  25.         com.google.common.geom.Point pBefore = new com.google.common.geom.Point();
  26.  
  27.         com.google.common.geom.Point pAfter =  new com.google.common.geom.Point();
  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.039 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.

pradeep.
deep
Freshman
Freshman
 
Posts: 3
Joined: Wed Apr 02, 2008 4:47 pm

Postby plusminus » Thu Apr 03, 2008 6:18 pm

:) no problem

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 17 guests