In this article we are going to see how to use the new Maps Direction Task in your windows phone application development which was introduced in Windows Phone 8. To get some basics about maps please refer to my previous article on How to use Maps in Windows Phone application. Maps Direction task is one of the new Launcher that was introduced in Windows Phone 8 which helps to launch the Maps and display the driving direction between two coordinates or end points. This task will be very much used in cases to track a driving direction from your current location to an end point or vice versa. The Start and End point will be the GeoCoordinates (Lat and Long) or use the label to specify the place to search far. [more]
Let us see the steps on how to achieve this task in our Windows Phone application development. Open Visual Studio 2012 IDE and create a new Windows Phone project with a valid project name as shown in the screen below.
Clicking on OK will create the project and the solution with the list of default files and folders that are required to run the application. It will take some time to create these files based on your system configuration, so once everything is ready we can see the Visual Studio IDE with the project as shown in the screen below.
Now add some control to trigger the Map Direction task and to show the direction, basically we will add a button alone to trigger the task. The task will open the default Map application and take the input point (Start and End) points and show the direction directly on the maps.
XAML Code:
[code:c#]<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
<TextBlock x:Name="ApplicationTitle" Text="F5debug How to Series" Style="{StaticResource PhoneTextNormalStyle}"/>
<TextBlock x:Name="PageTitle" Text="Map Direction" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
</StackPanel>
<!–ContentPanel – place additional content here–>
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<Button Content="Get Direction to California" HorizontalAlignment="Left" Margin="30,10,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.434,0.523" Width="416" Click="Button_Click" Height="138"/>
</Grid>
Now in the button click event write the below code which creates an instance of the MapDirection task and assign the end point for the direction required to be shown in the Map application as shown in the code below.
C# Code:
[code:c#]using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using F5debugHowto75.Resources;
using Microsoft.Phone.Tasks;
using System.Device.Location;
namespace F5debugHowto75
{
public partial class MainPage : PhoneApplicationPage
{
// Constructor
public MainPage()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
MapsDirectionsTask mapsDirectionsTask = new MapsDirectionsTask();
LabeledMapLocation myloc = new LabeledMapLocation("California", null);
mapsDirectionsTask.End = myloc;
mapsDirectionsTask.Show();
}
}
}
Now we are done with the code, just run the application by pressing F5 directly from the keyboard or we can use the Build and execute the project option from the tool bar to run the application. Once the Build is successful we can see the Windows Phone emulator with the application and the expected outputs as shown in the screen below.