Calculate Distance and Bearing

Tutorials that use the MapActivity. Many using GPS functionality.

Calculate Distance and Bearing

Postby don » Sun Mar 23, 2008 5:12 pm

I am trying to calculate some statistics on two points on the map. First, I used the Haversine Formula to calculate the distance between two points. However, now I want to calculate the bearing (angle) to travel from the first point in order to reach the second. Does anyone have any ideas how to do this?

Any help would be greatly appreciated.
don
Junior Developer
Junior Developer
 
Posts: 18
Joined: Fri Dec 07, 2007 5:17 am

Top

Postby plusminus » Mon Mar 24, 2008 10:04 am

Hello dan,

this is what I am using in my AndNav!-Application. (It works fine):
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.         }
Parsed in 0.032 seconds, using GeSHi 1.0.8.4


Hope I could help you. :)

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

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

Hey guys,

:oops: this is the missing [font=Lucida Console]location2Point[/font] 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


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

Postby owalekar » Wed Mar 31, 2010 6:26 am

Thanks,
Perfect!!
-
Nikhil
owalekar
Once Poster
Once Poster
 
Posts: 1
Joined: Wed Mar 31, 2010 6:24 am
Location: Los Angeles

Re:

Postby tylerdurden45 » Sat Jul 09, 2011 11:11 pm

plusminus wrote:Hey guys,

:oops: this is the missing [font=Lucida Console]location2Point[/font] function:

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


Regards,
plusminus


Hey plusminus,
My problem is that, I have two locations and how can i understand that angle between of them actually main problem is that if user in a car and turns right, or turns left, I have a locationlistener and want to understand that moves of user.
thank you
tylerdurden45
Freshman
Freshman
 
Posts: 4
Joined: Tue Jul 05, 2011 8:50 am

Top

Return to Map Tutorials

Who is online

Users browsing this forum: No registered users and 3 guests