(X) Hide this
    • Login
    • Join
      • Generate New Image
        By clicking 'Register' you accept the terms of use .

How to extend Bing Maps Silverlight with an elevation profile graph - Part 1

(4 votes)
Walter Ferrari
>
Walter Ferrari
Joined Dec 17, 2009
Articles:   19
Comments:   12
More Articles
6 comments   /   posted on Mar 08, 2010
Categories:   General

This article is compatible with the latest version of Silverlight.

This is part 1 of the series “How to extend Bing Maps Silverlight with an elevation profile graph”.

Introduction

One of the things I found missing in the current Bing Maps product is the possibility to create an elevation surface profile of routes. Perhaps this feature may not seem much on demand but actually affects more people than expected. Think for example about sports events like marathons and cycling races: to see a preview of the elevation profile of the trail would be of great benefit to the participants. But even if you're just simple hikers you might want to know what is the difference in level of your walking or bicycle trip to better understand the effort that it would entail.
So why not try to create this feature from scratch using the Bing Maps Silverlight Control? In this first part we will see how to extend the Bing Maps Silverlight Control by adding new commands to the navigation bar, how to create a route based on a start and end address or by clicking directly on the map and how to obtain elevation data of the route. In the second part we will briefly deal with details about the implementation via threads of the elevation data retrieval and we will show how to add some interactivity to the Chart control of the Silverlight toolkit.
I assume that the reader is already familiar with the basics related to the use of Bing Maps in Silverlight. If not so, I would like to suggest reading the first chapters of the SDK guide or at least the "Getting started" paragraph of the following interesting article already published on SilverlightShow.
In the second part a link to the live demo and to the source code will be made available; in the meantime you can watch a video here of an early beta.

The User Interface

What I wanted to do was to add this type of functionality to the Bing Maps Silverlight Control integrated with the existing interface. With this aim in mind I added 2 items to the navigation bar of the Bing Maps Silverlight Control as you can see on the following image, where the items “Route Profile“ and “Arbitrary Profile” have been added.

Navigation Bar Extended

A click on “Route Profile” item opens a popup window which allows choosing 2 options:

  • Building the route by inserting pushpins on the map
  • Building the route providing a start address and an end address

With the first option you can put a series of pushpins on the map with a simple click of the mouse on the map; a double click starts the route calculation and, after that, the generation of the elevation profile graph. The second option considers first the geocoding of the start and end address of our route and then proceeds in the same way as the first option.
A click on “Arbitrary Profile”, instead, allows you to draw polylines directly on the map; even here a double click of the mouse triggers the generation of the route and the subsequent creation of the elevation profile graph.

The graph offers some kind of interactivity in the way that you can select and move 2 cursors over the points of the profile and see some conclusive data like the average elevation with regard to the portion of the profile between the two cursors.

User Interface

Main points of interest

An interesting element is the description of how it was possible to add new commands to the navigation bar of the Bing Maps Silverlight Control without giving the impression of a last minute addition but of a natural integration. Another point which might be of interest for the reader is the retrieval of the elevation data and the subsequent creation of the profile graph; during the process the user is allowed to interact with the map thanks to the use of threads. Finally, the possibility to interact with the Chart control can offer some ideas for further improvements.

How to How to add items to the navigation bar of the Bing Maps Silverlight Control

The first thing I thought of doing to achieve this objective was to understand how the navigation bar was made. Obviously to better understand that I needed to see the XAML code which generates the navigation bar; but, how to get the XAML code? The setup of the Bing Maps Silverlight Control SDK (which can be downloaded at the following link ) installs two DLL files on your pc : “Microsoft.Maps.MapControl.dll” and “Microsoft.Maps.MapControl.Common.dll”. They contain all the assemblies needed in order to use the Silverlight Bing Maps Control in your applications. While I was looking inside the installation folders in order to find an inspiration at some point I remembered reading somewhere on the web that one could just open the DLL in a text editor and see the XAML code in plaintext. Indeed, if you open the "Microsoft.Maps.MapControl.dll" file and scroll down the content, at a certain point you see a large portion of XAML code and there is where you have to investigate. If you look at the code below which is the part describing how it is organized the superior portion of the navigation bar:

 <StackPanel Grid.Row="0" Grid.Column="3" Orientation="Horizontal" x:Name="HorizontalPanel">
     <StackPanel Orientation="Horizontal" x:Name="HorizontalLeftPanel">
         <navigation:CommandRadioButton x:Name="RoadStyleButton"  />
         <navigation:CommandRadioButton x:Name="AerialStyleButton"  />
     </StackPanel>
     <navigation:CommandSeparator />
     <StackPanel Orientation="Horizontal" x:Name="HorizontalRightPanel">
         <navigation:CommandToggleButton x:Name="LabelsButton" />
     </StackPanel>
 </StackPanel>

You see that there is a StackPanel (called “HorizontalPanel”) which contains two other StackPanels called respectively “HorizontalLeftPanel” and “HorizontalRightPanel”. Inside them, there are the buttons you are used to clicking when you interact with the Bing Maps Silverlight Control, i.e. “Road”, “Aerial” and “Labels”.

So, if you want to add two other commands as in the first image above, in principle you just have to create two instances of the type CommandRadioButton (which is in the assembly Microsoft.Maps.MapControl.Navigation) and add them to the “HorizontalPanel” as children controls. But the reality is somewhat more complicated than just described. In order to add these controls you have to wait for the completion of the initialization phase of some components. This phase, without going too much over details, can be regarded as made up of 2 steps. A first step where the ForegroundMap Control is loaded; this is the control which displays amongst other the map navigation bar on the map. A second step where the navigation bar is loaded. Only after these two phases you can add your controls. In practical terms first you have to add an EventHandler to the TemplateApplied EventHandler of the ForegroundMap Control. When the event is triggered it means that the NavigationBar has been instantiated and you can add another EventHandler this time to the TemplateApplied EventHandler of the NavigationBar. Once this last event is triggered you can add your controls to the NavigationBar. The portion of code below realizes what I have just described.

 ...
 private CommandRadioButton routeBtn = new CommandRadioButton();
 private CommandRadioButton freeProfileBtn = new CommandRadioButton();
 ...
 public MainPage()
 {
     InitializeComponent();
     ...
     ElevationChartMap.MapForeground.TemplateApplied += new EventHandler(MapForeground_TemplateApplied);
     ...
 }
  
 void MapForeground_TemplateApplied(object sender, EventArgs e)
 {
     ElevationChartMap.MapForeground.NavigationBar.TemplateApplied += new EventHandler(NavigationBar_TemplateApplied);
 }
 
 void NavigationBar_TemplateApplied(object sender, EventArgs e)
 {
      NavigationBar navControl = ElevationChartMap.MapForeground.NavigationBar;
  
      navControl.HorizontalPanel.Children.Add(routeBtn);
      navControl.HorizontalPanel.Children.Add(freeProfileBtn);
 }

Creating the route with the “Route Profile” option

For the creation of the route either giving start and end address or inserting some markers on the map I used part of the code samples available on Keith Kinnan’s Blog and presented during the PDC09. I don’t want to repeat what has been already very well explained at the links above so I will stick to a brief description.
First of all, you have to create two service references, the Geocode service and the Route service:

https://staging.dev.virtualearth.net/webservices/v1/routeservice/routeservice.svc?wsdl

https://staging.dev.virtualearth.net/webservices/v1/geocodeservice/geocodeservice.svc?wsdl

Don’t forget the final postfix “?wsdl” which is missing in the related paragraph (“Calculating a Route Using Bing Maps Web Services “) of the SDK Guide version 1.01.
With the first Service you obtain the coordinates of the user location inputs; these coordinates can be then provided to the Route Service to calculate the route. Since Silverlight uses an asynchronous model all the requests to the Services above have to be asynchronous. For instance, after you inserted the start and end address of your route, two asynchronous calls to the Geocode Service are made. To catch them, an EventHandler to the GeocodeCompleted event has been added. Inside this EventHandler we keep track of how many times it was crossed; if it has been twice it means that both start and end address have been elaborated and then it is possible to make the request to the Route Service, where another EventHandler has been added to intercept the CalculateRouteCompleted event. In this EventHandler the result of the calculation is checked and if it is Ok the Polyline representing the route is drawn. In particular an object of type MapPolyline is instantiated and the points in geographic coordinates resulting from the calculation are added to its "Locations" property. This object is then added to a MapLayer which represents a layer over the map and allows to position UI elements according to geographic coordinates. The code below illustrates what I have described.

 double dist = e.Result.Result.Summary.Distance;
 Color routeColor = Colors.Blue;
 SolidColorBrush routeBrush = new SolidColorBrush(routeColor);
                
 //Add the Polyline for the route
 MapPolyline routeLine = new MapPolyline();
 routeLine.Locations = new LocationCollection();
 routeLine.Stroke = routeBrush;
 routeLine.Opacity = 0.65;
 routeLine.StrokeThickness = 5.0;
 foreach (Location loc in e.Result.Result.RoutePath.Points)
 {
     routeLine.Locations.Add(loc);
 }
 profileLayer.Children.Add(routeLine);

“Arbitrary Profile” option: how to allow the user to draw lines directly on the map

The “Arbitrary Profile” option gives the user the chance to draw a completely arbitrary polyline with subsequent mouse clicks; you can either follow the profile of a road or click on whatever you want. A double click ends the drawing mode and launches the generation of the elevation profile graph. To do that two EventHandlers have been added to the Map Control in order to intercept the mouse click and double click on the map. In case of a single click, the ViewportPoint of the MouseEventArgs object is evaluated and the result is added to the Locations Collection of a MapPolyline previously added as a child UI element to the same MapLayer used for route showing. 

 ...
 private MapPolyline arbitraryProfilePolyline;
 ...
  
 void ElevationChartMap_MouseClick(object sender, MapMouseEventArgs e)
   {
      if (myProfileModeMap.EnableArbitraryProfileMode == true)
      {
          arbitraryProfilePolyline.Locations.Add(ElevationChartMap.ViewportPointToLocation(e.ViewportPoint));
      }
      ...

 

Summary

In this first part we have explored some “internals” of the navigation bar of the Bing Maps Silverlight Control and we have given some advice on how to create routes using Geocoding and Route Services and on how to draw lines on the Map and obtain the geographics coordinates of its points. In the next article we will see how to obtain elevation data for the routes and the arbitrary paths we created and how to customize the Chart Control of the Silverlight Toolkit for the needs of this specific application. You will be also able to try a live demo and download the source code.


Subscribe

Comments

  • -_-

    RE: How to extend Bing Maps Silverlight with an elevation profile graph - Part 1


    posted by Carl on Mar 09, 2010 15:33

    Hi

    What a coincidence I was writing a similar app this lunchtime as I want to track elevation for a community bike ride I'm doing. I'm using the routservice and was hoping to use the Altitude from each ItineraryItem within the Routleg collection but it looks like all values are zero. Is this the approach you are proposing to use?

  • walterf

    RE: How to extend Bing Maps Silverlight with an elevation profile graph - Part 1


    posted by walterf on Mar 09, 2010 16:12

    Hi Carl,
    I'm happy to know that someone else is developing a similar application for more or less the same purpose. In fact I was thinking to something for cycling fanatics :-)

    Regarding the elevation I not used the "Altitute" property of the ItineraryItem, because, as I read somewhere on msdn, it is reserved for future use. Instead, I use some free web services which provide elevation data coming from various GDEM  (Global Digital Elevation Model) like ASTAR, STRM ecc.

    The Part 2 of the article should be ready by the end of this week and in there you will find all the details of the method I used.

     

  • -_-

    RE: How to extend Bing Maps Silverlight with an elevation profile graph - Part 1


    posted by Daren May on Mar 10, 2010 18:20
     We (Aspenware) built a very similar solution 18 months or so ago for an activity trackign application we were developing. We also provide it to Ride The Rockies to display the overall race route and the individual legs. For the 2010 Ride The Rockies web site, we upgrade the control to leverage Bing Maps Silverlight control. You can view it here: Ride The Rockies 2010 Route. We too use external sources to cross reference latitude and longitude to obtain altitude information. The Silverlight control for Bing Maps is a powerful control that provides a lot of opportunity for developing interesting applications with great data visualization.
  • -_-

    RE: How to extend Bing Maps Silverlight with an elevation profile graph - Part 1


    posted by Michael on Mar 22, 2010 18:47

    Hi,

    Great article. I've been looking for ages how to add to the Map Mode nav bar. Looking forward to part 2.

    Cheers

  • walterf

    RE: How to extend Bing Maps Silverlight with an elevation profile graph - Part 1


    posted by walterf on Mar 22, 2010 19:01
  • -_-

    RE: How to extend Bing Maps Silverlight with an elevation profile graph - Part 1


    posted by John on Nov 02, 2010 19:41

    Wonder if there is a way to change the color of the buttons added to the navigation bar to make them more visible.  Or just add a button to the right hand corner of the map.

Add Comment

Login to comment:
  *      *       

From this series