Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rotating an ImageView to face a given position in latitude and longitude

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.

like image 318
sabo Avatar asked Nov 09 '22 12:11

sabo


1 Answers

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);

like image 155
Jaswanth Manigundan Avatar answered Nov 14 '22 22:11

Jaswanth Manigundan