diff --git a/TRViS.LocationService/LonLatLocationService.cs b/TRViS.LocationService/LonLatLocationService.cs index 48ecd100..55dc0cc0 100644 --- a/TRViS.LocationService/LonLatLocationService.cs +++ b/TRViS.LocationService/LonLatLocationService.cs @@ -22,7 +22,6 @@ public StaLocationInfo[]? StaLocationInfo _staLocationInfo = value; ResetLocationInfo(); - LocationStateChanged?.Invoke(this, new(CurrentStationIndex, IsRunningToNextStation)); } } @@ -46,6 +45,9 @@ void ResetLocationInfo(bool invokeEvent) static int GetPastStationIndex(in StaLocationInfo[] staLocationInfo, int currentStationIndex) { + if (currentStationIndex <= 0) + return CURRENT_STATION_INDEX_NOT_SET; + for (int i = currentStationIndex - 1; i >= 0; i--) { if (staLocationInfo[i].HasLonLatLocation) @@ -233,6 +235,11 @@ public double SetCurrentLocation(double lon_deg, double lat_deg) // LastStationであれば、RunningToNextStationはfalseであるはずである。 // -> 次の駅は必ず存在する int nextStationIndex = GetNextStationIndex(StaLocationInfo, CurrentStationIndex); + if (nextStationIndex < 0) + { + // 入るはずはないけども、念のため。 + return double.NaN; + } StaLocationInfo nextStation = StaLocationInfo[nextStationIndex]; distance = Utils.CalculateDistance_m(nextStation, new LocationLonLat_deg(lon_deg, lat_deg)); double distanceToNextStationAverage = GetDistanceToStationAverage(nextStation, lon_deg, lat_deg); diff --git a/TRViS/DTAC/VerticalTimetableView.Init.cs b/TRViS/DTAC/VerticalTimetableView.Init.cs index ce0dd2d3..18a76b3f 100644 --- a/TRViS/DTAC/VerticalTimetableView.Init.cs +++ b/TRViS/DTAC/VerticalTimetableView.Init.cs @@ -49,6 +49,7 @@ public VerticalTimetableView() Grid.SetColumnSpan(CurrentLocationLine, 8); LocationService.LocationStateChanged += LocationService_LocationStateChanged; + LocationService.IsEnabledChanged += (_, e) => IsLocationServiceEnabled = e.NewValue; LocationService.ExceptionThrown += (s, e) => { MainThread.BeginInvokeOnMainThread(() => Shell.Current.DisplayAlert("Location Service Error", e.ToString(), "OK")); diff --git a/TRViS/Services/LocationService.cs b/TRViS/Services/LocationService.cs index 875cac61..9542fdee 100644 --- a/TRViS/Services/LocationService.cs +++ b/TRViS/Services/LocationService.cs @@ -23,6 +23,7 @@ public partial class LocationService : ObservableObject, IDisposable [ObservableProperty] bool _IsEnabled; + public event EventHandler>? IsEnabledChanged; readonly LonLatLocationService LonLatLocationService; @@ -67,6 +68,7 @@ partial void OnIsEnabledChanged(bool value) logger.Info("IsEnabled is changed to true -> start GPS"); Task.Run(StartGPS); } + IsEnabledChanged?.Invoke(this, new ValueChangedEventArgs(!value, value)); } public void SetTimetableRows(TimetableRow[]? timetableRows) @@ -74,7 +76,7 @@ public void SetTimetableRows(TimetableRow[]? timetableRows) logger.Trace("Setting TimetableRows..."); IsEnabled = false; - LonLatLocationService.StaLocationInfo = timetableRows?.Select(v => new StaLocationInfo(v.Location.Location_m, v.Location.Longitude_deg, v.Location.Latitude_deg, v.Location.OnStationDetectRadius_m)).ToArray(); + LonLatLocationService.StaLocationInfo = timetableRows?.Where(v => !v.IsInfoRow).Select(v => new StaLocationInfo(v.Location.Location_m, v.Location.Longitude_deg, v.Location.Latitude_deg, v.Location.OnStationDetectRadius_m)).ToArray(); } static EasterEggPageViewModel EasterEggPageViewModel { get; } = InstanceManager.EasterEggPageViewModel; @@ -217,6 +219,10 @@ async Task PositioningTask() { double distance = LonLatLocationService.SetCurrentLocation(loc.Longitude, loc.Latitude); LogView.Add($"lonlat: ({loc.Longitude}, {loc.Latitude}), distance: {distance}m"); + if (double.IsNaN(distance)) + { + IsEnabled = false; + } } logger.Trace("Location Service Positioning Success (lon: {0}, lat: {1})", loc.Longitude, loc.Latitude); }