Retrieving Location on page open

0

I have a page and when it´s going opened, I want to retrieve the GeoLocation of the phone. Here is my code:

public partial class splash : PhoneApplicationPage
{
    public splash()
    {
        InitializeComponent();
    }
    protected override async void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
    {
        StatusTextBlock.Text = "Ermittle Position ...";
        Geolocator geolocator = new Geolocator();
        geolocator.DesiredAccuracyInMeters = 5;
        try
        {
            Geoposition geoposition = await geolocator.GetGeopositionAsync(
                maximumAge: TimeSpan.FromSeconds(10),
                timeout: TimeSpan.FromSeconds(10)
                );
            LatitudeTextBlock.Text = geoposition.Coordinate.Latitude.ToString("0.000000");
            LongitudeTextBlock.Text = geoposition.Coordinate.Longitude.ToString("0.000000");
            StatusTextBlock.Text = "Done.";
        }
        catch (Exception ex)
        {
            if ((uint)ex.HResult == 0x80004004)
            {
                StatusTextBlock.Text = "GeoLocation im Device deaktiviert !";
            }
            else
            {
                StatusTextBlock.Text = "Sonstiger Fehler aufgetreten !";
            }
        }
        StatusTextBlock.Text = "Fertig ...";
    }

The problem seems to be the line Geoposition geoposition = await geolocator.GetGeopositionAsync. I changed the declaration of OnNavigateTo to async shown here (async await execution windows phone 8) but now my code seems to wait "endless" because "Fertig ..." is never written to my TextBox.

Any ideas how to solve my problem?

windows-phone-8
asked on Stack Overflow Apr 30, 2014 by Oliver Apel • edited May 23, 2017 by Community

1 Answer

1

Please Increase the DesiredAccuracyInMeters and try again. use 50 you would get desired results, the less the desired accuracy in meters the more time its gonna take.

if you really want the position to be accurate i.e, accuracy in 5 meters, in that case as @Andre said increase the time out. hope this helps.

answered on Stack Overflow Apr 30, 2014 by AMS • edited Apr 30, 2014 by AMS

User contributions licensed under CC BY-SA 3.0