【问题标题】:Not Getting Current Location in Tizen .Net Application未在 Tizen .Net 应用程序中获取当前位置
【发布时间】:2019-08-03 22:51:07
【问题描述】:

我正在尝试使用GetLocation 方法从Tizen.Location.Locatorget location,但我将纬度和经度的值设置为0

我正在使用:

  • Visual Studio 2017
  • 可穿戴4.0模拟器
  • 已授予privileges 使用位置,还在清单文件中添加了位置功能。

这是我的代码:

private static Locator locator = new Locator(LocationType.Gps);
try
{
    if (locator != null)
    {
        // Starts the Locator
        locator.Start();

        //Get Location
        Tizen.Location.Location currentLocation = locator.GetLocation();

        //Update xaml view
        latitudeValue.Text = currentLocation.Latitude.ToString();
        longitudeValue.Text = currentLocation.Longitude.ToString();
    }   
}
catch (InvalidOperationException exception)
{
    // Exception handling here
}

没有关于变量currentLocation 的描述,因为它在那里被视为bool。当我尝试通过 Visual Studio 的快速监视功能(通过按 Shift + F9)获取描述时,我收到另一个与该 bool 变量转换相关的错误

成员引用基类型 'bool' 不是结构或联合

上面的代码没有显示任何注册的ServiceStateChanged事件,但我也使用sample code中显示的方式包含了它,但它对我不起作用。

现在想知道我在这里做错了什么。

【问题讨论】:

  • 模拟器模拟位置。您是否已在模拟器的控制面板中更改为所需位置?如果未配置,它将返回 0,0
  • 我真的忘记了 .. 非常感谢让我度过了愉快的一天 :) 现在它在模拟器中工作,但它也在真实设备上提供模拟值(因为我使用 @987654333 设置模拟值@方法)当我删除了模拟并开始调试时,它抛出了locator.Start()方法上的异常。不幸的是,我在这里看不到异常的描述。

标签: .net xamarin xamarin.forms tizen xamarin.tizen


【解决方案1】:

由于电池消耗或通过服务提供商获取位置,您的 GPS 大部分时间处于不活动状态

使用此插件获取当前位置 - https://github.com/jamesmontemagno/GeolocatorPlugin

 public async Task<Position> GetCurrentLocation()
{
   public static async Task<Position> GetCurrentPosition()
    {
            Position position = null;
            try
            {
                    var locator = CrossGeolocator.Current;
                    locator.DesiredAccuracy = 100;

                    position = await locator.GetLastKnownLocationAsync();

                    if (position != null)
                    {
                            //got a cahched position, so let's use it.
                            return position;
                    }

                    if (!locator.IsGeolocationAvailable || !locator.IsGeolocationEnabled)
                    {
                            //not available or enabled
                            return null;
                    }

                    position = await locator.GetPositionAsync(TimeSpan.FromSeconds(20), null, true);

            }
            catch (Exception ex)
            {
                    Debug.WriteLine("Unable to get location: " + ex);
            }

            if (position == null)
                    return null;

            var output = string.Format("Time: {0} \nLat: {1} \nLong: {2} \nAltitude: {3} \nAltitude Accuracy: {4} \nAccuracy: {5} \nHeading: {6} \nSpeed: {7}",
                    position.Timestamp, position.Latitude, position.Longitude,
                    position.Altitude, position.AltitudeAccuracy, position.Accuracy, position.Heading, position.Speed);

            Debug.WriteLine(output);

            return position;
    }
}

【讨论】:

  • 很高兴看到这样做的另一种方法。我使用了this 方式与您的if-else 条件相结合。我的代码看起来像this,但我遇到了一个异常,当我检查CrossGeolocator.IsSupportedlocator.IsGeolocationAvailable 甚至var position 的值时,我在快速查看时收到关于未声明标识符的错误i> 结束了,我是否错过了正确使用CrossGeolocator 的任何参考?
  • 或者在本地服务提供商中获取位置
猜你喜欢
  • 2012-11-26
  • 1970-01-01
  • 1970-01-01
  • 2014-08-07
  • 1970-01-01
  • 1970-01-01
  • 2012-06-12
  • 2015-11-18
  • 2015-08-01
相关资源
最近更新 更多