none
Problem with scaling pushpin

    Question

  • Hi everyone, I managed to scale my pushpin based on the zoom level thanks to this post: http://social.msdn.microsoft.com/Forums/en-US/8821757b-a7a5-49af-aa4a-f37699218d31/how-to-use-render-transform-in-bing-map-pushpin-in-code-behind?forum=bingmapswindows8. However, there is another problem occurred. The pushpin I added to WPF Bing map didn't stick to its location. When I zoomed in/out the map, the pushpin changed its location. Could you help me to fix this issue? Thanks in advance.

    Here is my code:

    DraggablePushpin myPushPin = new DraggablePushpin(); System.Windows.Data.Binding bind = new System.Windows.Data.Binding(); bind.Source = MainMap; bind.Path = new PropertyPath("ZoomLevel"); bind.Converter= new PushpinScaleTransform(); myPushPin.SetBinding(DraggablePushpin.RenderTransformProperty, bind); MapLayer mapLayer = new MapLayer(); mapLayer.AddChild(myPushPin, pinLocation, PositionOrigin.Center); MainMap.Children.Add(mapLayer);

    public class PushpinScaleTransform : IValueConverter
        {
            #region IValueConverter Members

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

                var scale = Math.Pow(0.05 * (currentZoomLevel + 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, System.Globalization.CultureInfo culture)
            {
                throw new NotImplementedException();
            }

            #endregion
        }


    Wednesday, February 05, 2014 5:32 PM

Answers

  • Your issue is that your transform does not account for the PositionOrigin of the pushpin. The PositionOrigin of the pushpin is the offset anchor point of the pin. This value does not scale and has to be converted. Setting the PositionOrigin value to "0,0" does make things a lot better although it is still not perfect. Alternatively you could add a translate transform in addition to the scale transform to offset the position of the Pushpin based on the size of the pushpin.

    http://rbrundritt.wordpress.com

    Wednesday, February 12, 2014 4:00 PM
  • You can use a TransformGroup. The following works pretty nicely:

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        //Pushpin dimensions when not scaled.
        double width = 30, height = 40;
    
        var p = parameter as Pushpin;
        double currentZoomLevel = (double)value;
    
        var scale = Math.Pow(0.05 * (currentZoomLevel + 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
        };
    
        TranslateTransform tt = new TranslateTransform()
        {
            X = -width * scale,
            Y = -height * scale
        };
                
        TransformGroup tg = new TransformGroup();
        tg.Children.Add(pushpinsScaleTransform);
        tg.Children.Add(tt);
    
        return tg;
    }
    I also added this to the Pushpin: PositionOrigin="0,0"

    http://rbrundritt.wordpress.com

    Wednesday, February 12, 2014 5:19 PM

All replies

  • Have you tried it with a normal pushpin instead of a "DraggablePushpin" to see if the scaling issue still happens?
    Thursday, February 06, 2014 6:47 PM
  • I tried with the normal pushpin and the issue still happened. I don't know why the pushpin doesn't stick to its location.
    Thursday, February 06, 2014 7:31 PM
  • Is there anyone knowing how to fix this issue? I greatly appreciate.
    Tuesday, February 11, 2014 7:35 PM
  • Your issue is that your transform does not account for the PositionOrigin of the pushpin. The PositionOrigin of the pushpin is the offset anchor point of the pin. This value does not scale and has to be converted. Setting the PositionOrigin value to "0,0" does make things a lot better although it is still not perfect. Alternatively you could add a translate transform in addition to the scale transform to offset the position of the Pushpin based on the size of the pushpin.

    http://rbrundritt.wordpress.com

    Wednesday, February 12, 2014 4:00 PM
  • Thank you very much for your help. However, I still don't understand how to add a translate transform to the scale transform to offset the position of the Pushpin based on the size of the pushpin. Could you please explain it clearer? Thanks a lot.
    Wednesday, February 12, 2014 4:38 PM
  • You can use a TransformGroup. The following works pretty nicely:

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        //Pushpin dimensions when not scaled.
        double width = 30, height = 40;
    
        var p = parameter as Pushpin;
        double currentZoomLevel = (double)value;
    
        var scale = Math.Pow(0.05 * (currentZoomLevel + 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
        };
    
        TranslateTransform tt = new TranslateTransform()
        {
            X = -width * scale,
            Y = -height * scale
        };
                
        TransformGroup tg = new TransformGroup();
        tg.Children.Add(pushpinsScaleTransform);
        tg.Children.Add(tt);
    
        return tg;
    }
    I also added this to the Pushpin: PositionOrigin="0,0"

    http://rbrundritt.wordpress.com

    Wednesday, February 12, 2014 5:19 PM
  • Thank you very much. It works great!
    Wednesday, February 12, 2014 5:40 PM