【问题标题】:Retrieving Location on page open在打开的页面上检索位置
【发布时间】:2014-04-30 22:46:33
【问题描述】:

我有一个页面,当它打开时,我想检索手机的地理位置。 这是我的代码:

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 ...";
    }

问题似乎出在Geoposition geoposition = await geolocator.GetGeopositionAsync 这一行。我将OnNavigateTo 的声明更改为此处显示的async (async await execution windows phone 8),但现在我的代码似乎在等待“无休止”,因为从未将"Fertig ... 写入我的TextBox。

任何想法如何解决我的问题?

【问题讨论】:

  • 您是否尝试过增加超时时间?由于这是您第一次获得当前位置,因此可能需要更长的时间......
  • 如果我在页面上添加一个按钮并在 Click 事件中运行代码,则设置(超时、准确性和年龄)都可以并且运行良好...但我想运行代码在页面打开时。
  • 你在按钮点击事件中声明了地理定位器?请尝试增加超时,正如@Dream Coder 所说,尝试增加 DesiredAccuracyInMeters。至少通过这种方式,您将确定问题是否与 GPS 获取位置(没有时间)或是否是任何其他问题有关。
  • 是按钮点击事件。并且它与设置一起使用 - 但不适用于 OnNavigatedTo。我需要10-20米的精度...
  • 我测试了上面的代码,没有问题。你能调试它并看到输出控制台吗?在这里没有得到问题:S

标签: windows-phone-8


【解决方案1】:

请增加 DesiredAccuracyInMeters 并重试。使用 50 你会得到想要的结果,以米为单位的所需精度越低,花费的时间就越多。

如果您真的希望位置准确,即 5 米的精度,在这种情况下,正如@Andre 所说,增加超时时间。希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-03
    • 1970-01-01
    • 1970-01-01
    • 2020-12-02
    • 1970-01-01
    相关资源
    最近更新 更多