2016-09-26 49 views
0

我正在开发与andorid,UWP和Windows 8项目的Xamarin表单应用程序。我正在使用Jamesmontemagno创建的Geolocation插件来获取当前设备位置。它在Windows 8和UWP中工作正常,但每当我尝试运行它对Android设备时,我不断收到任务取消异常。我已根据建议检查了所需的所有权限,但仍然没有运气。我的代码访问的位置低于Xamarin格式地理定位任务取消异常

protected override void OnAppearing() 
     { 
      var locator = CrossGeolocator.Current; 
      locator.DesiredAccuracy = 100; //100 is new default 
      if (locator.IsGeolocationAvailable && locator.IsGeolocationEnabled) 
      { 
       try 
       { 
        var position = locator.GetPositionAsync(timeoutMilliseconds: 60000).Result; 
        //var pp = helper.Setting.Location; 
        var Latitude = position.Latitude; 
        var Longitude = position.Longitude; 
       } 
       catch(Exception ex) 
       { 
        var exc = ex; 
       } 
      } 
     } 

下面是我设置的Android清单 enter image description here

+0

你试图使用的await关键字?像这里:https://github.com/jamesmontemagno/GeolocatorPlugin – Radinator

+0

你测试什么设备?像魅族这样的一些Android设备存在问题,导致这种异常。尝试重新启动您的手机 – Greensy

+0

@Radinator我一直在与任务工作了这么久,但从来没有想过,等待会修复它得到它的工作终于请改变你的评论,以便我可以接受它的答案我会发布的工作解决方案也 –

回答

0

感谢的图像@Radinator以下是可行的解决方案。就像是在original code使用

protected async override void OnAppearing() 
     { 
      var locator = CrossGeolocator.Current; 
      locator.DesiredAccuracy = 100; //100 is new default 
      if (locator.IsGeolocationAvailable && locator.IsGeolocationEnabled) 
      { 
       try 
       { 
        await SetLocation(); 
       } 
       catch (Exception ex) 
       { 
        var exc = ex; 
       } 
      } 
     } 

     private async Task SetLocation() 
     { 
      var locator = CrossGeolocator.Current; 
      locator.DesiredAccuracy = 100; //100 is new default 
      if (locator.IsGeolocationAvailable && locator.IsGeolocationEnabled) 
      { 
       try 
       { 
        var position = await locator.GetPositionAsync(timeoutMilliseconds: 60000); 

        var Latitude = position.Latitude; 
        var Longitude = position.Longitude; 
       } 
       catch (Exception ex) 
       { 
        //log ex; 
        throw ex; 
       } 
      } 
     } 
2

尝试使用await关键字:

try 
{ 
    var locator = CrossGeolocator.Current; 
    locator.DesiredAccuracy = 50; 

    var position = await locator.GetPositionAsync (timeoutMilliseconds: 10000); 

    Console.WriteLine ("Position Status: {0}", position.Timestamp); 
    Console.WriteLine ("Position Latitude: {0}", position.Latitude); 
    Console.WriteLine ("Position Longitude: {0}", position.Longitude); 
} 
catch(Exception ex) 
{ 
    Debug.WriteLine("Unable to get location, may need to increase timeout: " + ex); 
} 

这应该照顾,有没有竞争条件,因此TaskCancellationException

2

对于任何其他人,即使在await时也只是在Android上,即使该设备的Google地图应用正常工作,您也可能会遇到this bug,这种情况只发生在某些Android设备上,但只有少数他们在那。

这个问题是Google从未解决的问题。该解决方案以及Google地图应用正常运行的一个可能原因是使用Google Play服务的融合位置提供商。

目前Geolocator插件只是使用常规Android位置提供程序,但James提到他想在某个时候使用Fused提供程序。我还没有尝试融合的提供者。

0

面对v3.0.4中的'Task killed'问题。以下为我工作:

  1. 卸载的应用
  2. 更新Geolocator抢鲜4.0