0
votes

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))
1
You're only four times setting the Location of a single Pushpin. Create four Pushpins instances instead of just one, and add them all to the map. - Clemens
So should I just copy the two top lines of code with different names 4 times or is there a short hand method? - Gaudreau95
Try something. That's the way to learn programming. - Clemens
I mean I understand simply giving each pushpin a name e.g. pin pin2 etc... but that would involve me having two lines of code for each, surely there is a simpler way of containing multiple name or self assigning instead of having two of Dim Pin = New Microsoft.Maps.MapControl.WPF.Pushpin() UserControl11.BingMap.Children.Add(Pin) for each? - Gaudreau95
Eventually you may want to use a MapItemsControl with a Pushpin in its ItemTemplate, and bind its ItemsSource property to a collection of view model objects with a Location property. The WPF way. - Clemens

1 Answers

1
votes

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