The Bing Maps directions task launches the built-in Maps application and displays driving directions between two points. A start and an end point can be specified using the task’s Start
and End
properties, which are of type LabeledMapLocation
. If only one location is specified, the user’s current location is used in place of the unspecified location.
Note
At least one of the Start
or End
properties must be specified, or an InvalidOperationException
is thrown when you call the BingMapsDirectionsTask.Show
method.
Both Start
and End
properties allow you to specify a string label and geographic coordinates indicating the latitude and longitude of the location. If you omit the geographic coordinates, the Maps application searches for the label string.
The following example demonstrates how to launch the Maps application to search for two locations.
BingMapsDirectionsTask bingMapsDirectionsTask
= new BingMapsDirectionsTask();
...
bingMapsDirectionsTask.Start = new LabeledMapLocation("geneva", null);
bingMapsDirectionsTask.End = new LabeledMapLocation("london", null);
bingMapsDirectionsTask.Show();
To specify geographic coordinates, insert a using statement for System.Device.Location at the top of your class. A GeoCoordinate
can then be passed to the task as shown:
GeoCoordinate geoCoordinate = new GeoCoordinate(46.25829, 6.20966);
bingMapsDirectionsTask.Start
= new LabeledMapLocation("Geneva", geoCoordinate);
bingMapsDirectionsTask.Show();
By specifying the geographic coordinates, the Maps application places a label at the location using the supplied text.
The sample for this section allows the user to enter the start and end search terms, and to launch the BingMapsDirectionsTask
via an application bar button.
The sample can be found in the LaunchersAndChoosers/BingMaps directory of the WPUnleashed.Examples project.
The BingMapsDirectionsTaskViewModel
class contains two string properties, which are used as search terms for a BingMapsDirectionsTask
(see Listing 14.1).
An ICommand
, named SearchCommand
, initializes the BingMapsDirectionsTask
and calls its Show
method.
public class BingMapsDirectionsTaskViewModel : ViewModelBase
{
readonly BingMapsDirectionsTask bingMapsDirectionsTask
= new BingMapsDirectionsTask();
public BingMapsDirectionsTaskViewModel() : base("get directions")
{
searchCommand = new DelegateCommand(arg => ShowTask());
}
void ShowTask()
{
bingMapsDirectionsTask.Start
= new LabeledMapLocation(startLocation, null);
bingMapsDirectionsTask.End
= new LabeledMapLocation(endLocation, null);
bingMapsDirectionsTask.Show();
}
readonly DelegateCommand searchCommand;
public ICommand SearchCommand
{
get
{
return searchCommand;
}
}
string startLocation;
public string StartLocation
{
get
{
return startLocation;
}
set
{
Assign(ref startLocation, value);
}
}
string endLocation;
public string EndLocation
{
get
{
return endLocation;
}
set
{
Assign(ref endLocation, value);
}
}
}
The BingMapsDirectionsTaskView
page contains two TextBox
controls that are bound to the viewmodel string properties, as shown:
<StackPanel x:Name="ContentPanel" Grid.Row="1">
<TextBlock Text="start location"
Style="{StaticResource LabelTextStyle}" />
<TextBox Text="{Binding StartLocation, Mode=TwoWay}" />
<TextBlock Text="end location"
Style="{StaticResource LabelTextStyle}" />
<TextBox Text="{Binding EndLocation, Mode=TwoWay}" />
</StackPanel>
In addition, the view contains an AppBarIconButton
that is used to execute the viewmodel’s SearchCommand
. See the following excerpt:
<u:AppBar>
<u:AppBarIconButton
Command="{Binding SearchCommand}"
Text="Search"
IconUri="/LaunchersAndChoosers/BingMaps/Images/AppBarSearch.png" />
</u:AppBar>
Figure 14.1 shows the view before launching the Bing Maps directions task.
Tapping the Search button launches the built-in Maps application (see Figure 14.2).
The Bing Maps directions task is a simple way to launch the built-in Maps application. Chapter 18, “Incorporating Map-Based Positioning,” shows how to build similar capabilities directly into your app, allowing you to completely customize how direction information is presented.
18.188.109.205