Bing Maps with WPF

Several years ago I looked at using Bing Maps maps in a WPF desktop application. I decided to revisit the technique. The bottom line is that there’s not much new — and that’s good. Bing Maps with WPF is very cool.


First I needed to install the Bing Maps for WPF SDK. I found the SDK (which is really just a DLL plus a help file) by searching The install was quick and easy, and placed the DLL I needed at C: \ Program Files (x86) \ Bing Maps WPF Control \ V1 \ Libraries \ Microsoft.Maps.MapControlWPF.dll.

I already had a Bing Maps key I got from the Bing site. That process was kind of annoying.

Next, I launched VS 2015 and created a new WPF application. After the template code loaded, I added a reference to the Maps DLL.

I resized the MainWindow of the app to 800 x 1400, set the Grid Margin to (300, 0, 0, 0) to give me a 300 pixel space on the left. I added three Button controls and a ListBox control. Then I added the mapping CredentialProvider key that I got from the Bing Maps site.


Most of the key code is listed below.

In my little demo, I have a Button that centers the map to Redmond, WA and sets the Zoom level to 17. I have another Button that places three yellow dots are hard-coded lat-lon in Redmond. I have a third Button that clears all user-added shapes on the map.

I also created a custom double-click handler that places a red dot at the mouse-click position.

The moral is that you can do really powerful map applications with WPF — much easier than wrestling with a Web application.

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Shapes;
using Microsoft.Maps.MapControl.WPF;

namespace WpfWithMap
  /// Interaction logic for MainWindow.xaml
  public partial class MainWindow : Window
    public MainWindow()
      myMap.MouseDoubleClick += map_MouseDoubleClick;

    private void map_MouseDoubleClick(object sender,
      MouseButtonEventArgs e)
      e.Handled = true;
      listBox.Items.Add("Map double-click occurred");
      Point p0 = e.GetPosition(this);  // window position 
      Point p1 = new Point(p0.X - 300, p0.Y);   // map
      Location  loc = myMap.ViewportPointToLocation(p1); 

      listBox.Items.Add("Click window XY = " +
        p0.X + "," + p0.Y);
      listBox.Items.Add("Click map XY = " +
        p1.X + "," + p1.Y);
      listBox.Items.Add("Click lat-lon = " +
        loc.Latitude.ToString("F4") + 
        "," + loc.Longitude.ToString("F4"));
      PlaceDot(loc, Colors.Red);
      listBox.Items.Add("Placing red dot at click");

    private void button_Click(object sender,
      RoutedEventArgs e)
      listBox.Items.Add("Center button clicked");
      listBox.Items.Add("Center at 47.64, -122.14");
      myMap.Center = new Location(47.6421, -122.1420);
      myMap.ZoomLevel = 17.0;

    private void DrawLine(Location start, Location finish)
      LocationCollection coll = new LocationCollection();
      MapPolyline line = new MapPolyline();
      line.Stroke = new SolidColorBrush(Colors.Orange);
      line.StrokeThickness = 3.0;
      line.Locations = coll;

    private void button1_Click(object sender,
      RoutedEventArgs e)
      listBox.Items.Add("Yellow Dots button clicked");
      listBox.Items.Add("Placing yellow dots");
      double[][] data = new double[3][];
      data[0] = new double[] { 111, 47.643, -122.140 };
      data[1] = new double[] { 222, 47.642, -122.140 };
      data[2] = new double[] { 333, 47.641, -122.140 };

      for (int i = 0; i < data.Length; ++i)
        PlaceDot(new Location(data[i][1], data[i][2]),

    private void PlaceDot(Location location,
      Color color)
      Ellipse dot = new Ellipse();
      dot.Fill = new SolidColorBrush(color);
      double radius = 8.0;
      dot.Width = radius * 2;
      dot.Height = radius * 2;
      ToolTip tt = new ToolTip();
      tt.Content = "Location = " + location;
      dot.ToolTip = tt;
      Point p0 = myMap.LocationToViewportPoint(location);
      Point p1 = new Point(p0.X - radius, p0.Y - radius);
      Location loc = myMap.ViewportPointToLocation(p1);
      MapLayer.SetPosition(dot, loc);

    private void button2_Click(object sender,
      RoutedEventArgs e)
      listBox.Items.Add("Clear button clicked");
      int ct = myMap.Children.Count;
      listBox.Items.Add("Clearing " + ct +
        " items from map");
This entry was posted in Machine Learning. Bookmark the permalink.