Finding and Mapping a Route in Windows Phone 8
This is a support post for the Inside Windows Phone show on location and mapping in Windows Phone 8 published here.
One of the really cool and powerful things in the Windows Phone mapping services is the ability to simple hand the Windows Phone APIs a list of geo coordinates and have it create walking or driving route on the fly. Because of the power of offline mapping in Windows Phone 8, this means that developers can create extremely powerful directional software that works even when the phone doesn’t have network connectivity.
The first thing we need to do is set up a map control on which to display our resulting route.
In XAML:
<maps:Map x:Name="myMap" />
Then we’ll set up a list of geocoordinates representing the order of the places we want to use to establish our route and add some locations to it.
List<GeoCoordinate> wayPoints = new List<GeoCoordinate>();
wayPoints.Add(new GeoCoordinate(47.60887, -122.34094));
wayPoints.Add(new GeoCoordinate(47.6396, -122.1300));
Then set up our RouteQuery object, assign an event handler so we can read the result, choose between a route for walking or driving and then assign the list of geo coordinates and we’re off!
RouteQuery routeQuery = new RouteQuery();
routeQuery.QueryCompleted += routeQuery_QueryCompleted;
routeQuery.TravelMode = TravelMode.Walking;
routeQuery.Waypoints = wayPoints;
routeQuery.QueryAsync();
The RouteQuery will work for a little while and then return with (what is hopefully) a suitable route made of
void routeQuery_QueryCompleted(object sender, QueryCompletedEventArgs<Route> e)
{
if (null == e.Error)
{
Route MyRoute = e.Result;
}
}
The result will be a Route object that gives an estimated duration for travelling the route as well as an enormous amount of detailed data about how to get from point A to point B (and then, subsequently, points C, D, and E). The instructions between each point are kept in the Legs property of the Route and each leg has every street and every turn detailed in the Maneuvers property. It’s really easy to work with.
But even better than that is the fact adding the route to your map is only 1 line of code (but I like to use 3 to make things easier):
Route MyRoute = e.Result;
MapRoute mappedRoute = new MapRoute(MyRoute);
scavangeMap.AddRoute(mappedRoute);
scavangeMap.SetView(mappedRoute.Route.BoundingBox);
What I’ve done here is add the UI to represent this route to my map and then moved and scaled the map so that it is in the perfect spot for the user to start interacting with the route.
Comments
Anonymous
February 10, 2014
I need test this code, you can send me this source code? plz seba.romero1@hotmail.com tnxAnonymous
March 10, 2014
Hi, Can I get this test code too. Please send it to me to aptsiauri.ilia@outlook.com tnxAnonymous
May 15, 2014
Hi, im sorry but can i get this test code? Please send this source code to fasakshinta@gmail.com thanksAnonymous
May 20, 2014
Hi, can you please send the source code to nadiya.sakka@gmail.com ? ThanksAnonymous
October 18, 2014
The comment has been removedAnonymous
October 26, 2014
source code to sandeepkuriakose@ymail.comAnonymous
November 15, 2014
Source code to kakajd22@gmail.com thanksAnonymous
March 03, 2015
Hi,Can I get this code to leelaranichilukoti@gmail.comAnonymous
March 03, 2015
Hi,can u please send the source code to leelaranichilukoti@gmail.com Thanks in advanceAnonymous
March 07, 2015
Hi,can u please send the source code to 1205965e@gmail.com Thanks in advance!!Anonymous
July 16, 2015
Thanks for this post, it is very useful.Anonymous
August 26, 2015
Hello, Please send the source code to siddhartha.sarva@gmail.comAnonymous
February 14, 2016
Please send the source code to tungphien.cn08@gmail.com. Many thanksAnonymous
March 04, 2016
Please send the source code to satyag@sattvica.com