我想編程我的應用程序,以便我的ImageView的箭頭將指向給定的android.Location。現在它並沒有指向正確的方向。由於某種原因,它很漂亮。我認爲這是因爲我沒有考慮到我正確面對的方向。旋轉ImageView以面對給定位置的經緯度
下面是我在做什麼目前:
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);
這裏是GetAngle()函數:
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;
}
是否有更好的方法來做到這一點比我怎麼了?我不知道如何讓ImageView面向我從一個位置給出的座標。
讀http://stackoverflow.com/questions/4308262/calculate-compass-bearing-heading-to-location-in-android。這是你正在尋找的答案。 –