Android-Java: Constructing a triangle based on Coordinates on a map and your bearing

Posted by Aidan on Stack Overflow See other posts from Stack Overflow or by Aidan
Published on 2010-06-06T19:25:18Z Indexed on 2010/06/10 17:02 UTC
Read the original article Hit count: 971

Hi Guys,

I'm constructing a geolocation based application and I'm trying to figure out a way to make my application realise when a user is facing the direction of the given location (a particular long / lat co-ord). I've got the math figured, I just have the triangle to construct.

//UPDATE

So I've figured out a good bit of this...

Below is a method which takes in a long / lat value and attempts to compute a triangle finding a point 700 meters away and one to its left + right. It'd then use these to construct the triangle. It computes the correct longitude but the latitude ends up somewhere off the coast of east Africa. (I'm in Ireland!).

public void drawtri(double currlng,double currlat, double bearing){

    bearing = (bearing < 0 ? -bearing : bearing);

    System.out.println("RUNNING THE DRAW TRIANGLE METHOD!!!!!");
    System.out.println("CURRENT LNG" + currlng);
    System.out.println("CURRENT LAT" + currlat);
    System.out.println("CURRENT BEARING" + bearing);

    //Find point X(x,y)
    double distance = 0.7; //700 meters.
    double R = 6371.0; //The radius of the earth.
    //Finding X's y value.

    Math.toRadians(currlng);
    Math.toRadians(currlat);
    Math.toRadians(bearing);

    distance = distance/R;
    Global.Alat = Math.asin(Math.sin(currlat)*Math.cos(distance)+ 
            Math.cos(currlat)*Math.sin(distance)*Math.cos(bearing));
    System.out.println("CURRENT ALAT!!: " + Global.Alat);
    //Finding X's x value.
    Global.Alng = currlng + Math.atan2(Math.sin(bearing)*Math.sin(distance)
            *Math.cos(currlat), Math.cos(distance)-Math.sin(currlat)*Math.sin(Global.Alat));
    Math.toDegrees(Global.Alat);
    Math.toDegrees(Global.Alng);


    //Co-ord of Point B(x,y)
    // Note: Lng = X axis, Lat = Y axis.
    Global.Blat = Global.Alat+ 00.007931;
    Global.Blng = Global.Alng;

    //Co-ord of Point C(x,y)
    Global.Clat = Global.Alat  - 00.007931;
    Global.Clng = Global.Alng;

    }

From debugging I've determined the problem lies with the computation of the latitude done here..

 Global.Alat = Math.asin(Math.sin(currlat)*Math.cos(distance)+ 
        Math.cos(currlat)*Math.sin(distance)*Math.cos(bearing));

I have no idea why though and don't know how to fix it. I got the formula from this site..

http://www.movable-type.co.uk/scripts/latlong.html

It appears correct and I've tested multiple things...

I've tried converting to Radians then post computations back to degrees, etc. etc.

Anyone got any ideas how to fix this method so that it will map the triangle ONLY 700 meters in from my current location in the direction that I am facing?

Thanks,

EDIT///

Converting the outcome to radians gives me a lat of 5.6xxxxxxxxxxxxxx .I have a feeling this bug has something to do with conversions but its not THAT simple.

The equation is correct, it just.. outputs wrong..

© Stack Overflow or respective owner

Related posts about android

Related posts about math