开发者

Angle between two known android geolocations

开发者 https://www.devze.com 2023-03-09 23:28 出处:网络
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 pointing towards a fixed geoloca

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 pointing towards a fixed geolocation. So I am thinking if somehow, I am able to get an angle between these two geolocations then propably i will be able to d开发者_JAVA百科o the same.

Do you guys have some suggestions to do it in a better manner. Any help would be appreciated.


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."

0

精彩评论

暂无评论...
验证码 换一张
取 消