我一直在互联网上研究如何更改 Bing 地图 C# 控件中的图钉图像。我最接近的是使用以下解决方案:
这并不完全是我想要的,因为我希望能够更改图钉的颜色以及添加标签。
上述解决方案基本上是在图钉上绘制的图像,没有添加标签等附加功能。我希望能够在具有自定义标签功能的同时轻松更改图像。
还有其他方法可以做到这一点吗?另一种选择是使用“标准”Bing 图钉图形并能够更改大小。不过 C# 控件似乎没有此功能
令人惊讶的是,这是 SOF 中关于该主题的唯一问题(另一个已关闭)。对于 WPF 新手(像我一样)来说,很难找到好的且简单的信息,因此我将展示如何在 VB.NET 中以编程方式添加的图钉中使用自定义图像:
这是我的 MainWindow.xaml 文件:
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:m="clr-namespace:Microsoft.Maps.MapControl.WPF;assembly=Microsoft.Maps.MapControl.WPF"
xmlns:local="clr-namespace:MyBingMapsApp"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<ControlTemplate x:Key="PushpinControlTemplate" TargetType="m:Pushpin">
<Grid>
<Rectangle Width="24" Height="24">
<Rectangle.Fill>
<ImageBrush ImageSource= "Resources/marker_green.png"/>
</Rectangle.Fill>
</Rectangle>
</Grid>
</ControlTemplate>
</Window.Resources>
<Grid>
<m:Map x:Name="myMap"
CredentialsProvider= "xxxxxx mycredentialskey xxxxxxxx"
Center="42.13618,-0.40822"
ZoomLevel="15">
</m:Map>
</Grid>
</Window>
如您所见,您必须定义一个
ControlTemplate
,其 TargetType="m:Pushpin"
在那里你可以画任何你需要的东西。最简单的:使用资源中的图像。
重要提示:将图像“构建操作”更改为资源(单击解决方案资源管理器的资源文件夹中的图像,然后在高级设置中更改它)。否则你将不得不硬写图像路径或使用 Uris 或更复杂的东西
现在您可以在任何需要的地方创建图钉并分配模板:
mypin = New Pushpin
mypin.Location = New Location(mylat, mylon)
mypin.ToolTip = "This is a pushpin with custom image"
Dim mytemplate As System.Windows.Controls.ControlTemplate = FindResource(“PushpinControlTemplate”) 'here of course you must put the key name of your template
mypin.Template = mytemplate
这有效 - 创建小的透明 .png 图像 (24 x 24) - 我使用 Gimp - 存储在图像文件夹或资源中
Private Sub PlaceImagePin(location As Location, imageName As String)
Dim bmi As New BitmapImage()
' Begin initialization.
bmi.BeginInit()
' Set properties.
bmi.CacheOption = BitmapCacheOption.OnDemand
bmi.CreateOptions = BitmapCreateOptions.DelayCreation
bmi.DecodePixelHeight = 24
bmi.DecodePixelWidth = 24
bmi.UriSource = New Uri("Images/" & imageName & ".png", UriKind.Relative)
' End initialization.
bmi.EndInit()
Dim brush = New ImageBrush
brush.ImageSource = bmi
Dim pin As New Pushpin
pin.Background = brush
pin.Location = location
PinLayer.Children.Add(pin)
End Sub
最终的图钉放置在地图上提供的位置,图像位于图钉的棒棒糖部分。 Pin 具有所有标准 pin 属性,即内容和工具提示