准确的Windows Phone 8.1地理定位?

时间:2015-02-17 19:55:01

标签: windows-phone-8 geolocation windows-phone-8.1 compass-geolocation

我正在使用Windows Phone 8.1地理位置。我目前遇到的问题是我的代码只显示了我坐标的第一个数字。示例:如果坐标为" 41.233"该应用仅显示" 41.00" 。 我需要它尽可能准确。如果它很重要,我使用Windows Phone 8.1模拟器来试用应用程序,而不是真正的手机。

我的代码:

public sealed partial class MainPage : Page
{
    bool shouldSend = false;
    DispatcherTimer timer = new DispatcherTimer();

    public MainPage()
    {
        this.InitializeComponent();
        this.NavigationCacheMode = NavigationCacheMode.Required;

    }

    private async Task GetLocation()
    {
        Geolocator geolocator = new Geolocator();
        geolocator.DesiredAccuracy = Windows.Devices.Geolocation.PositionAccuracy.High;

        try
        {
            Geoposition geoposition = await geolocator.GetGeopositionAsync(
                maximumAge: TimeSpan.FromSeconds(1),
                timeout: TimeSpan.FromSeconds(10)
                );

            LatitudeTxt.Text  = geoposition.Coordinate.Latitude.ToString("0.00");
            LongitudeTxt.Text = geoposition.Coordinate.Longitude.ToString("0.00");
            LatLonTxt.Text = LatitudeTxt.Text + ", " + LongitudeTxt.Text;                
            var speed = geoposition.Coordinate.Speed.ToString();
            ProcessingTxt.Visibility = Windows.UI.Xaml.Visibility.Collapsed;


            string result = "";

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(
                "http://proyecto-busways.rhcloud.com/colectivos?p=lta123&l=80&d=moyano&lat=" + LatitudeTxt.Text + "&lon=" + LongitudeTxt.Text + "&v=" + speed + "&Accion=Agregar");
            request.ContinueTimeout = 4000;

            request.Credentials = CredentialCache.DefaultNetworkCredentials;

            using (HttpWebResponse response = (HttpWebResponse)await request.GetResponseAsync())
            {
                if (response.StatusCode == HttpStatusCode.OK)
                {
                    //To obtain response body
                    using (Stream streamResponse = response.GetResponseStream())
                    {
                        using (StreamReader streamRead = new StreamReader(streamResponse, Encoding.UTF8))
                        {
                            result = streamRead.ReadToEnd();
                        }
                    }
                }
            }


        }
        catch (Exception ex)
        {
            ProcessingTxt.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
            if ((uint)ex.HResult == 0x80004004)
            {
                // the application does not have the right capability or the location master switch is off
             }
            //else
            {
                // something else happened acquring the location
            }
        }

    }

    /// <summary>
    /// Invoked when this page is about to be displayed in a Frame.
    /// </summary>
    /// <param name="e">Event data that describes how this page was reached.
    /// This parameter is typically used to configure the page.</param>
    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        // TODO: Prepare page for display here.

        // TODO: If your application contains multiple pages, ensure that you are
        // handling the hardware Back button by registering for the
        // Windows.Phone.UI.Input.HardwareButtons.BackPressed event.
        // If you are using the NavigationHelper provided by some templates,
        // this event is handled for you.
    }

    private async void StartSending_Click(object sender, RoutedEventArgs e)
    {
        await GetLocation();

        timer.Tick += timer_Tick;
        timer.Interval = new TimeSpan(0, 0, 5);
        timer.Start();
        StartSending.IsEnabled = false;
    }

    async void timer_Tick(object sender, object e)
    {
        ProcessingTxt.Visibility = Windows.UI.Xaml.Visibility.Visible;
        await GetLocation();

    }

    private void EndSending_Click(object sender, RoutedEventArgs e)
    {
        timer.Tick -= timer_Tick;
        timer.Stop();
        StartSending.IsEnabled = true;
        EndSending.IsEnabled = false;
    }

    private void GPS_Tapped(object sender, TappedRoutedEventArgs e)
    {
        Frame.Navigate(typeof(ContactPage));
    }
}

感谢您的帮助!

2 个答案:

答案 0 :(得分:0)

你试过Geolocator.DesiredAccuracyInMeters属性吗?

 geolocator.DesiredAccuracyInMeters = 3;

Reference&amp; Sample

答案 1 :(得分:0)

此时LatitudeTxt.Text = geoposition.Coordinate.Latitude.ToString("0.00"); LongitudeTxt.Text = geoposition.Coordinate.Longitude.ToString("0.00");

您表示您有0.00位小数,为了更准确,您应该输入0.000000