【问题标题】:Get current location using C#使用 C# 获取当前位置
【发布时间】:2015-11-15 19:11:19
【问题描述】:

我的技能水平:初学者

代码: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)); 
        }


    }


}

【问题讨论】:

  • 尝试订阅OnPositionChanged事件,然后尝试获取位置。可能是没有足够的时间来实际获取位置。

标签: c# wpf gps triangulation


【解决方案1】:

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

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

记住这个方法是同步返回的,所以最好不要在UI线程上运行。

【讨论】:

    猜你喜欢
    • 2016-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-15
    相关资源
    最近更新 更多