I have this code:
using System.Reflection.PortableExecutable;
namespace MauiApp1;
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
}
double kaliningradlongitude = 54.70934095352097;
double kaliningradlatitude = 20.469516749819515;
double tokyolongitude = 35.76242245296096;
double tokyolatitude = 139.261582628989;
private void ToggleCompass()
{
if (Compass.Default.IsSupported)
{
if (!Compass.Default.IsMonitoring)
{
// Turn on compass
Compass.Default.ReadingChanged += Compass_ReadingChanged;
Compass.Default.Start(SensorSpeed.UI);
}
else
{
// Turn off compass
Compass.Default.Stop();
Compass.Default.ReadingChanged -= Compass_ReadingChanged;
}
}
}
private void Compass_ReadingChanged(object sender, CompassChangedEventArgs e)
{
// Update UI Label with compass state
this.text.Text = $"{e.Reading} + {this.arrow.Rotation}";
this.arrow.RotateTo(e.Reading.HeadingMagneticNorth - RadianToDegree(Bearing(tokyolongitude, tokyolatitude, kaliningradlongitude, kaliningradlatitude)));
}
public static double Bearing(double pt1long, double pt1lat, double pt2long, double pt2lat)
{
double x = Math.Cos(DegreesToRadians(pt1lat)) * Math.Sin(DegreesToRadians(pt2lat)) - Math.Sin(DegreesToRadians(pt1lat)) * Math.Cos(DegreesToRadians(pt2lat)) * Math.Cos(DegreesToRadians(pt2long - pt1long));
double y = Math.Sin(DegreesToRadians(pt2long - pt1long)) * Math.Cos(DegreesToRadians(pt2lat));
// Math.Atan2 can return negative value, 0 <= output value < 2*PI expected
return (Math.Atan2(y, x) + Math.PI * 2) % (Math.PI * 2);
}
private double RadianToDegree(double angle)
{
return angle * (180.0 / Math.PI);
}
public static double DegreesToRadians(double angle)
{
return angle * Math.PI / 180.0d;
}
private void Button_Clicked(object sender, EventArgs e)
{
ToggleCompass();
}
}
I have the most western point = Kaliningrad, and the most eastern point = Tokyo. What do I want? I want to make compas's arrow heading to Tokyo (as if I'm in Kaliningrad) depending where I look. Let's imagine that Tokyo is on East and compas's arrow must head to East, even if I look at South or North and etc. Now my compas's arrow is buggy and don't work properly.