2013-02-26 70 views
15

在我的Windows Phone 8应用程序中,我试图在主页上使用GetGeopositionAsync来根据用户位置显示一些项目。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中获取设备位置的推荐方式。

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

+0

命名变量“IsBusy”始终是创造僵局的好方法。我们无法看到您使用它的其他位置。 – 2013-02-26 03:17:16

+0

请忽略。我在等待和catch子句之后放置了断点,并且都没有到达 – 2013-02-26 03:18:48

+1

您是否在呼叫堆栈上进一步呼叫了“Wait”或“Result”? – 2013-06-11 03:04:44

回答

4

这是奇怪,但只有GetGeoPositionAsync时返回Geolocator与任何MovementThreshold和/或ReportInterval初始化的当前位置。

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 
} 
+0

这是解决方案! – saramgsilva 2014-08-10 13:21:45

+0

为了得到它,我只设置了这两个geolocator.DesiredAccuracyInMeters = 2; geolocator.MovementThreshold = 3;谢谢你的提示! – 2015-01-07 19:27:46

5

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

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

+0

这正是我提出的解决方案,它完全解决了我的问题。 – Senkwe 2013-11-30 13:24:43

+0

从page.Loaded事件调用也解决了我的问题。在我以前调用VM构造函数之前。 – Alex 2014-09-17 21:04:50

2

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

0

我有一些上述相同的问题。当我挂在geolocator.StatusChanged事件,我注意到事件的该序列是:

  1. StatusChanged - >初始化
  2. 我的await调用
  3. StatusChanged - >就绪

所以我在我的等待通话之前添加了一个循环:

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

这不雅,但确实有效。

0

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

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

因此,您只需在致电GetGeopositionAsync()之前订阅geolocatorpositionChanged事件。

希望这会有所帮助。

-3

我知道这有点旧,但我希望其他人在寻找这个主题时发现这个答案有帮助。

在尝试访问位置服务之前,请务必获得用户的同意。

我遇到了这个问题,但通过在打开页面以获得他们的同意时调用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(); 
    } 
} 
+1

Downvote,因为你的答案与OP没有任何关系。您的应用程序将无法通过认证,但是同意与地理位置/地理位置工作与否无关。 – 2014-03-06 00:13:46

1

我发现了一件事。如果我将精度设置为较大值,那么geolocator会启动返回坐标。所以它不适用于50米,但适用于500米,所以尝试使用下面的线代替。

 geolocator.DesiredAccuracyInMeters = 500; 
0

我发现如果你在本地创建了Geolocator,任务将最终被取​​消。它在我创建永久Geolocator实例时起作用。

0

那么它看起来像其他人砍死距离,直到它的工作...以下是我工作:

/// <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; 
}