Using the Route Calculator

,

The MapViewModel class contains two string properties: FromAddress and ToAddress, which are populated using two TextBox controls in the view.

When the viewmodel’s RouteSearchCommand is executed, the viewmodel’s Message property and a Busy Boolean flag are set, which are materialized using the ProgressIndicator of the page’s System Tray, as shown:

<shell:SystemTray.ProgressIndicator>
    <shell:ProgressIndicator IsIndeterminate="{Binding Busy}"
                                IsVisible="{Binding Busy}"
                                Text="{Binding Message}" />
</shell:SystemTray.ProgressIndicator>

The SearchRoute method then awaits for the result of the RouteCalculator object’s CalculateAsync method (see Listing 18.5).

The result of the route query is a Route object that contains a set of RouteLeg objects, with each containing a set of RouteManeuver objects. For each RouteManeuver object a custom MapItineraryItem is added to the viewmodel’s ItineryItems collection.

Conveniently, the Route object also includes a BoundingBox property of type LocationRectangle that allows you to zoom and pan the map to display the route.

LISTING 18.5. MapViewModel.SearchRoute Method


async void SearchRoute()
{
    if (string.IsNullOrEmpty(fromAddress)
        || string.IsNullOrEmpty(toAddress))
    {
        return;
    }

    Message = "searching...";
    Busy = true;

    RouteCalculationResult calculationResult;

    try
    {
        HideItinerary();
        HideRouteSearch();

        calculationResult = await routeCalculator.CalculateAsync(
            fromAddress, toAddress, center);
    }
    catch (Exception ex)
    {
        MessageService.ShowError(
            "An error occurred while calculating the route.");
        return;
    }
    finally
    {
        Busy = false;
        Message = string.Empty;
    }

    Route route = calculationResult.Route;
    MapRoute = new MapRoute(route);

    var collection = new ObservableCollection<MapItineraryItem>();

    foreach (RouteLeg leg in route.Legs)
    {
        foreach (RouteManeuver maneuver in leg.Maneuvers)
        {
            collection.Add(new MapItineraryItem(maneuver));
        }
    }

    ItineraryItems = collection;
    LocationRectangle = route.BoundingBox;

    ShowItinerary();
}


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.184.155