简体   繁体   English

在Windows Phone中获取位置时出错

[英]Error getting Location in windows Phone

This is my code to get the location in Windows Phone SDK. 这是我在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. “ await”运算符只能在异步lambda表达式中使用。 Consider marking this lambda expression with the 'async' modifier. 考虑使用'async'修饰符标记该lambda表达式。

Here try this code. 在这里尝试这段代码。 The method you must add the keyword async 您必须添加关键字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! 希望这可以帮助! :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM