Resizing and Auto-Scaling Pushpin in Bing Maps Silverlight

4. June 2010

Plotting pushpins on the Bing Maps Silverlight control is really simple when using the Pushpin control that comes with the control. But, what if you want to change the size of the Pushpin? It doesn't work to just change the Pushpin.Height and Pushpin.Width properties. This is actually because those properties pertain to the controls Content property. So, how exactly do you go about changing the size of the Pushpin if the Height and Width properties don't work?

Use ScaleTransform to Resize Pushpins

The answer is simple. All you need to do is use a ScaleTransform. Below is a couple examples, one making the pushpin smaller and the other bigger.

<m:Pushpin Location="0,-15">
    <m:Pushpin.RenderTransform>
        <ScaleTransform ScaleX=".5" ScaleY=".5" CenterX="17" CenterY="35"></ScaleTransform>
    </m:Pushpin.RenderTransform>
</m:Pushpin>
<m:Pushpin Location="0,15">
    <m:Pushpin.RenderTransform>
        <ScaleTransform ScaleX="2" ScaleY="2" CenterX="17" CenterY="35"></ScaleTransform>
    </m:Pushpin.RenderTransform>
</m:Pushpin>

You may have noticed that not only does the above example set the ScaleX and ScaleY properties of the ScaleTransform, but the CenterX and CenterY are also set. This is because if we don't set the CenterX and CenterY appropriately the Pushpin will get "moved" from its correct location when the Transform is performed.

How did I decide to set CenterX to 17 and CenterY to 35? Well, the Pushpins "default" size sets the Width equal to 34 and Height to 35, and the PositionOrigin to BottomCenter. Since the PositionOrigin is set to BottomCenter, in order to get the pushpin to stay in its correct position on the map we need to set the CenterX to half the Width or 17, and CenterY to the Height or 35.

The image to the right shows an example of this in action.

Auto Scale Pushpin with Map Zoom Level

A neat feature that becomes available with the Bing Maps Silverlight control once you start using ScaleTransforms to modify the size of Pushpins is the ability to change the size of the Pushpins when the Map Zoom Level is changed. All it takes is a little help from a custom IValueConverter and a little Data Binding.

The image to the right shows this in action. Both sides of the image were not resized at all, the difference in size of the Pushpins is due to the usage of the following code.

First, we'll add some Pushpins to a Map and set the Pushpins RenderTransform property binding to bind it to the Map's ZoomLevel property and set it to use a custom converter.

<m:Map Height="200" x:Name="myMap">
    <m:Map.Children>
        <m:Pushpin Location="0,-15"
            RenderTransform="{Binding ZoomLevel, ElementName=myMap, Converter={StaticResource PushpinScaleTransform}}"></m:Pushpin>
        <m:Pushpin Location="0,15"
            RenderTransform="{Binding ZoomLevel, ElementName=myMap, Converter={StaticResource PushpinScaleTransform}}"></m:Pushpin>
    </m:Map.Children>
</m:Map>

Next, make sure you declare the PushpinScaleTransform static resource in the UserControl, Page or App. Also, don't forget to include the namespace, in the below example I named it "local".

<UserControl.Resources>
    <local:PushpinScaleTransform x:Key="PushpinScaleTransform"></local:PushpinScaleTransform>

</UserControl.Resources>

Finally, add the PushpinScaleTransform custom IValueConverter object to the project. This custom IValueCoverter will recieve the ZoomLevel and "convert" it to a ScaleTransform with ScaleX, ScaleY and CenterX, CenterY properties set appropriately to Auto Scale the Pushpin with the Map ZoomLevel. Below is an example of this that I found works quite well.

using System;
using System.Windows.Data;
using System.Windows.Media;

namespace SLBingMapsScalePushpins
{
    public class PushpinScaleTransform : IValueConverter
    {
        #region IValueConverter Members

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            double currentZoomLevel = (double)value;

            // Calculate the scale to use. This is just a simple algorithm that
            // I found works nicely.
            var scaleVal = (0.05 * (currentZoomLevel + 1)) + 0.3;

            var transform = new ScaleTransform();
            transform.ScaleX = scaleVal;
            transform.ScaleY = scaleVal;

            // Set the transform center X and Y so the Pushpin stays at the correct location.
            // The Default Pushpin's height is 35 and width is 34
            // Since the Default Pushpin's PositionOrigin is set to BottomCenter, we need to
            // set the CenterX to half the width (17), and CenterY to the height (35).
            transform.CenterX = 17;
            transform.CenterY = 35;

            return transform;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}

Download Code

Here's a link to download a small Silverlight project that implements the above code examples.

SLBingMapsScalePushpins.zip (446.52 kb)

Bing Maps, Silverlight ,

Comments

6/6/2010 7:00:22 PM #
Pingback from sciencereportnow.com

Resizing and Auto-Scaling Pushpin in Bing Maps Silverlight | Science Report | Biology News, Economics News, Computer Science News, Mathematics News, Physics News, Psychology News
6/7/2010 3:25:47 AM #
Took me time to go through all of the feedback, however I actually loved the article. It proved to be very useful to me and I am certain to all the commenters right here as well! It's all the time nice when you cannot solely be told, but additionally entertained! I am certain you had enjoyed penning this blog post.
6/7/2010 9:34:40 AM #
Well written article, I am a big time fan of your site, keep up the superb work, and I will be a regular visitor for a very long time.
6/7/2010 12:32:54 PM #
Well written article, I am a avid fan of your site, keep up the superb work, and I will be a regular visitor for a very long time.
6/8/2010 4:35:27 AM #
I am sharing this with my spouse. Maybe this will get him interested with me again. Things have been a little boring lately, hopefully this will change that.
6/8/2010 8:41:52 AM #
solid post , really good view on the subject and very well written, this certainly has put a spin on my day, many thanks from the USA and observe up the good work
6/8/2010 11:43:55 AM #
Hey just posting to say your page is not working right for me. i'm using chrome it could be me though as there are other sites that appear broken to me too.
6/8/2010 1:17:52 PM #
I wish everyone would do this, it would make life so much easier.
6/8/2010 2:19:04 PM #
Thank you because I find this fairly engaging. I've quite liked reading through your writing. Purely enlightening what you have created here.
6/9/2010 10:06:59 AM #
solid post , really good view on the subject and very well written, this certainly has put a spin on my day, many thanks from the USA and observe up the good work
6/9/2010 10:48:59 AM #
Where can I purchase senuke?
6/9/2010 12:49:54 PM #
Hi, I found your blog in a directory of blogs. I dont know how your blog came up, must have been a typo, Your blog looks good. Have a nice day.
6/9/2010 8:13:03 PM #
Hello guys, excellent site, very rich in content and correctly carefully thought out, personally I found here much interesting and useful
6/9/2010 8:45:59 PM #
Thanks for this amazing article. I am refreshed after reading this. Thank you!
6/10/2010 4:11:24 AM #
Hi, I found your blog in a directory of blogs. I dont know how your blog came up, must have been a typo, Your blog looks good. Have a nice day.
6/10/2010 3:06:21 PM #
Easy to read, easy to understand. Very rare to find.
6/10/2010 4:20:30 PM #
By far the most concise and up to date information I found on this topic. Sure glad that I navigated to your page by accident. I’ll be subscribing to your feed so that I can get the latest updates. Appreciate all the information here
6/10/2010 4:57:12 PM #
Indian takeaway London delivers you authentic Indian food, piping hot at your doorstep and you can relish the mouth-watering dishes at the luxury of your home while watching your favorite sports on TV. You can order from a wide variety of vegetarian and non-vegetarian dishes which you can enjoy with wide variety of breads.
6/10/2010 4:57:35 PM #
An Indian restaurant London serves you gourmet Indian food cooked to perfection the traditional way using special aromatic Indian spices. You can savour a wide variety of both vegetarian and non-vegetarian fare and some delectable desserts for the perfect ending to your sumptuous meal.
6/10/2010 11:05:24 PM #
Hey this is a great article. I'm going to email this to my buddies. I stumbled on this while googling for some lyrics, I'll be sure to visit regularly. thanks for sharing.
6/11/2010 7:43:29 AM #
Hi i must say very nice blog.
6/11/2010 4:22:01 PM #
Thank you for taking the time to write this brilliant article. I'll be sure to share this with my friends. Thank you!
6/12/2010 3:44:46 AM #
Hello, I found your blog from search engine. And I found one thing also that google is also very accurate in result...I found exactly what I was looking for...Have a nice day.
6/12/2010 3:52:00 AM #
i like your post through out...
6/12/2010 6:46:58 AM #
hi i got a problem when i tried to subscribe to your rss feed. i try it again later regards
6/12/2010 9:39:44 PM #
I cant believe the amount of wonderful info you have on your blog. I have learned a lot from it. Will be coming back soon.
6/12/2010 11:16:12 PM #
I don't believe this! Smile
6/12/2010 11:44:32 PM #
Great post.  I really enjoyed it!
6/13/2010 1:47:47 AM #
I think your post and site is wonderfully constructed. I found it whild doing some research on the topic. You present this in a way not many others see. I hope to be checking back from time to time to see what else you have to say on other topics.
6/13/2010 5:14:03 AM #
Thank you for taking the time to write this brilliant article. I'll be sure to share this with my friends. Thank you!
6/13/2010 6:38:52 AM #
Nice informationa and great template.  I think I would like to get my hands on that template if you have a copy for me that would be nice!  Keep up the posting...