Placing Text on a WPF Bing Maps Map Control

I’ve been working with the Bing Maps map control for WPF recently and doing some fascinating geo-location UI that I can’t imagine doing with any other technology. A couple of days ago a colleague of mine asked me how to place some text on a WPF Bing Maps map. Amazingly, even though I’d done all kinds of sophisticated arcs and other UI, I’d never had the need to simply place some text on a WPF map. After some experimentation, here is one simple approach I came up with:

static void PlaceText(Map map, string text, Location location,
  Color fontColor, double fontSize)
  System.Windows.Controls.Label label =
    new System.Windows.Controls.Label();
  label.Content = text;
  label.Foreground = new SolidColorBrush(fontColor);
  label.FontSize = fontSize;
  MapLayer.SetPosition(label, location);

Essentially the idea is to just programmatically place a Label control onto the map. Calling this helper method onto a WPF Bing Maps map control named ‘myMap’ could resemble:

Location textLocation = myMap.Center;
string text = "Some short text";
PlaceText(myMap, text, textLocation, Colors.Yellow, 25.0);

Instead of a Label control, if you need a bit more flexibility, you can use a TextBlock control:

System.Windows.Controls.TextBlock tb = new 
tb.Text = text;
tb.Foreground = new SolidColorBrush(fontColor);
tb.FontSize = fontSize;
MapLayer.SetPosition(tb, location);

When WPF was first released I wasn’t too impressed, but the Bing Maps map control for WPF is the killer app for me. I’ve tried lots of geo-location UI tools and technologies, but the Bing Maps WPF map control is by far the best I’ve used. The combination of WPF and Bing Maps is one of my favorite technologies.

This entry was posted in Software Test Automation. Bookmark the permalink.