Click here to Skip to main content
14,666,491 members
Rate this:
Please Sign up or sign in to vote.
Hi,

I'd like to ask how can I make the Xamarin Forms Compass to point to a specific degree?

I already have my Compass and I am passing the Value to it as you can see in the below code but I want ti make sure it updates when the user moves.

I am getting the correct degree there but my problem is only the movement of the device because if the user moves the device then the pointer will no change in the compass which give totally wrong direction for it

Kindly help..


Thanks,
Jassim

What I have tried:

private double Mod(double a, double b)
{
    return a - b * Math.Floor(a / b);
}

private void Button_Clicked(object sender, EventArgs e)
{
    GetLocation();

    // current_latitude = 25.2680572;
    // current_longitude = 55.3159565;

    // if (!OrientationSensor.IsMonitoring) OrientationSensor.Start(speed);

    double latt_from_radians = current_latitude * Math.PI / 180;
    double long_from_radians = current_longitude * Math.PI / 180;
    double latt_to_radians = HolyPlaceLatitude * Math.PI / 180;
    double lang_to_radians = HolyPlaceLongitude * Math.PI / 180;

    int the_r = 6371;            // for miles
                                // int r = 6371;            // for kilometers
                                // int r = 20, 903, 520;    // to get the result in feet

    double law_of_cos_dist = Math.Acos((Math.Cos(latt_from_radians) * Math.Cos(latt_to_radians) * Math.Cos((-1 * lang_to_radians) - (-1 * long_from_radians))) + (Math.Sin(latt_from_radians) * Math.Sin(latt_to_radians))) * the_r;

    LabelA.Text = law_of_cos_dist.ToString();

    double dLat = latt_to_radians - latt_from_radians;
    double dLong = lang_to_radians - long_from_radians;

    double the_a = Math.Pow((Math.Sin(dLat / 2)), 2) + Math.Cos(latt_from_radians) * Math.Cos(latt_to_radians) * Math.Pow((Math.Sin(dLong / 2)), 2);

    LabelB.Text = the_a.ToString();

    double the_c = 2 * Math.Atan2(Math.Sqrt(the_a), Math.Sqrt(1 - the_a));
    LabelC.Text = the_c.ToString();

    double haversine_dist = the_c * the_r;

    double bearing = Math.Atan2(Math.Sin(lang_to_radians - long_from_radians) * Math.Cos(latt_to_radians), (Math.Cos(latt_from_radians) * Math.Sin(latt_to_radians)) - (Math.Sin(latt_from_radians) * Math.Cos(latt_to_radians) * Math.Cos(lang_to_radians - long_from_radians)));

    bearing = Mod(bearing, 2 * Math.PI);

    LabelD.Text = bearing.ToString();

    double bearing_degree = bearing * 180 / Math.PI;
    LabelE.Text = bearing_degree.ToString();

    pointer1.Value = bearing_degree;
}
Posted
Updated 19-Aug-19 4:26am
Comments
Richard MacCutchan 19-Aug-19 6:40am
   
You need to have an event handler that captures movement of the compass so you can recalculate the pointer angle.

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

If the code in your "Click handler" works, then make it into a routine that you can (also) call from a TIMER routine that "ticks" at 100ms or longer which "gets the location" and updates the text box(es) in question.

(100ms is the "lag" threshold).
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100