Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get direction (compass) with two longitude/latitude points

Tags:

I'm working on a "compass" for a mobile-device. I have the following points:

point 1 (current location): Latitude = 47.2246, Longitude = 8.8257
point 2 (target  location): Latitude = 50.9246, Longitude = 10.2257

Also I have the following information (from my android-phone):

The compass-direction in degree, which bears to the north. 
For example, when I direct my phone to north, I get 0°

How can I create a "compass-like" arrow which shows me the direction to the point?

Is there a mathematic-problem for this?

EDIT: Okay I found a solution, it looks like this:

/**
 * Params: lat1, long1 => Latitude and Longitude of current point
 *         lat2, long2 => Latitude and Longitude of target  point
 *         
 *         headX       => x-Value of built-in phone-compass
 * 
 * Returns the degree of a direction from current point to target point
 *
 */
function getDegrees(lat1, long1, lat2, long2, headX) {
    
    var dLat = toRad(lat2-lat1);
    var dLon = toRad(lon2-lon1);

    lat1 = toRad(lat1);
    lat2 = toRad(lat2);

    var y = Math.sin(dLon) * Math.cos(lat2);
    var x = Math.cos(lat1)*Math.sin(lat2) -
            Math.sin(lat1)*Math.cos(lat2)*Math.cos(dLon);
    var brng = toDeg(Math.atan2(y, x));

    // fix negative degrees
    if(brng<0) {
        brng=360-Math.abs(brng);
    }

    return brng - headX;
}
like image 288
eav Avatar asked Dec 14 '11 10:12

eav


People also ask

How do you find the direction between two coordinates?

To find the directional vector, subtract the coordinates of the initial point from the coordinates of the terminal point.

How do you find the direction using latitude and longitude?

Here is the formula to find the second point, when first point, bearing and distance is known: latitude of second point = la2 = asin(sin la1 * cos Ad + cos la1 * sin Ad * cos θ), and. longitude of second point = lo2 = lo1 + atan2(sin θ * sin Ad * cos la1 , cos Ad – sin la1 * sin la2)

How do you find the direction of a compass?

Determining direction is possible by measuring the angle between the object sighted or the desired direction and the magnetized needle. A compass needle always points to magnetic north, which is different than true north. Magnetic variation is the angle between true north and magnetic north.


2 Answers

O forgot to say I found the answer eventually. The application is to determine compass direction of a transit vehicle and its destination. Essentially, fancy math for acquiring curvature of Earth, finding an angle/compass reading, and then matching that angle with a generic compass value. You could of course just keep the compassReading and apply that as an amount of rotation for your image. Please note this is an averaged determination of the vehicle direction to the end point (bus station) meaning it can't know what the road is doing (so this probably best applies to airplanes or roller derby).

//example obj data containing lat and lng points
//stop location - the radii end point
endpoint.lat = 44.9631;
endpoint.lng = -93.2492;

//bus location from the southeast - the circle center
startpoint.lat = 44.95517;
startpoint.lng = -93.2427;

function vehicleBearing(endpoint, startpoint) {
    endpoint.lat = x1;
    endpoint.lng = y1;
    startpoint.lat = x2;
    startpoint.lng = y2;

    var radians = getAtan2((y1 - y2), (x1 - x2));

    function getAtan2(y, x) {
        return Math.atan2(y, x);
    };

    var compassReading = radians * (180 / Math.PI);

    var coordNames = ["N", "NE", "E", "SE", "S", "SW", "W", "NW", "N"];
    var coordIndex = Math.round(compassReading / 45);
    if (coordIndex < 0) {
        coordIndex = coordIndex + 8
    };

    return coordNames[coordIndex]; // returns the coordinate value
}

ie: vehicleBearing(mybus, busstation) might return "NW" means its travelling northwesterly

like image 104
ericjam Avatar answered Sep 17 '22 19:09

ericjam


I found some useful gps coordinates formula in math here. For this case, here my solution

 private double getDirection(double lat1, double lng1, double lat2, double lng2) {

    double PI = Math.PI;
    double dTeta = Math.log(Math.tan((lat2/2)+(PI/4))/Math.tan((lat1/2)+(PI/4)));
    double dLon = Math.abs(lng1-lng2);
    double teta = Math.atan2(dLon,dTeta);
    double direction = Math.round(Math.toDegrees(teta));
    return direction; //direction in degree

}
like image 38
deya tri Avatar answered Sep 20 '22 19:09

deya tri