Azimuthto() - got the formula but the angle is just not precise enough...



  • @function azimuthto() {
    // console.log(Math.ceil(centerScreenCoordinate.distanceTo(currentUserCoordinates)))
    var dx = currentUserCoordinates.longitude-centerScreenCoordinate.longitude;
    var dy = currentUserCoordinates.latitude-centerScreenCoordinate.latitude;
    var azimuth = (Math.PI/2) - Math.atan(dy/dx);
    if (dx < 0) azimuth += Math.PI;
    var mapAngle = azimuth*180/Math.PI;
    return mapAngle;
    }@

    Basically took the formula from here: "StackOverflow":http://stackoverflow.com/questions/642555/how-do-i-calculate-the-azimuth-angle-to-north-between-two-wgs84-coordinates/1050914#1050914

    Modified slighly as I need bearing rather than azimuth (I need the arrow to show the direction from the map center to user location).

    It works, bt problem is the angle is just not precise enough, points to the proper direction but somehow the arrow is not pointing exactly to the center of the second point (user location).



  • I'm guessing that the problem is that you're working out the angle between the points based on latitude and longitude (ie as they are in the world) whereas the points on the screen are being drawn based on the Mercator projection of those points.

    I can't remember if the QML Maps functions that convert between the screen and world coordinates clip results that are outside of the viewport or not. If they don't clip the output you could use those functions to find the screen positions of the points and work out the angle between them.

    If those functions aren't helpful then you'd probably want to just Mercator project the (lat,lon) points and then work out the angle as above. It should be fairly easy to track down some Mercator projection code online (or in the Qt Location code if you're willing to dig).



  • Maybe "QGeoCoordinate::azimuthTo()":http://doc.qt.nokia.com/qtmobility-1.2/qgeocoordinate.html#azimuthTo works. Or check the actual implementation:

    https://qt.gitorious.org/qt-mobility/qt-mobility/blobs/master/src/location/qgeocoordinate.cpp
    @/*!
    Returns the azimuth (or bearing) in degrees from this coordinate to the
    coordinate specified by \a other. Altitude is not used in the calculation.

    The bearing returned is the bearing from the origin to \a other along the
    great-circle between the two coordinates. There is an assumption that the
    Earth is spherical for the purpose of this calculation.
    
    Returns 0 if the type of this coordinate or the type of \a other is
    QGeoCoordinate::InvalidCoordinate.
    \since 1.0
    

    */
    qreal QGeoCoordinate::azimuthTo(const QGeoCoordinate &other) const
    {
    if (type() == QGeoCoordinate::InvalidCoordinate
    || other.type() == QGeoCoordinate::InvalidCoordinate) {
    return 0;
    }

    double dlon = qgeocoordinate_degToRad(other.d->lng - d->lng);
    double lat1Rad = qgeocoordinate_degToRad(d->lat);
    double lat2Rad = qgeocoordinate_degToRad(other.d->lat);
    
    double y = sin(dlon) * cos(lat2Rad);
    double x = cos(lat1Rad) * sin(lat2Rad) - sin(lat1Rad) * cos(lat2Rad) * cos(dlon);
    
    double whole;
    double fraction = modf(qgeocoordinate_radToDeg(atan2(y, x)), &whole);
    return qreal((int(whole + 360) % 360) + fraction);
    

    }@



  • From what I can gather QGeoCoordinate::azimuthTo will do something similar to what he's already trying, and it's not easily reachable from the QML side. It could be worth a shot, but I think the Mercator projection is probably the first thing to try.

    Imagine drawing an arrow from the middle of a square map to a point near the poles - any formula that is used for spherical coordinates isn't going to have anything in it to handle the stretching of the map to fit in the square, especially as you get away from the equator. Bearings are going to get strange pretty quickly in the environment.

    I might be surprised though - although I wouldn't find it that surprising :)



  • Hi both,

    thanks - for pointing out to this Mercator projection code...I think thi sis the catch.
    I have basically two ways now...either I can implement 8 fixed directions suggesting to end user where he is from current Map center location (so not to do a real time arrow moving around, just like in Nokia Maps). Or keep trying to implement the comlex formula. SOmething tells me I will chose the first option for now.


  • Moderators

    [quote author="cmer4" date="1314771770"]Hi both,

    thanks - for pointing out to this Mercator projection code...I think thi sis the catch.
    I have basically two ways now...either I can implement 8 fixed directions suggesting to end user where he is from current Map center location (so not to do a real time arrow moving around, just like in Nokia Maps). Or keep trying to implement the comlex formula. SOmething tells me I will chose the first option for now.[/quote]

    Or you could convert both points to screen coordinates, and then calculate the angle based off of those, rather than the lat/lon.



  • hmm..do you mean through something like Map element? I am nit quite sure I understand how can I convert coordinates to screen position if the coordinate is far off the screen?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.