Angle between two known android geolocations

后端 未结 4 542
鱼传尺愫
鱼传尺愫 2021-01-14 09:52

I want to find an angle between two known geolocations.Basically what I want is, I want to direct an arrow whose tail point is at my current location and arrow head is point

4条回答
  •  甜味超标
    2021-01-14 10:22

    I wanted to achieve the same. using this referance to calculate Angle as follows:

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

    and then rotate ImageView to this angle

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

提交回复
热议问题