none
How to use Render Transform in Bing map pushpin in code behind RRS feed

  • Question

  • Hi have a Windows 8 Metro style Application which uses a Bing map. The map has several pushpins to locate different locations. Now for adjusting the zoomlevel I am using the following code in XAML.

    <bi:Pushpin  x:Name="locationpushpin"

    RenderTransform="{Binding ZoomLevel, ElementName=mymap, Converter={StaticResource PushpinScaleTransform}}">

          </bi:Pushpin>

    But this works for a single pushpin. When I need to create n number of pushpins depending on my requirement. so I want to create this pushpin in my code Behind(C#).I am facing problem in setting the RenderTransform part. Please help..

    Wednesday, September 12, 2012 7:46 AM

Answers

  • The Windows 8 controls do not support binding to the ZoomLevel property at this time. The XAML you using shouldn't work and testing it myself it doesn't appear to work. When binding to this value is supported you will be able to do this from code behind using code like this:

    private void AddPushpins()
    {
        Random rand = new Random();
        Windows.UI.Xaml.Data.Binding binding = new Windows.UI.Xaml.Data.Binding();
        binding.Source = MyMap;
        binding.Path = new PropertyPath("ZoomLevel");
        binding.Converter = new PushpinScaleConverter();
                
        for (int i = 0; i < 100; i++)
        {
            //Generate random location
            Location l = new Location(rand.NextDouble() * 180 - 90, rand.NextDouble() * 360 - 180);
            Pushpin p = new Pushpin();
            MapLayer.SetPosition(p, l);
                    
            //p.SetBinding(Pushpin.RenderTransformProperty, binding);
            MyMap.Children.Add(p);
        }
    }
    public class PushpinScaleConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, string culture)
        {
            double zoom = (double)value;
            //Powerlaw scaling 
            //http://www.soulsolutions.com.au/Blog/tabid/73/EntryId/599/Powerlaw-scaling-Pushpin-in-the-Virtual-Earth-Silverlight-control.aspx
            double scale = Math.Pow(0.05 * (zoom + 1), 2) + 0.01;
            if (scale > 1) scale = 1;
            if (scale < 0.125) scale = 0.125;
            ScaleTransform pushpinsScaleTransform = new ScaleTransform()
            {
                CenterX = 0,
                CenterY = 0,
                ScaleX = scale,
                ScaleY = scale
            };
            return pushpinsScaleTransform;
        }
        public object ConvertBack(object value, Type targetType, object parameter, string culture)
        {
            return null;
        }
    }


    http://rbrundritt.wordpress.com


    Friday, September 14, 2012 10:50 AM