如何在Compass Windows Phone 8上获得Qibla方向

本文关键字:Qibla 方向 Phone Compass Windows | 更新日期: 2023-09-27 18:35:04

我正在开发一个具有朝拜功能的伊斯兰应用程序。我可以使用我的经度和纬度从当前位置以度为单位确定朝拜方向。考核:从开罗出发,朝拜方向为137度。如何使指南针传感器在Windows手机导航到这个角度?

编辑:

我正在使用此方法获取传感器航向读数:

public void RunCompass()
{
    try
    {
        if (Compass.IsSupported)
        {
            // If compass sensor is supported create new compass object and attach event handlers
            Compass myCompass = new Compass();
            // This defines how often heading is updated
            myCompass.TimeBetweenUpdates = System.TimeSpan.FromMilliseconds(100); 
            myCompass.Calibrate += new System.EventHandler<CalibrationEventArgs>((s, e) =>
            {
                // This will show the calibration screen
                this.IsCalibrationNeeded = true;
            });
            myCompass.CurrentValueChanged += new System.EventHandler<SensorReadingEventArgs<CompassReading>>((s, e) =>
            {
                // This will update the current heading value. We have to put it in correct direction
                Deployment.Current.Dispatcher.BeginInvoke(() =>
                {
                    CurrentHeading =   e.SensorReading.TrueHeading;
                    if (CurrentHeading >= (RotationAngel - 10) && CurrentHeading <= (RotationAngel + 10))
                    {
                        //Show Kaba
                        KabaVisability = true;
                    }
                    else
                    {
                        KabaVisability = false;
                    }                       
                });
            });
            // Start receiving data from compass sensor
            myCompass.Start();              
        }
    }
    catch (Exception)
    {
    }
}

我使用当前标题作为我的指针的旋转天使。RotaionAgel是朝拜的角度,例如137。

我的 XAML 代码:

<Grid> 
 <Ellipse>
  <Ellipse.Fill>
   <ImageBrush ImageSource="/Assets/qebla_new.png" Stretch="UniformToFill"/>
  </Ellipse.Fill>
  </Ellipse>
  <Border x:Name="head" RenderTransformOrigin="0.5,0.5" Margin="191,0" Padding="0,66,0,162" UseLayoutRounding="False">
   <Border.RenderTransform>
      <RotateTransform Angle="{Binding CurrentHeading,Mode=TwoWay}">     
   </RotateTransform> <!---->
   </Border.RenderTransform>
</Grid>

提前感谢,

如何在Compass Windows Phone 8上获得Qibla方向

我在一个应用程序中做了类似的事情,显示指向选定地标(在您的情况下为麦加)的指南针。我用这段代码来计算方位角:

public static class DistanceCalculator
{
    const double kDegreesToRadians = Math.PI / 180.0;
    const double kRadiansToDegrees = 180.0 / Math.PI;

    public static double Bearing(GeoCoordinate position, GeoCoordinate 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(toLat);
        double x = Math.Cos(fromLat) * Math.Sin(toLat) - Math.Sin(fromLat) * Math.Cos(toLat) * 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));
    }
} 

并用了

var res = DistanceCalculator.Bearing(Position, SelectedPlace.Position);
TargetHeading = (360 - res) % 360;

然后我从指南针中获取当前的真实方向

CurrentHeading = 360 - e.SensorReading.TrueHeading;

并使用差异

 public double HeadingDifference
 {
    get
    {
        return CurrentHeading - TargetHeading;
    }
 }

指向 XAML 中的箭头

<Image Source="/Assets/sn_ico_et_compass_whitepointer.png" x:Name="arrow">
    <Image.RenderTransform>
        <RotateTransform Angle="{Binding HeadingDifference}" CenterX="240" CenterY="240" x:Name="arrowTransform" />
    </Image.RenderTransform>
</Image>