【问题标题】:Xamarin.Forms DependencyService difficulty getting locationXamarin.Forms DependencyService 难以获取位置
【发布时间】:2014-09-02 09:18:42
【问题描述】:

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

我的界面

public interface ICurrentLocation
{
    MyLocation SetCurrentLocation();
}

我的位置

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

调用它的行

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

以及在实现 Xamarin.Mobile 的 Geolocation 类的 Android 项目的 CurrentLocation 类中

[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 Activity 运行,则该代码有效

【问题讨论】:

  • 我在“地理定位器”声明中遇到错误。我添加了 nuget 包“Plugin.Geolocator”。

标签: android location xamarin xamarin.forms


【解决方案1】:

我会稍作修改,因为最佳做法是全部异步或不执行。当您尝试从非异步方法返回异步方法的结果时,您可能会遇到死锁问题。此外,由于您在调用 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());
    }
}

【讨论】:

  • 谢谢 jendsendp,我现在正在尝试你的版本,我还有几个问题,但我怀疑这是我所说的 -
  • 获取以 System.AggregateException 开头的错误列表:---> Java.Lang.IllegalStateException,会做一些研究
  • 您是在上面引用的代码中还是在代码中的其他地方遇到错误?
  • 它在GetPosition(我把它改成了Task(Position> GetPosition())
  • Java.Lang.IllegalStateException
【解决方案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,因为它已经抽象了 GPS 服务和适用于所有 3 个平台的工作示例:

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

【讨论】:

  • 感谢您的回复,我从 Xamarin-Forms-Labs 示例开始,但作为一个完整的新手,我根本无法遵循它。我正在开始 C#、Xamarin 等速成课程
【解决方案3】:

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

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

【讨论】:

  • 感谢您的回复。我查看了等待/异步 - 据我所知,使用它的方法必须返回 void 或任务,我怎么能返回 MyLocation?这对我来说是全新的,你能告诉我如何实施吗?另外,我已经在 Android 文件中的命名空间上方有程序集,我还需要把它放在其他地方吗?
  • 你可以使用通用的Task类。返回类似 Task. 的内容
猜你喜欢
  • 2023-03-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多