Religious Compass not working --- Help!

JassimRahmaJassimRahma USMember ✭✭✭✭

Hi,

I am trying to make a religious compass pointing to holy places

But as you can see in the attached it is not working properly

My app (with white background) is pointing to a wrong direction where the other photo shows another published app showing the correct location

Also, how can I make the accuracy up to 90 degree only because it won't be correct if it exceeded 90 degree?

Sample app and the screenshots are attached

Thanks,
Jassim

Answers

  • batmacibatmaci DEMember ✭✭✭✭✭

    I think that you should specify your question in more details. You should say what isn't working. Map isn't working because you tried this code etc. your question is like my app isn't working please fix it for me. Nobody has time for it to debug and program for you.

  • JassimRahmaJassimRahma USMember ✭✭✭✭

    @batmaci I already mentioned the problem it's pointingg ot a wrong direction

    This is what I am getting:

    and this is where it should point:

    Here is the update sample app attached

  • JarvanJarvan Member, Xamarin Team Xamurai
    edited August 13

    1.Try to use Xamarin.Essentials.Compass to monitor the device's magnetic north heading.

    Tutorial:
    https://docs.microsoft.com/en-us/xamarin/essentials/compass?tabs=android

    2.A sample about compass application:
    https://github.com/jamesmontemagno/app-compass

  • JassimRahmaJassimRahma USMember ✭✭✭✭

    @Jarvan said:
    1.Try to use Xamarin.Essentials.Compass to monitor the device's magnetic north heading.

    Tutorial:
    https://docs.microsoft.com/en-us/xamarin/essentials/compass?tabs=android

    2.A sample about compass application:
    https://github.com/jamesmontemagno/app-compass

    but Essentials' Compass does not provide property to set a custom heading point?

  • Amar_BaitAmar_Bait DZMember ✭✭✭✭✭
    edited August 13

    It's just mathematics/trigonometry and nothing related to Xamarin.Forms. You have Makkah GPS coordinates, just find the current GPS position of the user (Essentials.Geolocation), current compass heading (Essentials.Compass) and then you calculate the right Qibla angle and rotate your image accordingly.

  • JassimRahmaJassimRahma USMember ✭✭✭✭

    @Amar_Bait said:
    It's just mathematics/trigonometry and nothing related to Xamarin.Forms. You have Makkah GPS coordinates, just find the current GPS position of the user (Essentials.Geolocation), current compass heading (Essentials.Compass) and then you calculate the right Qibla angle and rotate your image accordingly.

    I tried that but it's going to wrong direction as you can see in the attached sample

  • Amar_BaitAmar_Bait DZMember ✭✭✭✭✭

    Why are you using Accelerometer?

  • JassimRahmaJassimRahma USMember ✭✭✭✭

    because the Essentials Compass doesn't have the option to set custom location and only point heading to current location

  • Amar_BaitAmar_Bait DZMember ✭✭✭✭✭
    edited August 13

    You need to do 3 things:

    1. Build a normal compass

    1. Find Makkah angle relative to North from the current position:

    1. When you find Makkah angle, draw it on the compass.

    Also you don't need Accelerometer, you're not moving, but rotating.

  • JassimRahmaJassimRahma USMember ✭✭✭✭

    @Amar_Bait said:
    You need to do 3 things:

    1. Build a normal compass

    1. Find Makkah angle relative to North from the current position:

    1. When you find Makkah angle, draw it on the compass.

    Also you don't need Accelerometer, you're not moving, but rotating.

    I hate Math :wink:

    I tried this but still not pointing to the correct Qibla (also attached)

    void Compass_ReadingChanged(object sender, CompassChangedEventArgs e)
    {
        var data = e.Reading;
    
        current_compass = data.HeadingMagneticNorth;
    
        var res = Bearing(new Location(current_latitude, current_longitude), new Location(HolyPlaceLatitude, HolyPlaceLongitude));
        double TargetHeading = (360 - res) % 360;
        double CurrentHeading = 360 - data.HeadingMagneticNorth;
        double difference = CurrentHeading - TargetHeading;
    
        Title = Convert.ToString(difference);
    
        pointer1.Value = difference;
    }
    
    
    const double kDegreesToRadians = Math.PI / 180.0;
    const double kRadiansToDegrees = 180.0 / Math.PI;
    
    
    public double Bearing(Location position, Location location)
    {
        double fromLong = position.Longitude * kDegreesToRadians;
        double toLong = location.Longitude * kDegreesToRadians;
        double fromLat = position.Latitude * kDegreesToRadians;
    
        double dlon = toLong - fromLong;
        double y = Math.Sin(dlon) * Math.Cos(HolyPlaceLatitude);
        double x = Math.Cos(fromLat) * Math.Sin(HolyPlaceLatitude) - Math.Sin(fromLat) * Math.Cos(HolyPlaceLatitude) * Math.Cos(dlon);
    
        double direction = Math.Atan2(y, x);
    
        // convert to degrees
        direction = direction * kRadiansToDegrees;
    
        // normalize
        double fraction = modf(direction + 360.0, direction);
        direction += fraction;
    
        if (direction > 360)
        {
            direction -= 360;
        }
    
        return direction;
    }
    
    private static double modf(double orig, double ipart)
    {
        return orig - (Math.Floor(orig));
    }
    
  • Amar_BaitAmar_Bait DZMember ✭✭✭✭✭
    edited August 13

    You're not applying the formula I gave you, you are overcomplicating things (calculating directions, converting to rad/degs, normalizations you don't need...) by pasting code you don't understand from I don't know where. Just create a blank new project, forget everything you did, and do what I showed you, you'll finish in 10 minutes or less. Also you don't need to calculate the Qibla angle each time the Compass turns, as the Qiblah is relative to a position, not your current direction, calculate it only once after you receive the GPS position (or the GPS position is updated). The compass is just to show the user her/his direction relative to the Qibla (and the North)

  • JassimRahmaJassimRahma USMember ✭✭✭✭

    @Amar_Bait said:
    You're not applying the formula I gave you, you are overcomplicating things (calculating directions, converting to rad/degs, normalizations you don't need...) by pasting code you don't understand from I don't know where. Just create a blank new project, forget everything you did, and do what I showed you, you'll finish in 10 minutes or less. Also you don't need to calculate the Qibla angle each time the Compass turns, as the Qiblah is relative to a position, not your current direction, calculate it only once after you receive the GPS position (or the GPS position is updated). The compass is just to show the user her/his direction relative to the Qibla (and the North)

    Thanks Amaar

    I tried attached for the same location (Bakersfield) but getting different figures

    One more question please, where the Essentials' Compass role comes here?

    Sorry, it's first time I try Compass and such Math

    How about the last two steps in the image, I mean (Makkah Angle relative to North from current position) and (East from True North)?

    Thanks in advance

  • JassimRahmaJassimRahma USMember ✭✭✭✭
    edited August 19

    @Amar_Bait said:
    It's just mathematics/trigonometry and nothing related to Xamarin.Forms. You have Makkah GPS coordinates, just find the current GPS position of the user (Essentials.Geolocation), current compass heading (Essentials.Compass) and then you calculate the right Qibla angle and rotate your image accordingly.

    I am getting the correct degree now but my problem is how to adjust the pointer with the compass current heading

    My pointer is accepting double value like this:

    pointer1.Value = bearing_degree;

    because I am getting the correct direction but not when I move the device

Sign In or Register to comment.