I'm having this small code snippet here. What I'm looking for is to get the current GPS walking direction (bearing) of the user and then get the opposite walking direction.
I seem to be missing some crucial information here since the code seems to mostly work when I walk North East and West but fails when I walk South.
Any idea how to fix my snippet?
private static Location lastValidLocation;
private float getOppositeGPSMovementDirection(Location lastLocation){
float bearing;
if (!lastLocation.hasBearing){
//Use bearing of the last Location with a valid bearing.
bearing = lastValidLocation.getBearing();
}else{
lastValidLocation = lastLocation;
bearing = lastLocation.getBearing();
}
//Calculate backwards direction
int direction = (int) (bearing - 180);
direction = direction<0?direction*-1:direction;
return direction;
}
Your math is faulty. If you're at 10 degrees, your opposite is 190. Your math would give it 170. The correct math is:
direction = (bearing + 180) % 360;
Basically, your assumption that you can multiple a negative angle by -1 to get the correct angle is wrong.