更改图钉WPF的图像

Use*_*238 2 c# wpf bing-maps pushpin

我有2个图钉,pin1和pin2.如何将默认黑色图像更改为我自己的图像?请帮忙.谢谢

Leo*_*die 8

如果你不需要所有的图钉功能,你可以使用a Image并将其添加到a MapLayer.

例:

MapLayer mapLayer = new MapLayer();
Image myPushPin = new Image();
myPushPin.Source = new BitmapImage(new Uri("YOUR IMAGE URL",UriKind.Relative));
myPushPin.Width = 32; 
myPushPin.Height = 32;
mapLayer.AddChild(myPushPin, <LOCATION_OF_PIN>, PositionOrigin.Center);
bingMap.Children.Add(mapLayer);
Run Code Online (Sandbox Code Playgroud)

如果您确实需要某些Pushpin功能,另一个选项是使用PushPin模板:

Pushpin pushpin = new Pushpin();
pushpin.Template = Application.Current.Resources["PushPinTemplate"]  
    as (ControlTemplate);
Run Code Online (Sandbox Code Playgroud)

然后在您的应用程序资源XAML中,您可以像这样定义模板:

<ControlTemplate x:Key="PushPinTemplate">
    <Grid>
        <Rectangle Width="32" Height="32">
            <Rectangle.Fill>
               <ImageBrush BitmapSource="YOUR IMAGE URL" /> 
            </Rectangle.Fill>
        </Rectangle>
    </Grid>
</ControlTemplate>
Run Code Online (Sandbox Code Playgroud)

  • 我使用 mapLayer 为我的代码添加了上面提到的代码,但它没有在 Map 上显示任何图钉,有什么建议为什么它不起作用? (2认同)