5

I am trying to program my app so my ImageView of an arrow will point at a given android.Location. Right now it doesn't point in the right direction. It is pretty off for some reason. I think it is because I'm not taking into account what direction I am facing correctly.

Here is what I am doing currently:

float angle = GetAngle(myLocation);
RotateAnimation anim = new RotateAnimation(0.0f, angle, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
anim.setInterpolator(new LinearInterpolator());
anim.setRepeatCount(0);
anim.setDuration(2);
anim.setFillAfter(true);
arrow.startAnimation(anim);

Here is the GetAngle() function:

float GetAngle(Location myLocation)
{
    float angle = (float) Math.toDegrees(Math.atan2(theirLocation.getLatitude() - myLocation.getLatitude(), theirLocation.getLongitude() - myLocation.getLongitude()));
    if (angle < 0)
        angle += 360;
    return angle;
}

Is there a better way to do this than how I am? I have no idea how to get the ImageView to face toward the coordinates that I am given from a location.

sabo
  • 911
  • 13
  • 37
  • 1
    Read http://stackoverflow.com/questions/4308262/calculate-compass-bearing-heading-to-location-in-android. This is the answer you are looking for. – Jean-Baptiste Yunès Jul 22 '16 at 15:31

2 Answers2

1

Firstly, I am not sure why you want an image view to rotate to an angle. But if you think you can replace the imageview with a marker then do so.

There is a lot of advantages of using marker in this case

  1. Easy to get and set rotation

  2. Your rotation will be very smooth

Secondly, use this function to get the angle

toAngle = startingLocation.bearingTo(endingLocation);

  • Doesn't a marker just show a pinpoint location? I am wanting an arrow to point in a direction that something is. – sabo Jul 18 '16 at 22:17
  • Set the marker's icon to any image your want. In this case your 'arrow'. Marker has a lot of advantages over any view in terms of direction and rotation – Jaswanth Manigundan Jul 18 '16 at 23:43
  • Do I need to be using a google map? Because I am not. I am just getting latitude and longitude from android.Location. – sabo Jul 19 '16 at 00:42
  • Gotcha. Did you try the bearingTo function which I have mentioned in my answer ? – Jaswanth Manigundan Jul 19 '16 at 04:38
  • Well it is giving me an angle for sure. It is hard to tell if it is correct since I only have one device that I can test this on. I'll have to test with preset coordinates and see if it points in the right direction based on that new angle calculation. – sabo Jul 20 '16 at 01:51
  • I am testing it out now with a preset location to compare with mine. The angle changes a tiny bit when I face different directions but it is still facing the wrong way. – sabo Jul 20 '16 at 02:19
  • Any idea with that? – sabo Jul 20 '16 at 22:58
  • Sorry for the delay. Basically the arrow points downward at all times, like the point of interest is behind me even if it is not actually behind me. – sabo Jul 27 '16 at 01:55
  • Any ideas on this? I'm not really sure what else I need to figure out to make sure it is facing the right way. – sabo Aug 03 '16 at 00:06
0

Update your code without animation, at first. I had the same problem, so animation may restart or stop on different behavior.

float angle = GetAngle(myLocation);
myView.setRotation(angle);
//Or also check 
myView.setRotationX(angle);
myView.setRotationY(angle);
GensaGames
  • 5,538
  • 4
  • 24
  • 53
  • This wasn't the fix but I do appreciate the setRotation() function rather than using an animation so thank you. – sabo Jul 27 '16 at 01:54
  • @saboehnke I got the same problem with my custom listview - LinkedListView in github. I want to animate view per scrolling and start animation, but it was my bi problem. So i solved issue with just transformation per any action with it. – GensaGames Jul 27 '16 at 06:50