0

I have a problem with ImageView Rotation. I have IMageView with arrow (simple png image) i want to rotate that image to point destination location from my current location. I have tried a many possibilities but in every time angle is bad (use lat, long from google maps by clicking around target)

for example i have used code found somewhere on this forum:

private double angleFromCoordinate(double lat1, double long1, double lat2, double long2) {

    double dLon = (long2 - long1);

    double y = Math.sin(dLon) * Math.cos(lat2);
    double x = Math.cos(lat1) * Math.sin(lat2) - Math.sin(lat1)
            * Math.cos(lat2) * Math.cos(dLon);

    double brng = Math.atan2(y, x);

    brng = Math.toDegrees(brng);
    brng = (brng + 360) % 360;
    brng = 360 - brng;

    return brng;
}

private void rotateImage(ImageView imageView, double angle) {

    Matrix matrix = new Matrix();
    imageView.setScaleType(ScaleType.MATRIX); // required
    matrix.postRotate((float) angle, imageView.getDrawable().getBounds()
            .width() / 2, imageView.getDrawable().getBounds().height() / 2);
    imageView.setImageMatrix(matrix);
}

and the same , rotation angle is invalid...

is it possible?

Thanks.

MattiahIT
  • 87
  • 9

2 Answers2

0

You could calculate it with pythagoras...

First find out in which section the coordinate is and then use the Law of cosines to calculate the angle, and add the right amount of degrees to the angle for the appropriate section.

a bit dirty, but it works.

enter image description here

Daan Olislagers
  • 3,253
  • 2
  • 17
  • 35
0

Ok i have an answer, I have used Sensors to get AZIMUTH and to calculate angle this: getting direction

It is important to put flag checking that aizmuth have changed before you rotate an image otherwise it will be rotating in many different directions like crazy ;p

Thanks

Community
  • 1
  • 1
MattiahIT
  • 87
  • 9