C#中的地理位置

前端之家收集整理的这篇文章主要介绍了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 = "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");
       };
}

上面的代码重新定位了一个位置(在我的情况下),精度为〜35米,但等待了大约20-30秒.还要注意,精度取决于可用的卫星的数量.

还有一些来自MSDN评论

>将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.

>设置Geolocator.DesiredAccuracyInMeters到10米:

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

>尝试在启动Geolocator并重新启动它之间dealy:

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.

原文链接:https://www.f2er.com/csharp/97322.html

猜你在找的C#相关文章