Windows Phone 7 - Jogging Application Part 2


In an earlier blog, I produced a very basic application for Windows Phone 7 that allowed joggers to track their progress of a training session on a Bing Map. The application simply plotted a map polyline whenever there was a change in the device’s location, with the changes in the devices location being raised as events by the GeoCoordinateWatcher class, which is part of the Windows Phone 7 API.

Now although the user’s training session was plotted on the map, with the start and end points being identified with pushpins on the map, the application didn’t indicate to the user the total distance travelled, so to obtain this I decided to use the Route Service, which is one of the Bing Maps Web Services.

Route Service
As the name suggests, the route service is an API used to calculate routes between designated start and end points.

In order to use the Route service, a service reference to http://dev.virtualearth.net/webservices/v1/routeservice/routeservice.svc?wsdl needs to be added to the project.
Please note that currently you are unable to add a service reference from a Windows Phone 7 project within the Visual Studio 2010 release candidate, although this functionality does work correctly within Visual Studio 2010 Express for Windows Phone, so you can simply add the reference there, then reopen the project from within Visual Studio.

Upon adding the reference to the service, the RouteServiceClient becomes available. Now to calculate a route you would typically just pass to the RouteServiceClient the details of the start and end points for your route, now in my case this is still the case, but I also pass in the location details of the jogger’s entire route. This then ensures that the Route Service will use exactly the same route as that used by the jogger.

To pass in the route details to the client, you create an instance of the RouteRequest class. This class has a WayPoints property, which is a collection that holds instances of WayPoint classes, with a WayPoint class simply acting as a container for a single location, represented by longitude and latitude.

Once all of the Way Points have been set, you simply pass the RouteRequest to the CalculateRouteAsync method of the RouteServiceClient. Once the asynchronous call to the web service is complete a RouteResponse is returned, and this details all of the directions of the route (represented as RouteLegs), as well the total distance travelled.

Now although the distance travelled seemed accurate to me, I needed to verify that the route returned by the route service, was identical to each of the locations that I had passed to it. To do this I simply iterated through each of the returned RouteLegs and overlaid them as a new Map Polyline on my Bing Map. Seeing this overlay on the map verified that the data returned was as expected.

private void CalculateRoute()
{
RouteRequest routeRequest = new RouteRequest();
ApplicationIdCredentialsProvider mapCredentials = (ApplicationIdCredentialsProvider)Application.Current.Resources["MapCredentials"];
string credentialID = mapCredentials.ApplicationId;
routeRequest.Credentials = new Credentials() { ApplicationId = credentialID };
Waypoint[] waypoints = new Waypoint[_joggingSession.Locations.Count];
for (int i = 0; i < waypoints.Length; i++)
{
Waypoint waypoint = new Waypoint();
waypoint.Location = new Location(_joggingSession.Locations[i].Latitude, _joggingSession.Locations[i].Longitude);
waypoints[i] = waypoint;
}

routeRequest.Waypoints = waypoints;
RoutingService.RouteServiceClient routeService = new RouteServiceClient("BasicHttpBinding_IRouteService");
routeService.CalculateRouteCompleted += new EventHandler(routeService_CalculateRouteCompleted);
routeRequest.Options = new RouteOptions();
routeRequest.Options.Mode = TravelMode.Walking;
routeRequest.UserProfile = new UserProfile() { DistanceUnit = DistanceUnit.Mile };
routeService.CalculateRouteAsync(routeRequest);
}

void routeService_CalculateRouteCompleted(object sender, CalculateRouteCompletedEventArgs e)
{
RouteResponse routeResponse = e.Result;
_joggingSession.DistanceTravelled = routeResponse.Result.Summary.Distance;
DistanceTravelled.Text = _joggingSession.DistanceTravelled.ToString("000.00") + " miles";
//verfiy distance travelled by overlaying the returned route on top of the real time route.
foreach (RouteLeg routeLeg in routeResponse.Result.Legs)
{
_routePolyLine.Locations.Add(routeLeg.ActualStart);
_routePolyLine.Locations.Add(routeLeg.ActualEnd);
}
}

Comments

  1. Another great post! I look forward to the next installments.

    ReplyDelete
  2. Nice!!! I'll use your Jogging app when I have my first WP7 ;-)

    I'd like to talk about your app on my Spanish blog about Windows Phone 7. What do you think?

    ReplyDelete
  3. Hi Edwin,

    Feel free to discuss the app anytime that you like.

    Andy

    ReplyDelete
  4. Thanks!!! Do you have Twitter?

    I'm following you by RSS :-)

    ReplyDelete
  5. Hey Andy,
    I was wondering if you have tried building this app under the newly-released CTP April Refresh of the Windows Phone 7 SDK. I am running into several issues regarding the Bing Maps Silverlight control and I just wanted to know if I was the only one.

    Thanks!

    ReplyDelete
  6. Ryan, the Bing Maps task has been removed in the latest CTP. See the release notes.

    ReplyDelete
  7. Hi Ryan,

    Apologies for the delay in getting back to you, ive just been swamped the last few days.

    Sly is correct, the Bing Maps task has been removed from the CTP refresh for some strange reason. Im going to see if there's a work around, and when I know anymore then Ill post an update.

    Cheers
    Andy

    ReplyDelete
  8. This comment has been removed by the author.

    ReplyDelete
  9. Hi guys,

    So what are we supposed to do as Bing Maps is currently unavailable in this CTP?
    Any news when they will release new CTP?
    Is there any way to go back to the March one cause I personally can't find it?

    ReplyDelete
  10. Think you can share the source code for this project?

    ReplyDelete
  11. Hi
    I am doing a project on using Windows Azure services in a WP 7 app. I was going through your Jogger app. I tried it but having lot of errors. This is my first time with the SDK. I was hoping if you could provide me with the source code so that i could study how to implement these services?

    ReplyDelete

Post a Comment

Popular posts from this blog

Windows Phone 7 - Selecting device photos without PhotoChooserTask

Windows Phone 7 CTP April Refresh - Bing Maps Silverlight Control