# Android QuestionAngle between 2 points using GPS coordinates

#### JTmartins

##### Active Member
Longtime 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.

B4X:
``````GPS_LocationChanged (Location1 As Location)

pontoA_lat=Location1.Latitude
pontoA_long=Location1.Longitude

Private y As Float
Private x As Float
Private angulo As Float
y=(pontoA_lat-pontoC_lat)
x=Cos(cPI/180*pontoC_lat)*(pontoA_long-pontoC_long)
angulo=ATan2(y,x)
Label5.Text=Round2(angulo*(180/cPI),1)

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

JM

Last edited:

#### Beja

##### Expert
Longtime User
Hi JM,
sorry not so clear to me..
I thought angel between two points is zero.

#### JTmartins

##### Active Member
Longtime 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

#### Informatix

##### Expert
Longtime 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
Angle between 2 points:
B4X:
``````difX = Orig.X - Target.X
difY = Orig.Y - Target.Y
a = Orig.Angle - Atan2(difY, difX)
if a > 0 then
a = cPI - a
else
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:

#### Troberg

##### Well-Known Member
Longtime User
Can't you just use Bearing from the GPS?

#### JTmartins

##### Active Member
Longtime 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.

#### Troberg

##### Well-Known Member
Longtime User
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)

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.

#### marcick

##### Well-Known Member
Longtime User
The added inaccuracy of GPS. Since GPS is, from the beginning, a military system ....
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.

#### JTmartins

##### Active Member
Longtime 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 earth...lol...so 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 !

#### Troberg

##### Well-Known Member
Longtime User
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.

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.

#### Troberg

##### Well-Known Member
Longtime 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 earth...lol...so 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 !

Remember to test if the GPS location is valid. The GPS object has a property for that.

Replies
2
Views
793
Replies
9
Views
5K
Replies
4
Views
2K
Android Question Angle between two points
Replies
12
Views
2K
Android Question Get an angle between 2 lines
Replies
32
Views
5K