In the Bing Maps Silverlight control there is a Pushpin object that you can use to mark locations on the map. Unfortunately there is little you can do as far as customizations. Changing the icon image is nearly impossible. But not all is lost, Microsoft made it so that we can add UIElements to the map. This gives us the ability to add any kind of UIElement to the map as a pushpin if we wanted. For those who are migrating from the AJAX version of Bing Maps there is a gap in how pushpins are created. In this blog post I’ll explain how I’ve gone about creating a set of tools that gives you the ability to create custom pushpins in a similar manner as it was done in the AJAX control.
In the AJAX control we had the VECustomIconSpecification class for creating custom pushpins. We can create a similar CustomIconSpecific class that consists of a subset of the properties that are in the VECustomIconSpecification. In the following code you may notice that you can specify an Uri or an UIElement. This allows you to create a custom pushpin by either specifying an Uri to an image or to create a UIElement. Specifying an Uri is similar to using the VECustomIconSpecification.Image property, where as using an UIElement is like using the VECustomIconSpecification.CustomHTML property.
| CustomIconSpecification.cs |
|
using System; namespace BingCustomPushpins //Default properties #endregion #region Public Properties /// <summary> /// <summary> /// <summary> /// <summary> /// <summary> /// <summary> /// <summary> /// <summary> /// <summary> #endregion |
We can then create a UserControl that uses this CusomtIconSpecification class to generate a UIElement that we can use as a pushpin. We will call this class CustomPushpin. The xaml will be pretty basic and will contain a single Grid object which we will use to add our pushpin icon and custom text too.
| CustomPushpin.xaml |
|
<UserControl x:Class="BingCustomPushpins.CustomPushpin" |
For the code behind to this UserControl we will create three public properties; IconSpecification, location, and MetaData. The IconSpecification will be used to create the pushpin. When this property is set the pushpin content is created. If a UIElement is specified in the specification it will be used as the Icon, otherwise an image will be created from the IconUri if one is specified. Note that a PushpinTools class is used to generate the Image from the IconUri. We will look into this later. If TextContent is specified in the specification a TextBlock is generated and added on top of the pushpin Icon. The Location property is used to specify where this pushpin is to be placed. The MetaData property allows you to store any information that is relevant to this location. This is useful if you want to store a location ID so that you can retrieve details about this location.
| CustomPushpin.xaml.cs |
|
using System; namespace BingCustomPushpins private CustomIconSpecification _iconSpec; #endregion #region Constructor /// <summary> #endregion #region Public Properties /// <summary> PushpinBase.Children.Clear(); if (value.Icon != null) if (!string.IsNullOrEmpty(value.TextContent)) /// <summary> /// <summary> #endregion |
The following PushpinTools class gives you two useful methods for generating common pushpin UIElements; image pushpins, and circle pushpins.
| PushpinTools.cs |
|
using System; namespace BingCustomPushpins /// <summary> |
As a final step it would be useful if we had an easy way to add these custom pushpins to a map layer. We can create a simple extension to the MapLayer class to do this.
| BingMapsExtensions.cs |
|
using System; namespace BingCustomPushpins |
I’ve put these classes together into a reusable class library so that you can easily add this functionality to your applications. In order to use this class simply add a reference to the dll or project. I’ve put together a sample application that shows how you can use these tools. You can download the complete code with samples here: http://cid-e7dba9a4bfd458c5.skydrive.live.com/self.aspx/VE%20Sample%20code/CustomPushpins.zip
Here is a screen shot of the sample application with two different types of pushpins.

#1 by greg on March 17, 2011 - 8:21 pm
This is great! The zip file seems corrupt though. Can you send me the whole library? Much appreciated!!
#2 by Susan Hernandez on April 20, 2011 - 9:01 pm
Thank you very much for this complete solution. I needed the example to help me get a custom configurable pushpin going.
Cheers!
#3 by Anonymous on October 20, 2011 - 9:18 pm
Why didn’t you just create a custom control template and apply it to the pushpin?
#4 by rbrundritt on October 20, 2011 - 9:46 pm
The ideas was to create a class similar to the Custom Pushpins in Bing Maps V6.3 as that’s what developers are used to. This made it more familar for people who are already Bing Maps developers.