Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Geolocation in C#

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?

like image 664
user2700896 Avatar asked Jun 16 '14 07:06

user2700896


1 Answers

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.

like image 192
Romasz Avatar answered Sep 18 '22 16:09

Romasz