【问题标题】:GetGeopositionAsync does not returnGetGeopositionAsync 不返回
【发布时间】:2014-12-07 16:20:15
【问题描述】:

在我的 Windows Phone 8 应用程序中,我尝试在主页上使用 GetGeopositionAsync 来根据用户位置显示一些项目。

调用 GetGeopositionAsync 不会在指定的超时时间内返回,它根本不会返回。

我使用的代码很简单:

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

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

                // location services disabled or other error
                // user should setup his location

            }

我能够找到的解决方案是为 GeoCoordinateWatcher 创建一个异步包装器,它似乎工作正常。 但我对我的解决方案不太有信心,我更喜欢使用 GetGeopositionAsync,这看起来像是在 WP8 中获取设备位置的推荐方式。

更新:其他人报告相同的行为: http://social.msdn.microsoft.com/forums/en-us/wpdevelop/thread/ff166fac-b423-4428-abd8-610bf0102fc0

【问题讨论】:

  • 名为“IsBusy”的变量始终是创建死锁的好方法。我们看不到你还在哪里使用它。
  • 请忽略。我在 await 之后和 catch 子句中放置了断点,但都没有到达
  • 你调用Wait 还是Result 进一步提高你的调用堆栈?

标签: windows-phone-8


【解决方案1】:

你什么时候调用该方法来请求地理位置?我发现当我在 ViewModel 中调用构造函数的一部分时遇到了同样的问题。

我能够通过添加 OnLoadedCommand 并从那里调用方法来解决我的代码中的问题。从那以后我再也没有遇到过问题。

【讨论】:

  • 这正是我想出的解决方案,它完全解决了我的问题。
  • 从 page.Loaded 事件调用也为我解决了这个问题。在我以前调用 VM 构造函数之前。
【解决方案2】:

这很奇怪,但 GetGeoPositionAsync 仅在使用 MovementThreshold 和/或 ReportInterval 初始化 Geolocator 时返回当前位置。

Geolocator geolocator = new Geolocator();
geolocator.DesiredAccuracyInMeters = 50;
geolocator.MovementThreshold = 5;
geolocator.ReportInterval = 500;

Geoposition geoposition = null;
try
{
    geoposition = await geolocator.GetGeopositionAsync(
        maximumAge: TimeSpan.FromMinutes(5),
        timeout: TimeSpan.FromSeconds(10));
}
catch (UnauthorizedAccessException ex)
{
    // location services disabled or other error
    // user should setup his location
}

【讨论】:

  • 这就是解决方案!!
  • 为了让它工作,我只设置了这两个 geolocator.DesiredAccuracyInMeters = 2; geolocator.MovementThreshold = 3;感谢您的提示!
【解决方案3】:

我在设备上进行测试时遇到了这个问题。我必须禁用设备上的 WiFi 才能使其正常工作。我知道有些人在模拟器上工作时遇到了相反的问题。我不必做任何包装。希望对你有帮助

【讨论】:

    【解决方案4】:

    我发现了一件事。如果我将精度设置为更大,则地理定位器开始返回坐标。所以它不适用于 50 米,但适用于 500 米,因此请尝试使用下面的线。

            geolocator.DesiredAccuracyInMeters = 500;
    

    【讨论】:

      【解决方案5】:

      我在上面遇到了一些相同的问题。当我连接到 geolocator.StatusChanged 事件时,我注意到事件序列是:

      1. StatusChanged -> 正在初始化
      2. 我的等待电话
      3. StatusChanged -> 就绪

      所以我在 await 调用之前添加了一个循环:

        while (geolocator.LocationStatus == PositionStatus.Initializing)
        {
            System.Threading.Thread.Sleep(100);
        }
      

      这很不雅,但确实有效。

      【讨论】:

        【解决方案6】:

        当调用GetGeopositionAsync()geolocator 的状态处于NotInitialized 状态时会发生这种奇怪的行为。

        geolocator 仅在两种情况下为 Ready。一,当它订阅PositionChanged 事件时。二,当 GetGeopositionAsync() 已经被调用时。

        因此,您只需在调用GetGeopositionAsync() 之前将geolocator 订阅到positionChanged 事件即可。

        希望这会有所帮助。

        【讨论】:

          【解决方案7】:

          我发现如果你在本地创建地理定位器,任务最终会被取消。当我创建一个永久的 Geolocator 实例时,它就可以工作了。

          【讨论】:

            【解决方案8】:

            查看我的示例: http://code.msdn.microsoft.com/windowsapps/How-to-use-Cimbalino-3888977e

            它使用 MVVM 和 Cimbalino 工具包!

            在我的例子中,我设置 ReportInterval = 5 来解决这个问题。

            【讨论】:

              【解决方案9】:

              好吧,看起来每个人都被黑了,直到它起作用了……这对我有用:

              /// <summary>
              /// HACK: For some reason Geolocator.GetGeopositionAsync hangs indefinitely.
              /// The workaround is to add a PositionChanged handler.
              /// </summary>
              private Geoposition GetGeoposition()
              {
                  var geolocator = new Geolocator();
                  var semaphoreHeldUntilPositionReady = new SemaphoreSlim(initialCount: 0);
                  Geoposition position = null;
              
                  geolocator.ReportInterval = 1000;
                  geolocator.PositionChanged += (sender, args) =>
                  {
                      position = args.Position;
                      semaphoreHeldUntilPositionReady.Release();
                  };
              
                  semaphoreHeldUntilPositionReady.Wait();
                  return position;
              }
              

              【讨论】:

                【解决方案10】:

                我知道这有点老,但我希望其他搜索此主题的人会发现此答案有帮助。

                请确保在尝试访问位置服务之前征得用户的同意。

                我遇到了这个问题,但通过在打开页面时调用 OnNavigatedTo 事件来解决它以获得他们的同意。

                protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
                {
                    if (IsolatedStorageSettings.ApplicationSettings.Contains("LocationConsent"))
                    {
                        // User has opted in or out of Location
                        return;
                    }
                    else
                    {
                        MessageBoxResult result = 
                            MessageBox.Show("This app accesses your phone's location. Is that ok?", 
                            "Location",
                            MessageBoxButton.OKCancel);
                
                        if (result == MessageBoxResult.OK)
                        {
                            IsolatedStorageSettings.ApplicationSettings["LocationConsent"] = true;
                        }else
                        {
                            IsolatedStorageSettings.ApplicationSettings["LocationConsent"] = false;
                        }
                
                        IsolatedStorageSettings.ApplicationSettings.Save();
                    }
                }
                

                【讨论】:

                • 投反对票,因为您的回答与 OP 没有任何关系。您的应用将无法通过认证,但是否同意与地理位置/地理定位工作与否无关。
                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2013-09-26
                • 2019-04-10
                • 1970-01-01
                • 1970-01-01
                • 2019-06-30
                • 2011-11-17
                相关资源
                最近更新 更多