wpf 使用 C# 获取当前位置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32147112/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 13:21:22  来源:igfitidea点击:

Get current location using C#

c#wpfgpstriangulation

提问by Dan Priest

My skill level: Beginner

我的技能水平:初学者

Code: C# (wpf)

代码:C# (wpf)

Hardware: Dell Venue 11 Pro tablet (windows 8.1)

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

I would like to get the current location of my computervia cellular triangulation in coordinates(latitude/longitude). Using the following link as a reference (Getting GPS coordinates on Windows phone 7), I have been attempting to pull the lat/lon coordinates from Windows Location Services. I have verified that Windows Location Services is receiving the coordinates via cell tower triangulation, but every time I run the following code the coordinates are listed as "NaN". Any help here would be greatly appreciated.

我想通过坐标(纬度/经度)中的蜂窝三角测量来获取计算机的当前位置。使用以下链接作为参考(在 Windows 手机 7 上获取 GPS 坐标),我一直在尝试从 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)); 
        }


    }


}

回答by Frode Evensen

If you only need your current position once, and not continous update as you move, you can replace Startwith TryStart:

如果您只需要一次当前位置,而不需要在移动时不断更新,则可以替换StartTryStart

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

Keep in mind that this method returns synchronously, so it is best not to run in on the UI thread.

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