在windows Phone中获取位置错误

本文关键字:位置 错误 获取 windows Phone | 更新日期: 2023-09-27 18:11:44

这是我在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
    }
}

我得到以下错误。

'await'操作符只能在async lambda表达式中使用。考虑用'async'修饰符标记这个lambda表达式。

在windows Phone中获取位置错误

试试下面的代码。方法中必须添加关键字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)
        {
        }
    }

希望这对你有帮助!:)