c# - 使用C#获取当前位置

标签 c# wpf gps triangulation

我的技能水平:初学者

代码:C# (wpf)

硬件:Dell Venue 11 Pro 平板电脑(Windows 8.1)

我想通过坐标(纬度/经度)中的蜂窝三角测量获取我的计算机的当前位置。使用以下链接作为引用(Getting GPS coordinates on Windows phone 7),我一直在尝试从 Windows 定位服务中提取纬度/经度坐标。我已验证 Windows 位置服务正在通过蜂窝塔三角测量接收坐标,但每次运行以下代码时,坐标都列为“NaN”。此处的任何帮助将不胜感激。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Device.Location;

namespace GPS
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {

        private GeoCoordinateWatcher watcher = new GeoCoordinateWatcher(GeoPositionAccuracy.Default);


        public MainWindow()
        {
            InitializeComponent();

            watcher.Start();

            LocationMessage(); 
        }



        private void LocationMessage()
        {

            var whereat = watcher.Position.Location;

            var Lat = whereat.Latitude.ToString("0.000000");
            var Lon = whereat.Longitude.ToString("0.000000");


            //optional parameters for future use
            whereat.Altitude.ToString();
            whereat.HorizontalAccuracy.ToString();
            whereat.VerticalAccuracy.ToString();
            whereat.Course.ToString();
            whereat.Speed.ToString();

            MessageBox.Show(string.Format("Lat: {0}\nLon: {1}",Lat,Lon)); 
        }


    }


}

最佳答案

如果您只需要当前位置一次,而不是在移动时不断更新,您可以将 Start 替换为 TryStart:

if (watcher.TryStart(false, TimeSpan.FromSeconds(3)))
{
    LocationMessage();
}
else
{
    // Position not found after 3 seconds...
}

请记住,此方法是同步返回的,因此最好不要在 UI 线程中运行。

关于c# - 使用C#获取当前位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32147112/

相关文章:

c# - Caliburn.Micro-OnDeactivate不触发

c# - 从 .NET 确定 C# 和 C++ 的 SLOC 和复杂性

c# - WPF UI 仅在调试时崩溃

android - PWA 已经支持 GPS 了吗?

Iphone 谷歌地图 GPS 图标

c# - 将 char 数组/字符串转换为 bool 数组

c# - 有没有办法使用 MSTest 运行一组参数的所有测试?

c# - 如何在 Prism 中向 IoC 注册类

c# - 从 ViewModel 打开/关闭 View

c - 16F819 原始类型问题中的 GPS SD 卡数据记录器