c# - C# 中的地理定位

标签 c# geolocation windows-phone windows-phone-8.1

我正在尝试开发一个应该类似于游戏的应用程序。用户将在城市中有一些位置,他必须在每个位置上做一些事情。为了跟踪用户的位置,我尝试使用带有以下代码的地理定位:

Geolocator geolocator = new Geolocator();
//geolocator.DesiredAccuracy = Windows.Devices.Geolocation.PositionAccuracy.High;
geolocator.DesiredAccuracyInMeters = 50;
try
{
    Geoposition geoposition = await geolocator.GetGeopositionAsync(TimeSpan.FromMilliseconds(500), TimeSpan.FromSeconds(1));
    textLatitude.Text = "Latitude: " + geoposition.Coordinate.Latitude.ToString("0.0000000000");
    textLongitude.Text = "Longitude: " + geoposition.Coordinate.Longitude.ToString("0.0000000000");
    textAccuracy.Text = "Accuracy: " + geoposition.Coordinate.Accuracy.ToString("0.0000000000");
}

使用以下方式获取坐标我尝试使用以下代码测试设备是否会正确定位我的位置:

if( Math.Abs(geoposition.Coordinate.Latitude - 45.3285) < 0.001 ){
    if (Math.Abs(geoposition.Coordinate.Longitude - 14.4474) < 0.001)
    {
        txt.Text = "KONT";              
    }
}

问题是位置的精度非常小,如果我尝试使用更精确的坐标,它永远不会再次获得相同的坐标,并且使用此代码,精度非常差(甚至 300 米都可能失败)。

有没有人知道如何获得更可靠的位置或其他解决方法?

最佳答案

我认为问题出现了,因为您给 Geolocator 的时间太少,无法使用 Geolocator.GetGeopositionAsync - timeout 进行正确的读取。 :

Geoposition geoposition = await geolocator.GetGeopositionAsync(TimeSpan.FromMilliseconds(500), TimeSpan.FromSeconds(1));

你只给它 1 秒,而获得更准确的位置需要时间。

我的例子:

Geolocator geolocator;
Geoposition geoposition;
public MainPage()
{
   this.InitializeComponent();
   geolocator = new Geolocator();
   geolocator.DesiredAccuracyInMeters = 10;
   geolocator.ReportInterval = 0;

   myButton.Click += async (sender, e) =>
       {
           geoposition = await geolocator.GetGeopositionAsync();
           string latitude = geoposition.Coordinate.Latitude.ToString("0.0000000000");
           string Longitude = geoposition.Coordinate.Longitude.ToString("0.0000000000");
           string Accuracy = geoposition.Coordinate.Accuracy.ToString("0.0000000000");
       };
}

上面的代码在等待大约 20-30 秒后返回一个准确度约为 35 米的位置(在我的例子中),但是。另请注意,准确性取决于可用卫星的数量。

还有来自 MSDN 的一些评论:

  1. 设置Geolocator.ReportInterval到 0:

    Apps that do require real-time data should set ReportInterval to 0, to indicate that no minimum interval is specified. On Windows, when the report interval is 0, the app receives events at the frequency that the most accurate location source sends them. On Windows Phone, the app will receive updates at a rate dependent on the accuracy requested by the app.

  2. 设置Geolocator.DesiredAccuracyInMeters到 10 米:

    ◾If the user is trying to share his position, the app should request an accuracy of about 10 meters.

  3. 尝试在启动 Geolocator 和重新编辑它之间进行处理:

    Consider start-up delay. The first time an app requests location data, there might be a short delay (1-2 seconds) while the location provider starts up. Consider this in the design of your app's UI. For instance, you may want to avoid blocking other tasks pending the completion of the call to GetGeopositionAsync.

关于c# - C# 中的地理定位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24238373/

相关文章:

c# - .NET 与 POS 的串行端口通信

c# - 如何从具有索引的字典对象中获取特定项目?

c# - 如何替换一组 int 值?

c# 简单的 xml 阅读器跳过所有其他元素

database - 存储栅格数据的好方法是什么?

c# - LongListSelector 和 DataTemplateSelector

mysql - MySQL 是否具有基于地理位置的查询的内置功能?

ios - Cordova geolocation.getCurrentPosition 在 iPad 上抛出超时

windows-phone - 如何在 Windows Phone 8.1 中处理高分辨率照片

javascript - 单击 Windows Mobile 中的绝对 DIV 触发单击底层元素