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.
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
Easy to get and set rotation
Your rotation will be very smooth
Secondly, use this function to get the angle
toAngle = startingLocation.bearingTo(endingLocation);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With