Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WPF and WinUI: Improve mouse-move behavior in OfflineRouting sample #1490

Merged
merged 3 commits into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ public partial class OfflineRouting
// Route task and parameters.
private RouteTask _offlineRouteTask;
private RouteParameters _offlineRouteParameters;
private bool _parametersChangedSinceLastSolve = false;
private Task<RouteResult> _lastSolveTask = null;

// List of travel modes, like 'Fastest' and 'Shortest'.
private List<TravelMode> _availableTravelModes;
Expand Down Expand Up @@ -149,51 +151,75 @@ private void ResetRoute()

private async Task UpdateRoute(TravelMode selectedTravelMode)
{
try
// Create a list of stops.
List<Stop> stops = new List<Stop>();

// Add a stop to the list for each graphic in the stops overlay.
foreach (Graphic stopGraphic in _stopsOverlay.Graphics)
{
// Create a list of stops.
List<Stop> stops = new List<Stop>();
Stop stop = new Stop((MapPoint)stopGraphic.Geometry);
stops.Add(stop);
}

// Add a stop to the list for each graphic in the stops overlay.
foreach (Graphic stopGraphic in _stopsOverlay.Graphics)
{
Stop stop = new Stop((MapPoint)stopGraphic.Geometry);
stops.Add(stop);
}
if (stops.Count < 2)
{
// Don't route, there's no where to go (and the route task would throw an exception).
return;
}

if (stops.Count < 2)
{
// Don't route, there's no where to go (and the route task would throw an exception).
return;
}
// Configure the route parameters with the list of stops.
_offlineRouteParameters.SetStops(stops);

// Configure the route parameters with the list of stops.
_offlineRouteParameters.SetStops(stops);
// Configure the travel mode.
_offlineRouteParameters.TravelMode = selectedTravelMode;

// Configure the travel mode.
_offlineRouteParameters.TravelMode = selectedTravelMode;
// Check if a route calculation is already in progress
if (_lastSolveTask == null || _lastSolveTask.IsCompleted)
{
// No calculation in progress, start a new one
await SolveRouteAndDisplayResultsAsync();
}
else
{
// Route calculation already in progress, flag for recalculation when current task completes
_parametersChangedSinceLastSolve = true;
}
}

// Solve the route.
RouteResult offlineRouteResult = await _offlineRouteTask.SolveRouteAsync(_offlineRouteParameters);
private async Task SolveRouteAndDisplayResultsAsync()
{
do
{
try
{
// Start the route calculation
_lastSolveTask = _offlineRouteTask.SolveRouteAsync(_offlineRouteParameters);

// Clear the old route result.
_routeOverlay.Graphics.Clear();
// Reset the flag immediately after starting the task.
// This ensures that any parameter changes made after this point will be caught for the next iteration.
_parametersChangedSinceLastSolve = false;

// Get the geometry from the route result.
Polyline routeGeometry = offlineRouteResult.Routes.First().RouteGeometry;
// By awaiting here, we allow the UI to remain responsive while route calculation is in progress.
// Any changes to parameters during this await will have set _parametersChangedSinceTaskRun to true.
RouteResult offlineRouteResult = await _lastSolveTask;

// Note: symbology left out here because the symbology was set once on the graphics overlay.
Graphic routeGraphic = new Graphic(routeGeometry);
// Clear the old route result.
_routeOverlay.Graphics.Clear();

// Display the route.
_routeOverlay.Graphics.Add(routeGraphic);
}
catch (Exception e)
{
Debug.WriteLine(e);
ShowMessage("Couldn't update route", "There was an error updating the route. See debug output for details.");
_selectedStopGraphic = null;
}
// Get the geometry from the route result.
Polyline routeGeometry = offlineRouteResult.Routes.First().RouteGeometry;

// Note: symbology left out here because the symbology was set once on the graphics overlay.
Graphic routeGraphic = new Graphic(routeGeometry);

// Display the route.
_routeOverlay.Graphics.Add(routeGraphic);
}
catch (Exception e)
{
Debug.WriteLine(e);
}
} while (_parametersChangedSinceLastSolve); // Recalculate if parameters changed during execution
}

private async Task AddStop(Point tappedPosition)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ public partial class OfflineRouting
// Route task and parameters.
private RouteTask _offlineRouteTask;
private RouteParameters _offlineRouteParameters;
private bool _parametersChangedSinceLastSolve = false;
private Task<RouteResult> _lastSolveTask = null;

// Track the graphic being interacted with.
private Graphic _selectedStopGraphic;
Expand Down Expand Up @@ -148,51 +150,75 @@ private void ResetRoute()

private async Task UpdateRoute(TravelMode selectedTravelMode)
{
try
// Create a list of stops.
List<Stop> stops = new List<Stop>();

// Add a stop to the list for each graphic in the stops overlay.
foreach (Graphic stopGraphic in _stopsOverlay.Graphics)
{
// Create a list of stops.
List<Stop> stops = new List<Stop>();
Stop stop = new Stop((MapPoint)stopGraphic.Geometry);
stops.Add(stop);
}

// Add a stop to the list for each graphic in the stops overlay.
foreach (Graphic stopGraphic in _stopsOverlay.Graphics)
{
Stop stop = new Stop((MapPoint)stopGraphic.Geometry);
stops.Add(stop);
}
if (stops.Count < 2)
{
// Don't route, there's no where to go (and the route task would throw an exception).
return;
}

if (stops.Count < 2)
{
// Don't route, there's no where to go (and the route task would throw an exception).
return;
}
// Configure the route parameters with the list of stops.
_offlineRouteParameters.SetStops(stops);

// Configure the route parameters with the list of stops.
_offlineRouteParameters.SetStops(stops);
// Configure the travel mode.
_offlineRouteParameters.TravelMode = selectedTravelMode;

// Check if a route calculation is already in progress
if (_lastSolveTask == null || _lastSolveTask.IsCompleted)
{
// No calculation in progress, start a new one
await SolveRouteAndDisplayResultsAsync();
}
else
{
// Route calculation already in progress, flag for recalculation when current task completes
_parametersChangedSinceLastSolve = true;
}
}

// Configure the travel mode.
_offlineRouteParameters.TravelMode = selectedTravelMode;
private async Task SolveRouteAndDisplayResultsAsync()
{
do
{
try
{
// Start the route calculation
_lastSolveTask = _offlineRouteTask.SolveRouteAsync(_offlineRouteParameters);

// Solve the route.
RouteResult offlineRouteResult = await _offlineRouteTask.SolveRouteAsync(_offlineRouteParameters);
// Reset the flag immediately after starting the task.
// This ensures that any parameter changes made after this point will be caught for the next iteration.
_parametersChangedSinceLastSolve = false;

// By awaiting here, we allow the UI to remain responsive while route calculation is in progress.
// Any changes to parameters during this await will have set _parametersChangedSinceTaskRun to true.
RouteResult offlineRouteResult = await _lastSolveTask;

// Clear the old route result.
_routeOverlay.Graphics.Clear();
// Clear the old route result.
_routeOverlay.Graphics.Clear();

// Get the geometry from the route result.
Polyline routeGeometry = offlineRouteResult.Routes.First().RouteGeometry;
// Get the geometry from the route result.
Polyline routeGeometry = offlineRouteResult.Routes.First().RouteGeometry;

// Note: symbology left out here because the symbology was set once on the graphics overlay.
Graphic routeGraphic = new Graphic(routeGeometry);
// Note: symbology left out here because the symbology was set once on the graphics overlay.
Graphic routeGraphic = new Graphic(routeGeometry);

// Display the route.
_routeOverlay.Graphics.Add(routeGraphic);
}
catch (Exception e)
{
Debug.WriteLine(e);
ShowMessage("Couldn't update route", "There was an error updating the route. See debug output for details.");
_selectedStopGraphic = null;
}
// Display the route.
_routeOverlay.Graphics.Add(routeGraphic);
}
catch (Exception e)
{
Debug.WriteLine(e);
}
} while (_parametersChangedSinceLastSolve); // Recalculate if parameters changed during execution
}

private async Task AddStop(Point tappedPosition)
Expand Down
Loading