Xamarin.Forms DependencyService难以获取位置

时间:2014-09-01 08:21:36

标签: android location xamarin xamarin.forms

因此,在最终了解了Xamarin.Forms DependencyService后,我几乎得到了它返回设备的当前位置。

我的界面

public interface ICurrentLocation
{
    MyLocation SetCurrentLocation();
}

MyLocation

public class MyLocation
{
    public double Latitude {get; set;}
    public double Longitude{get; set;}
}

调用它的行

MyLocation location = DependencyService.Get<ICurrentLocation>().SetCurrentLocation();

并在Android项目的CurrentLocation类中实现了Xamarin.Mobile的Geolocation类

[assembly: Dependency(typeof(CurrentLocation))]
namespace MyCockburn.Droid
{

    public class CurrentLocation : Activity, ICurrentLocation

        Geolocator locator;
        Position position = new Position();
        MyLocation location;

        public MyLocation SetCurrentLocation()
        {
            GetPosition();
            location = new MyLocation()
            {
                Latitude = position.Latitude,
                Longitude = position.Longitude
            };


            return location;
       }

       async void GetPosition()
       {
           try
           {
                 locator = new Geolocator(this) { DesiredAccuracy = 50 };
                 if (locator.IsListening != true)
                       locator.StartListening(minTime: 1000, minDistance: 0);

                 position = await locator.GetPositionAsync(timeout: 20000);

           }
           catch (Exception e)
           {
               Log.Debug("GeolocatorError", e.ToString());
           }
       }
   }

我的问题似乎是在位置保持经度和纬度之前返回位置

我希望我的错误显而易见

编辑:如果我将其作为普通的Android活动

运行,则代码可以正常运行

3 个答案:

答案 0 :(得分:3)

我会稍作修改,因为最佳做法是要么全部执行异步操作,要么不执行任何操作。当您尝试从非异步方法返回异步方法的结果时,您可能会遇到死锁问题。此外,由于您在调用GetPosition方法时未使用await关键字,因此您将返回一个任务,但不会检查操作何时完成。我建议稍微修改你的代码。

public interface ICurrentLocation {
    Task<MyLocation> GetCurrentLocation();
}

public async Task<MyLocation> GetCurrentLocation()
{
    var position = await GetPosition();
    return new MyLocation()
    {
        Latitude = position.Latitude,
        Longitude = position.Longitude
    };
}

async Task<Location> GetPosition()
{
    try
    {
          locator = new Geolocator(this) { DesiredAccuracy = 50 };
          if (locator.IsListening != true)
                locator.StartListening(minTime: 1000, minDistance: 0);

          return await locator.GetPositionAsync(timeout: 20000);

    }
    catch (Exception e)
    {
        Log.Debug("GeolocatorError", e.ToString());
    }
}

答案 1 :(得分:2)

您不等待位置功能完成。许多不同的选项并保持异步是最好的选择,但是如果你想让它同步,那么试试这个阻塞调用:

   void GetPosition()
   {
       try
       {
             locator = new Geolocator(this) { DesiredAccuracy = 50 };
             if (locator.IsListening != true)
                   locator.StartListening(minTime: 1000, minDistance: 0);

             position = locator.GetPositionAsync(timeout: 20000).Result;

       }
       catch (Exception e)
       {
           Log.Debug("GeolocatorError", e.ToString());
       }
   }

我还建议看一下Xamarin.Forms.Labs,因为它已经提取了所有3个平台的GPS服务和工作样本:

https://github.com/XForms/Xamarin-Forms-Labs

答案 2 :(得分:0)

尝试在命名空间上方添加程序集并等待GetPosition方法。

看看这张图片: http://developer.xamarin.com/guides/cross-platform/xamarin-forms/dependency-service/Images/solution.png