代码之家  ›  专栏  ›  技术社区  ›  Gaudreau95

如何向Bing Maps WPF图钉添加信息

  •  2
  • Gaudreau95  · 技术社区  · 10 年前

    我希望在我的每个图钉上添加一个自定义的信息标签,当点击或光标滑过它时,它会出现。这对我来说是第一次,我想知道我将如何制作一个非常基本的程序,因为我目前使用VB的代码布局,我宁愿使用VB而不是XAML。

    目前我正在使用循环在地图上实现多个图钉。

    Dim CountyLocations(3) As Location
    CountyLocations(0) = New Location(55.852663, -2.3889276)
    CountyLocations(1) = New Location(55.956023, -3.1607265) 
    CountyLocations(2) = New Location(54.840279, -3.2886766) 
    CountyLocations(3) = New Location(52.819511, -1.8851815)
    
    For index = 0 to  CountyLocations.Length - 1
    Dim Pin = New Microsoft.Maps.MapControl.WPF.Pushpin()
    Pin.Location =  CountyLocations(index)
    UserControl11.BingMap.Children.Add(Pin)
    Next
    

    Q1.如何在图钉上创建基本信息选项卡(参考资料)

    Q2.我将如何添加到当前代码中?

    1 回复  |  直到 8 年前
        1
  •  3
  •   Dave    10 年前

    是的,我就是这么想的。只需使用 ToolTips 。也许你必须适应 Location 的字符串表示形式转换为所需的坐标格式。这是一个完整的工作示例。

    XAML:

    <Window x:Class="MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:wpf="clr-namespace:Microsoft.Maps.MapControl.WPF;assembly=Microsoft.Maps.MapControl.WPF"
        Title="MainWindow" Height="350" Width="525">
        <Grid>
            <wpf:Map x:Name="myMap" ZoomLevel="5" CredentialsProvider="My Key" 
                     Mode="Aerial" Center="55.852663, -2.3889276"></wpf:Map>
        </Grid>
    </Window>
    

    代码隐藏:

    Imports Microsoft.Maps.MapControl.WPF
    
    Class MainWindow
    
        Public Sub New()
            InitializeComponent()
            CreatePushPinsWithToolTip()
        End Sub
    
        Private Sub CreatePushPinsWithToolTip()
            Dim CountyLocations(3) As Location
            CountyLocations(0) = New Location(55.852663, -2.3889276)
            CountyLocations(1) = New Location(55.956023, -3.1607265)
            CountyLocations(2) = New Location(54.840279, -3.2886766)
            CountyLocations(3) = New Location(52.819511, -1.8851815)
    
            For index = 0 To CountyLocations.Length - 1
                Dim Pin = New Microsoft.Maps.MapControl.WPF.Pushpin()
    
                Dim CoordinateTip As ToolTip = New ToolTip()
                CoordinateTip.Content = CountyLocations(index).ToString
    
                Pin.Location = CountyLocations(index)
                Pin.ToolTip = CoordinateTip
                myMap.Children.Add(Pin)
            Next
        End Sub
    
    End Class
    

    现在,如果您将鼠标悬停在 PushPin 将显示带有坐标的工具提示。

    enter image description here

    编辑: 要显示不同的名称,只需创建 Strings 并将其用于创建 Pushpins 和他们的 工具提示 。下面是一个示例:

    Private Sub CreatePushPinsWithToolTip()
        Dim CountyLocations(3) As Location
        CountyLocations(0) = New Location(55.852663, -2.3889276)
        CountyLocations(1) = New Location(55.956023, -3.1607265)
        CountyLocations(2) = New Location(54.840279, -3.2886766)
        CountyLocations(3) = New Location(52.819511, -1.8851815)
    
        Dim names = New String() {"sam", "tom", "leon", "eddy"}
    
        For index = 0 To CountyLocations.Length - 1
            Dim Pin = New Microsoft.Maps.MapControl.WPF.Pushpin()
    
            Dim CoordinateTip = New ToolTip()
            CoordinateTip.Content = names(index)
    
            Pin.Location = CountyLocations(index)
            Pin.ToolTip = CoordinateTip
            myMap.Children.Add(Pin)
        Next
    End Sub