Search code examples
wpfvb.netgeocodingbing-mapspushpin

Displaying multiple pushpins on Bing Maps WPF


Currently when I load my program Bing Maps will only load the first pushpin onto the map, for my example I have 4 pushpins which should be displayed when the application is loaded, what additional code would I add in order to make it complete all four.

In addition I have a couple of questions if you don't mind answering

Do I need to use a loop for each location?

Do I have to give each one an individual name? (Pin)

Can I link a access database instead of copying the locations across?

Is it possible to hide or remove pushpins when a button is clicked?

 Dim Pin = New Microsoft.Maps.MapControl.WPF.Pushpin()
    UserControl11.BingMap.Children.Add(Pin)

    Pin.Location = (New Location(55.852663, -2.3889276))
    Pin.Location = (New Location(55.956023, -3.1607265))
    Pin.Location = (New Location(54.840279, -3.2886766))
    Pin.Location = (New Location(52.819511, -1.8851815))

Solution

  • If it is just these 4 pins you want to create, then you can use the following code:

    Dim Pin = New Microsoft.Maps.MapControl.WPF.Pushpin()
    Pin.Location = (New Location(55.852663, -2.3889276))
    UserControl11.BingMap.Children.Add(Pin)
    
    Dim Pin2 = New Microsoft.Maps.MapControl.WPF.Pushpin()
    Pin2.Location = (New Location(55.956023, -3.1607265))
    UserControl11.BingMap.Children.Add(Pin2)
    
    Dim Pin3 = New Microsoft.Maps.MapControl.WPF.Pushpin()
    Pin3.Location = (New Location(54.840279, -3.2886766))
    UserControl11.BingMap.Children.Add(Pin3)
    
    Dim Pin4 = New Microsoft.Maps.MapControl.WPF.Pushpin()
    Pin4.Location = (New Location(52.819511, -1.8851815))
    UserControl11.BingMap.Children.Add(Pin4)
    

    Alternatively, if your location data is changing or you have an array/list of location information you can loop through, create pushpins and add them to the map like this:

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