Error getting Location in windows Phone

2

This is my code to get the location in Windows Phone SDK.

Geolocator geolocator = new Geolocator();
geolocator.DesiredAccuracyInMeters = 50;

try
{
    Geoposition geoposition = await geolocator.GetGeopositionAsync(maximumAge: TimeSpan.FromMinutes(5),timeout: TimeSpan.FromSeconds(10));

    LatitudeTextBlock.Text = geoposition.Coordinate.Latitude.ToString("0.00");
    LongitudeTextBlock.Text = geoposition.Coordinate.Longitude.ToString("0.00");
}
catch (Exception ex)
{
    if ((uint)ex.HResult == 0x80004004)
    {
        // the application does not have the right capability or the location master switch is off
        MessageBox.Show("location  is disabled in phone settings.");
    }
    //else
    {
        // something else happened acquring the location
    }
}

I am getting the following error.

The 'await' operator can only be used within an async lambda expression. Consider marking this lambda expression with the 'async' modifier.

c#
windows-phone-8
lambda
visual-studio-2013
async-await
asked on Stack Overflow Jul 5, 2014 by Samarth Agarwal • edited Jul 7, 2014 by svick

1 Answer

0

Here try this code. The method you must add the keyword async

public string latitude, longitude;

 async private void GetLocation()
        {

        try
        {

            var geolocator = new Geolocator();

            if ((bool)IsolatedStorageSettings.ApplicationSettings["LocationConsent"] == true)
            {
                Geoposition position = await geolocator.GetGeopositionAsync();
                Geocoordinate coordinate = position.Coordinate;

                latitude = Convert.ToString(Math.Round(coordinate.Latitude, 2));
                longitude = Convert.ToString(Math.Round(coordinate.Longitude, 2));
            }
            else
            {


                MessageBox.Show("Please enable location services to use this feature. You can turn it on from Settings.");

            }

        }
        catch (Exception)
        {

        }
    }

Hope this helps! :)

answered on Stack Overflow Jul 23, 2014 by user3795349

User contributions licensed under CC BY-SA 3.0