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

时间:2016-02-16 17:49:27

标签: c# windows-phone-8 compass

我正在开发一款具有Qibla功能的伊斯兰应用。 我可以使用经度和纬度从当前位置确定朝向角度。 例如:从开罗开始,朝拜方向将是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)
    {
    }
}

我使用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>

提前致谢,

1 个答案:

答案 0 :(得分:3)

我做了一个类似的应用程序,显示指向选定地标的指南针(在你的情况下是麦加)。我用这段代码来计算轴承:

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>