【问题标题】:How to give image to a pushpin dynamically in c#如何在 C# 中动态地将图像提供给图钉
【发布时间】:2012-07-23 14:49:30
【问题描述】:

我有一个场景,我希望用户在 wp7 应用程序中查看 Bing 地图中的多个图钉。我使用 maplayer 制作图钉集群,但我无法在 cs 文件本身中动态地将图像添加到该图钉。顺便说一句,我没有在 xaml 中使用过图钉控件。我只是在循环时将图钉对象添加到地图播放器。

这是我的代码:

maplayer layer = new maplayer();

watcher.start();

for (int i = 0; i < lst.count; i++)

           {                  
                    Pushpin mypin = new Pushpin();
                    watcher.Position.Location.Latitude = Convert.ToDouble(lst[i].Latitude);
                    watcher.Position.Location.Longitude=Convert.ToDouble(lst[i].Longitude);

                }


                GeoCoordinate geo = new GeoCoordinate(watcher.Position.Location.Latitude, watcher.Position.Location.Longitude);
                mypin.Location = geo;

                mypin.Background = new SolidColorBrush(Colors.Gray);
                mypin.Foreground = new SolidColorBrush(Colors.White);
                mypin.Content = "My location";
                layer.AddChild(mypin, mypin.Location);
            }
            map1.SetView(watcher.Position.Location, Status == true ? 5.0 : 3.0);
            map1.Children.Add(layer);


watcher.stop();

我也尝试使用图像画笔属性为图钉提供图像源,但图钉本身不可见。

像这样:

ImageBrush ib = new ImageBrush();

ib.ImageSource = new System.Windows.Media.Imaging.BitmapImage(new Uri(@"Images/push.png", UriKind.Relative));

mypin.Background = ib; 

请帮助我。我需要在不从 xaml 端更改/添加数据模板到图钉的情况下完成此操作。

【问题讨论】:

    标签: windows-phone-7 xaml bing-maps pushpin


    【解决方案1】:

    这个问题在 MSDN 中,在 Working With Pushpins 的页面上进行了介绍。这是给出的示例,其中图像直接添加到地图上的图层:

    namespace WindowsPhoneApplication1
    {
        public partial class MainPage : PhoneApplicationPage
        {
            MapLayer imageLayer;
    
    
            public MainPage()
            {
                InitializeComponent();
    
                //Create a layer to contain the pushpin images.
                imageLayer = new MapLayer();
                map1.Children.Add(imageLayer);
            }
    
    
            private GeoCoordinate mapCenter;
    
            private void button1_Click(object sender, RoutedEventArgs e)
            {
    
                // Retrieve the center of the current map view.
                mapCenter = map1.Center;
    
                // Define the image to use as the pushpin icon.
                Image pinImage = new Image();
    
                //Define the URI location of the image.
                pinImage.Source = new System.Windows.Media.Imaging.BitmapImage(new Uri("bluepushpin.png", UriKind.Relative));
    
                //Define the image display properties.
                pinImage.Opacity = 0.8;
                pinImage.Stretch = System.Windows.Media.Stretch.None;
    
                // Put the image at the center of the view.
                PositionOrigin position = PositionOrigin.Center;
                imageLayer.AddChild(pinImage, mapCenter, position);
    
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2013-01-27
      • 1970-01-01
      • 2020-05-26
      • 2016-06-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-05
      • 1970-01-01
      相关资源
      最近更新 更多