Integrating Bing Maps with WPF applications

A little background info

Windows Presentation Foundation (WPF) is a UI framework for building rich Windows client apps that can be standalone or browser-hosted. As is the case with Windows 8 Store Apps, WPF also seeks to separate the appearance of a user interface from its behaviour. The appearance is specified in XAML and the behaviour is implemented in a managed programming language like C# or Visual Basic. If you are not already familiar with WPF or are getting started, I recommend having a look at this first.

The Bing Maps WPF Control already contains all the standard features we already expect from a Bing Maps control such as Map Styles (Road, Aerial and Hybrid), shapes (pins, polylines and polygons) and map navigation with pan and zoom keyboard controls. All we then need to do is download the control and add it to our application. 

Sample WPF application

One of the most common features of a mapping application is the ability to track a user’s location and build some functionality around that, like bringing the user more relevant information based on his/her location. An example would be an app that tracks a user’s current location then displays restaurants that are closest to the user. Both Windows 8 and Windows Phone 8 have an inbuilt geolocation component that gets you location information from the Windows Location Provider (using Wi-Fi triangulation and IP address data) or it may come from other sources such as a GPS device.

Most modern browsers can also handle location-aware browsing using a public geolocation service provider. How about a WPF application? There are multiple ways of approaching this problem. In the sample app, we use, a public geolocation provider, to get our location then display this on a map. (Note: the provider’s data in this scenario might not be accurate for some regions, but it will give us an idea of how to integrate Bing Maps into a WPF application)

So let’s get started on our application:

  1. Download the Bing Maps WPF control from this link.
  2. Using Visual Studio, create a new Windows WPF Application (in this case we shall be using C# for the code behind)
  3. On the project menu, select add reference and click on browse on the subsequent dialog box
  4. Browse to the location of the Bing Maps WPF Control installation select the Microsoft.Maps.MapControl.WPF.dll file from the libraries folder. 
Add the map control assembly to MainWindow.xaml of your project by inserting the following code:
Our UI is pretty basic: a control that holds our actual map, the zooming buttons and a button, which when clicked, gets my location and displays it on the map. (Remember to replace the BING_MAPS_KEY placeholder with the actual key you got from the Bings Maps Portal). By default, the map centres on Kigali, Rwanda at startup.
The get-my-location button click method contains standard Bing Maps code that adds a pushpin to a map indicating our location based on the coordinates returned by the geolocation service provider APIs.
We are now ready to run our application …

NB: The full project is attached on this blog post. 

Comments (2)

  1. xy says:

    Location myloc = new Location(Convert.ToDouble(_myGeocodeInfo[8]), Convert.ToDouble(_myGeocodeInfo[9]));

    Sorry,I want to know what's it meaning?

  2. shakeelsoft says:

    Hi John  !

    I want to develop a desktop application in C# (preferably WinForm) which will give me the distance travelled within a country through Bing Maps i.e. if the route crosses a country border then I want to know the real distance in each country.

    I am trying to find any solution that can calculate distance between point A and point B, but including information about country’s border. I need this information in order to calculate the price of trip, as price per km in one country might be different from price per km in other country.

    I want to calculate the number of miles a vehicles travel in each country over a period of time. For trips taken over the period, the user will create routes on the map, and the program should be able to generate some output like: Country A: 100 miles, Country B: 200 miles and Country C: 150 miles etc.

    I did some search in and found very useful stuff but not exactly what I am looking for.

    I am newbie to Bing Maps, could you please help me. Could you please point to a working sample app (sample might be Silverlight, WPF etc)?

    Would it be required to add a SQL database with the outlines of country borders (example of source:

    Thanks in advance


Skip to main content