Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xamarin 实时更新地理位置_Xamarin_Xamarin.forms - Fatal编程技术网

Xamarin 实时更新地理位置

Xamarin 实时更新地理位置,xamarin,xamarin.forms,Xamarin,Xamarin.forms,我正在为自己的学习构建一个小型跑步应用程序,我正在尝试获取gps位置,因为我正在使用xamrain essentials地理位置它对手动输入工作正常我将在点更新时将点存储在列表中,但我可以订阅它们的活动,它将告诉我位置何时发生了变化,就像大多数情况一样运行应用程序用户不单击按钮 private async void BtnStart_OnClicked(object sender, EventArgs e) { var location = await Geolocation.GetLa

我正在为自己的学习构建一个小型跑步应用程序,我正在尝试获取gps位置,因为我正在使用xamrain essentials地理位置它对手动输入工作正常我将在点更新时将点存储在列表中,但我可以订阅它们的活动,它将告诉我位置何时发生了变化,就像大多数情况一样运行应用程序用户不单击按钮

private async void BtnStart_OnClicked(object sender, EventArgs e)
{
    var location = await Geolocation.GetLastKnownLocationAsync();
    if (location != null)
    {
      Console.WriteLine($"Latitude: {location.Latitude}, Longitude: {location.Longitude}, Altitude: {location.Altitude}");
    }

        lbllong.Text = location.Longitude.ToString();

        lbllat.Text = location.Latitude.ToString();
        Location StartLocation = new Location(location.Latitude,location.Longitude);
        Location endLocation = new Location(37.783333, -122.416667);

        double miles = Location.CalculateDistance(StartLocation, endLocation, DistanceUnits.Miles);

}
我希望它能够每几英尺更新一次,但我看不到任何事件,我可以挂钩到这一点

我希望它能够每几英尺更新一次,但我看不到任何事件,我可以挂钩到这一点

如果您想在距离或超时时更新位置,正如Jason所说,您可以通过nuget软件包安装使用Xam.Plugin.geologitor

您可以实施此方法来监视时间和距离:

Task<bool> StartListeningAsync(TimeSpan minimumTime, double minimumDistance, bool includeHeading = false, ListenerSettings listenerSettings = null);
您可以通过Jason first link查看整个示例,我提供了您需要的代码:

bool tracking;

    public ObservableCollection<Position> Positions { get; } = new ObservableCollection<Position>();

    public HomePage()
    {
        InitializeComponent();
        ListViewPositions.ItemsSource = Positions;
    }

private async void ButtonTrack_Clicked(object sender, EventArgs e)
    {
        try
        {
            var hasPermission = await Utils.CheckPermissions(Permission.Location);
            if (!hasPermission)
                return;

            if (tracking)
            {
                CrossGeolocator.Current.PositionChanged -= CrossGeolocator_Current_PositionChanged;
                CrossGeolocator.Current.PositionError -= CrossGeolocator_Current_PositionError;
            }
            else
            {
                CrossGeolocator.Current.PositionChanged += CrossGeolocator_Current_PositionChanged;
                CrossGeolocator.Current.PositionError += CrossGeolocator_Current_PositionError;
            }

            if (CrossGeolocator.Current.IsListening)
            {
                await CrossGeolocator.Current.StopListeningAsync();
                labelGPSTrack.Text = "Stopped tracking";
                ButtonTrack.Text = "Start Tracking";
                tracking = false;
                count = 0;
            }
            else
            {
                Positions.Clear();
                if (await CrossGeolocator.Current.StartListeningAsync(TimeSpan.FromSeconds(TrackTimeout.Value), TrackDistance.Value,
                    TrackIncludeHeading.IsToggled, new ListenerSettings
                    {
                        ActivityType = (ActivityType)ActivityTypePicker.SelectedIndex,
                        AllowBackgroundUpdates = AllowBackgroundUpdates.IsToggled,
                        DeferLocationUpdates = DeferUpdates.IsToggled,
                        DeferralDistanceMeters = DeferalDistance.Value,
                        DeferralTime = TimeSpan.FromSeconds(DeferalTIme.Value),
                        ListenForSignificantChanges = ListenForSig.IsToggled,
                        PauseLocationUpdatesAutomatically = PauseLocation.IsToggled
                    }))
                {
                    labelGPSTrack.Text = "Started tracking";
                    ButtonTrack.Text = "Stop Tracking";
                    tracking = true;
                }
            }
        }
        catch (Exception ex)
        {
            await DisplayAlert("Uh oh", "Something went wrong, but don't worry we captured for analysis! Thanks.", "OK");
        }
    }




void CrossGeolocator_Current_PositionError(object sender, PositionErrorEventArgs e)
{

    labelGPSTrack.Text = "Location error: " + e.Error.ToString();
}

void CrossGeolocator_Current_PositionChanged(object sender, PositionEventArgs e)
{

    Device.BeginInvokeOnMainThread(() =>
    {
        var position = e.Position;
        Positions.Add(position);
        count++;
        LabelCount.Text = $"{count} updates";
        labelGPSTrack.Text = 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);

    });
}
bool跟踪;
公共ObservableCollection位置{get;}=new ObservableCollection();
公共网页()
{
初始化组件();
ListViewPositions.ItemsSource=位置;
}
私有异步无效按钮已单击回退(对象发送方,事件参数e)
{
尝试
{
var haspmission=wait Utils.CheckPermissions(Permission.Location);
如果(!hasPermission)
返回;
如果(跟踪)
{
CrossGeolocator.Current.PositionChanged-=CrossGeolocator\u Current\u PositionChanged;
CrossGeolocator.Current.PositionError-=CrossGeolocator\u Current\u PositionError;
}
其他的
{
CrossGeolocator.Current.PositionChanged+=CrossGeolocator\u Current\u PositionChanged;
CrossGeolocator.Current.PositionError+=CrossGeolocator\u Current\u PositionError;
}
if(CrossGeolocator.Current.IsListening)
{
等待CrossGeolocator.Current.StopListingAsync();
labelGPSTrack.Text=“停止跟踪”;
ButtonTrack.Text=“开始跟踪”;
跟踪=假;
计数=0;
}
其他的
{
位置。清除();
如果(等待CrossGeolocator.Current.StartListeningAsync(TimeSpan.FromSeconds(TrackTimeout.Value)),TrackDistance.Value,
TrackIncludeHeading.i切换,新的ListenerSettings
{
ActivityType=(ActivityType)ActivityTypePicker.SelectedIndex,
AllowBackgroundUpdates=AllowBackgroundUpdates.i切换,
DeferLocationUpdates=DeferUpdates.i切换,
延迟距离计=延迟距离.Value,
延迟时间=TimeSpan.FromSeconds(延迟时间值),
ListenForSignificantChanges=ListenForSig.IsToggled,
PauseLocation更新自动=PauseLocation.i切换
}))
{
labelGPSTrack.Text=“已开始跟踪”;
ButtonTrack.Text=“停止跟踪”;
跟踪=真;
}
}
}
捕获(例外情况除外)
{
等待DisplayAlert(“哦”,“出了点问题,但别担心,我们捕获了一些数据进行分析!谢谢。”,“好”);
}
}
无效交叉地理定位器\u当前位置错误(对象发送器,位置错误事件参数e)
{
labelGPSTrack.Text=“位置错误:”+e.error.ToString();
}
无效交叉地理定位器\u当前位置已更改(对象发送方,位置事件参数e)
{
Device.beginInvokeMainThread(()=>
{
var位置=e位置;
职位。添加(职位);
计数++;
Text=$“{count}更新”;
labelGPSTrack.Text=string.Format(“时间:{0}\nLat:{1}\nLong:{2}\nAltitude:{3}\nAltitude准确度:{4}\n准确度:{5}\n阅读:{6}\n速度:{7}”,
位置,时间戳,位置,纬度,位置,经度,
位置。高度,位置。高度准确度,位置。准确度,位置。航向,位置。速度);
});
}

这就是我已经在使用的@Jason插件,它告诉我什么我还没有不“我正在使用xamrain essentials geolocation”-这不是XamarinEssentials@JasonJames说他自己的插件被抹黑了,你应该使用xamrainessientials@Jason我想你会发现它是var location=await Geolocation.GetLastKnownLocationAsync();
bool tracking;

    public ObservableCollection<Position> Positions { get; } = new ObservableCollection<Position>();

    public HomePage()
    {
        InitializeComponent();
        ListViewPositions.ItemsSource = Positions;
    }

private async void ButtonTrack_Clicked(object sender, EventArgs e)
    {
        try
        {
            var hasPermission = await Utils.CheckPermissions(Permission.Location);
            if (!hasPermission)
                return;

            if (tracking)
            {
                CrossGeolocator.Current.PositionChanged -= CrossGeolocator_Current_PositionChanged;
                CrossGeolocator.Current.PositionError -= CrossGeolocator_Current_PositionError;
            }
            else
            {
                CrossGeolocator.Current.PositionChanged += CrossGeolocator_Current_PositionChanged;
                CrossGeolocator.Current.PositionError += CrossGeolocator_Current_PositionError;
            }

            if (CrossGeolocator.Current.IsListening)
            {
                await CrossGeolocator.Current.StopListeningAsync();
                labelGPSTrack.Text = "Stopped tracking";
                ButtonTrack.Text = "Start Tracking";
                tracking = false;
                count = 0;
            }
            else
            {
                Positions.Clear();
                if (await CrossGeolocator.Current.StartListeningAsync(TimeSpan.FromSeconds(TrackTimeout.Value), TrackDistance.Value,
                    TrackIncludeHeading.IsToggled, new ListenerSettings
                    {
                        ActivityType = (ActivityType)ActivityTypePicker.SelectedIndex,
                        AllowBackgroundUpdates = AllowBackgroundUpdates.IsToggled,
                        DeferLocationUpdates = DeferUpdates.IsToggled,
                        DeferralDistanceMeters = DeferalDistance.Value,
                        DeferralTime = TimeSpan.FromSeconds(DeferalTIme.Value),
                        ListenForSignificantChanges = ListenForSig.IsToggled,
                        PauseLocationUpdatesAutomatically = PauseLocation.IsToggled
                    }))
                {
                    labelGPSTrack.Text = "Started tracking";
                    ButtonTrack.Text = "Stop Tracking";
                    tracking = true;
                }
            }
        }
        catch (Exception ex)
        {
            await DisplayAlert("Uh oh", "Something went wrong, but don't worry we captured for analysis! Thanks.", "OK");
        }
    }




void CrossGeolocator_Current_PositionError(object sender, PositionErrorEventArgs e)
{

    labelGPSTrack.Text = "Location error: " + e.Error.ToString();
}

void CrossGeolocator_Current_PositionChanged(object sender, PositionEventArgs e)
{

    Device.BeginInvokeOnMainThread(() =>
    {
        var position = e.Position;
        Positions.Add(position);
        count++;
        LabelCount.Text = $"{count} updates";
        labelGPSTrack.Text = 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);

    });
}