Android Question Angle between 2 points using GPS coordinates

Discussion in 'Android Questions' started by JTmartins, Mar 15, 2015.

  1. JTmartins

    JTmartins Active Member Licensed User

    Hi all,

    I'm facing a problem, wich I do not understand.

    I need to find the angle between 2 points . The ideia is to detect turns above a certain angle during motion.

    I have this little piece of code in GPS_LocationChanged (there is more code there, but is not relevant to this problem).

    PointC_lat & pointC_long hold the coordinates of the previous detected point.

    pontoA_long, PontoA_lat, PontoC_long and PontoC_lat are all global variables.

    The distance between the points is small, between 30 to 300 meters, depending on speed.

    GPS_LocationChanged (Location1 As Location)


    Private y As Float
    Private x As Float
    Private angulo As Float

    end sub
    First of all the value never stays put. Even with the device on a stable surface the value keeps changing in almost a random way. (it can be -146, 10,64,-110, or whatever)

    Second it does not correlate in any form with the angle of movement if I actualy start moving around to test,

    I also tryed with bearing to...but no luck in getting the angle.

    Anyone there to help me out understanding this behaviour ?

    Many thanks

    Last edited: Mar 15, 2015
  2. Beja

    Beja Expert Licensed User

    Hi JM,
    sorry not so clear to me..
    I thought angel between two points is zero.
  3. JTmartins

    JTmartins Active Member Licensed User

    Obvious, if we are talking in the cartesian axis. I mean 2 GPS points (gps coordinates)

    Anyway I think I solved it. I will post solution here, as soon as I can test properly outside and will post the solution
  4. Informatix

    Informatix Expert Licensed User

    Angle between 2 points:
    difX = Orig.X - Target.X
    difY = Orig.Y - Target.Y
    a = Orig.Angle - 
    Atan2(difY, difX)
    if a > 0 then
                a = 
    cPI - a
                a = -
    cPI - a
    end if
    Angle > 0 : target on the left
    Angle < 0 : target on the right
    Abs(Angle) < PI/2 : target ahead
    Abs(Angle) > PI/2 : target behind
    Last edited: Mar 16, 2015
    thedesolatesoul likes this.
  5. Troberg

    Troberg Well-Known Member Licensed User

    Can't you just use Bearing from the GPS?
  6. JTmartins

    JTmartins Active Member Licensed User

    I actualy did it using bearing, and it seems quite accpetable.

    Got bearing from point A and bearing from point B then subtracted the values.

    I will do some proper tests to check if it suits what I want to do, but it seems it will work.
  7. Troberg

    Troberg Well-Known Member Licensed User

    That's because of two reasons:

    1. The inherent accuracy of GPS. You never get the perfect, true value. The measurement is always a bit off. Think of it as flinging darts at a target. They won't hit dead center, they will be spread around the center. This variation is detected as a tiny movement.

    2. The added inaccuracy of GPS. Since GPS is, from the beginning, a military system, the US wanted to make sure the opponent wouldn't be able to get as much use of it as they would. To facilitate this, the signal sent by the satellites varies a bit from the true value, according to a secret pattern only they know. So, they get the full accuracy, but others don't. Of course, this was pretty easy to circumvent by having a high quality stationary GPS reciever in a know position, and use the deviation it saw as correction for other devices, so if I remember correctly, that coding has been turned off.
  8. marcick

    marcick Well-Known Member Licensed User

    The Selective Availability you ar ereferring to, has been removed since year 2000. There is actually no error introduced in the GPS signal and we, civils, with proper hardware, can obtain the same precision of military systems.
    The main trouble here is point 1 you mention. I think it is better to calculate the angle with a couple of coordinates, because (specially if you are in static conditions, not moving) the bearing indication is not stable.
  9. JTmartins

    JTmartins Active Member Licensed User

    The reason of the value never stayed put, had something to do with the crappy device I was using for tests.

    In another device the behaviour is correct.

    I was actually playing with this device's GPS, and ocasionaly it throws coordinates to the other side of the you can be in London, and suddenly you apear somewhere in a Tanzanian Jungle. So if some one digs on KMLs I have stored in this device while testing it, will believe that I master the secrets of teletransportation !
  10. Troberg

    Troberg Well-Known Member Licensed User

    When you are stationary, you get no bearing. On the other hand, the bearing you get from coordinates when stationary is useless anyway, so either way, you're without data.
  11. Troberg

    Troberg Well-Known Member Licensed User

    Remember to test if the GPS location is valid. The GPS object has a property for that.
    Reviewnow likes this.
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice