【问题标题】:C# Xamarin Forms - CrossGeolocator.PositionChanged not being calledC# Xamarin Forms - CrossGeolocator.PositionChanged 未被调用
【发布时间】:2017-07-10 05:53:23
【问题描述】:

我正在使用 Xamarin Forms 创建一个跟踪您的位置的应用程序。在我的页面WhosOnFarm.xaml.cs 上,我有一个功能可以检测您的位置何时发生变化,该功能使用Plugins.Geolocator 类。

这段代码在我的页面的构造函数中,称为WhosOnFarm(),尽管我尝试在我的 Android 模拟器中手动更改位置信息,或者带着我的物理设备四处走动,但它从未被调用过在WhosOnFarm.xaml.cs 页面上运行的应用程序。

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

locator.PositionChanged += (sender, e) => {
    try
    {
        DisplayAlert("Success", "Your position has changed!", "OK");
        var position = e.Position;
        longitudeLabel.Text = "Longitude: " + position.Longitude.ToString() + " Latitude: " + position.Latitude.ToString();
    }
    catch (Exception ex)
    {
                longitudeLabel.Text = "================= Trip Log encountered error - ========================== " + ex.Message.ToString();
    }
};

为什么这段代码从不执行?

【问题讨论】:

    标签: c# xamarin geolocation xamarin.forms


    【解决方案1】:

    检查权限

    在开始跟踪之前,您必须检查以确保您在所有平台上都拥有位置权限。我建议使用 Permissions Plugin 以确保您拥有权限。

    开始跟踪

    /// <summary>
    /// Start listening for changes
    /// </summary>
    /// <param name="minimumTime">Time</param>
    /// <param name="minimumDistance">Distance</param>
    /// <param name="includeHeading">Include heading or not</param>
    /// <param name="listenerSettings">Optional settings (iOS only)</param>
    Task<bool> StartListeningAsync(TimeSpan minimumTime, double minimumDistance, bool includeHeading = false, ListenerSettings listenerSettings = null)
    

    UWP 注意:地理定位器的工作方式必须设置 minTime 或 minDistance。设置两者意味着 minDistance 将优先于两者。您可以在 Windows 博客上阅读更多内容。

    async Task StartListening()
    {
        if(CrossGeolocator.Current.IsListening)
            return;
    
        ///This logic will run on the background automatically on iOS, however for Android and UWP you must put logic in background services. Else if your app is killed the location updates will be killed.
        await CrossGeolocator.Current.StartListeningAsync(TimeSpan.FromSeconds(5), 10, true, new Plugin.Geolocator.Abstractions.ListenerSettings
                    {
                        ActivityType = Plugin.Geolocator.Abstractions.ActivityType.AutomotiveNavigation,
                        AllowBackgroundUpdates = true,
                        DeferLocationUpdates = true,
                        DeferralDistanceMeters = 1,
                        DeferralTime = TimeSpan.FromSeconds(1),
                        ListenForSignificantChanges = true,
                        PauseLocationUpdatesAutomatically = false
                    });
    
        CrossGeolocator.Current.PositionChanged += Current_PositionChanged;
    }
    
    private void Current_PositionChanged(object sender, Plugin.Geolocator.Abstractions.PositionEventArgs e)
    {
        Device.BeginInvokeOnMainThread(() =>
        {
            var test = e.Position;
            listenLabel.Text = "Full: Lat: " + test.Latitude.ToString() + " Long: " + test.Longitude.ToString();
            listenLabel.Text += "\n" + $"Time: {test.Timestamp.ToString()}";
            listenLabel.Text += "\n" + $"Heading: {test.Heading.ToString()}";
            listenLabel.Text += "\n" + $"Speed: {test.Speed.ToString()}";
            listenLabel.Text += "\n" + $"Accuracy: {test.Accuracy.ToString()}";
            listenLabel.Text += "\n" + $"Altitude: {test.Altitude.ToString()}";
            listenLabel.Text += "\n" + $"AltitudeAccuracy: {test.AltitudeAccuracy.ToString()}";
        });
    }           
    

    欲了解更多信息,请使用以下链接

    https://github.com/jamesmontemagno/GeolocatorPlugin

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-11-12
      • 1970-01-01
      • 2022-01-22
      • 2016-05-10
      • 1970-01-01
      • 1970-01-01
      • 2021-12-26
      相关资源
      最近更新 更多