C#中的地理位置 [英] Geolocation in C#

查看:203
本文介绍了C#中的地理位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图开发一个应该像游戏一样的应用程序。用户在城市中会有一些位置,他将不得不在每个位置上做一些事情。为了跟踪用户的位置,我尝试使用以下代码的地理定位:

  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 =精确度:+ 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地理位置;
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);
字符串Accuracy = geoposition.Coordinate.Accuracy.ToString(0.0000000000);
};

$ / code>

上面的代码修改了一个位置(在我的情况下),精度为〜35米, BUT 等待大约20-30秒后。另请注意,准确度取决于可用卫星的数量。



还有一些来自 MSDN


  1. set < a href =http://msdn.microsoft.com/en-us/library/windows/apps/xaml/windows.devices.geolocation.geolocator.reportinterval.aspx =noreferrer> Geolocator.ReportInterval 到0:


    确实需要实时数据的应用程序应将ReportInterval设置为0,以指示未指定最小间隔。在Windows上,当报告间隔为0时,应用程序会以最准确的位置来源发送它们的频率接收事件。在Windows Phone上,应用程序会根据应用程序所要求的准确性接收更新。



  2. set Geolocator.DesiredAccuracyInMeters 到10米:


    ◾如果用户试图分享他的位置,应用程序应该要求约10米的精度。



  3. 尝试在启动Geolocator和注册它之间进行协商:


    考虑启动延迟。应用程序第一次请求位置数据时,位置提供程序启动时可能会有短暂的延迟(1-2秒)。在应用程序的用户界面设计中考虑这一点。例如,您可能希望避免阻止其他任务,等待完成对GetGeopositionAsync的调用。




I'm trying to develop an application that should be something like a game. The user would have some locations in a city and he would have to do something on each location. In order to track the position of the user, I have tried using geolocation with the following code:

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");
}

Using the following way to get the coordinates I tried to test if the device will locate my position correctly with the following code:

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

The problem is that the accuracy of the location is really small, if I try using more precise coordinates it would never get the same coordinates again, and with this code the accuracy is really bad (it can fail even 300 meters).

Has anyone an idea how to get a more reliable location, or another way to fix that?

解决方案

I think that the problem occurs, because you give too little time for Geolocator to make a proper readout with Geolocator.GetGeopositionAsync - timeout:

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

You give it only 1 second, while getting more accurate position takes time.

My example:

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");
       };
}

The code above returs a position (in my case) with accuracy of ~35 meters, BUT after waiting about 20-30 seconds. Note also that accuracy depends on numer of available sattelites.

Also some remarks from MSDN:

  1. set Geolocator.ReportInterval to 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. set Geolocator.DesiredAccuracyInMeters to 10 meters:

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

  3. try to dealy between starting Geolocator and reding it:

    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#中的地理位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆