Angle between two known android geolocations

六月ゝ 毕业季﹏ 提交于 2019-12-01 08:50:37
Hyperboreus

Refer to this, there are several approaches explained.

How do I calculate the Azimuth (angle to north) between two WGS84 coordinates

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

This is simple trig, really. If they're close enough together, you can just use plane geometry. Take the two locations, figure out the right triangle that has the two locations as the acute angles, and compute.

There's already a link to the basic math up.

One thing -- if the locations are very far apart, you'll want to use spherical trig or the angles will be inaccurate.

Well, there was a link ... http://www.easycalculation.com/trigonometry/triangle-angles.php

I'm not sure that I understand what you want to do, but check this: Android Reference, Location, distanceBetween(double startLatitude, double startLongitude, double endLatitude, double endLongitude, float[] results)

results parameter should be array of floats - at least 2 elements. Then in results[0] you will find distance and in results1 bearing between this two points. What is important "Distance and bearing are defined using the WGS84 ellipsoid."

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!