如何在 Compass Windows Phone 8 处获取 Qibla 方向?

How to Get Qibla Direction at Compass Windows Phone 8?

我正在开发一款具有朝拜功能的伊斯兰应用程序。 我可以使用我的经度和纬度从我当前位置确定以度为单位的 Qibla 方向。 例如:从开罗,朝拜方向为 137 度。 如何让 windows phone 中的罗盘传感器导航到这个角度?

编辑:

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

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)
    {
    }
}

我使用 CurrentHeading 作为指针的旋转角度。 RotaionAgel 是 Qibla 的角度,例如 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>

提前致谢,

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

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>