我正在开发具有Qibla功能的伊斯兰应用程序。
我可以使用我的经度和纬度从当前位置以度为单位确定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>

关于c# - 如何在Compass Windows Phone 8上获取Qibla Direction?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35439622/

10-10 03:01