开发者

Constructing a triangle based on Coordinates on a map

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,


for long distance: http://www.dtcenter.org/met/users/docs/write_ups/gc_simple.pdf but for short distance You can try simple 2d math to simulate "classic" compass using: http://en.wikipedia.org/wiki/Compass#Using_a_compass. For example you can get pixel coordinates from points A and B and find angle between line connecting those points and vertical line.

also You probably should consider magnetic declination: http://www.ngdc.noaa.gov/geomagmodels/Declination.jsp

//edit:

I was trying to give intuitive solution. However calculating screen coordinates from long/lat wouldn't be easy so You probably should use formulas provided in links.


Maybe its because I don't know javascript, but don't you have to do something like

currlat = Math.toRadians(currlat);

to actually change the currlat value to be radians.


Problem was no matter what I piped in java would output in Radians, Trick was to change everything to Radians and then output came in radians, convert to degrees.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜