Search code examples
c#winformswpf-controlsbing-mapspushpin

How can I delete all existing Pushpins from Bing Maps?


Using a different version/flavor of Bing Maps several years ago, I asked this same question and ultimately self-posted the answer, here.

But that doesn't work with my Winforms app using the WPF control. With this code:

var mapLayerChildren = from c in DataLayer.Children select c;
var kinderGarten = mapLayerChildren.ToArray();
for (int i = 0; i < kinderGarten.Count(); i++)
{
    if (kinderGarten[i] is Pushpin)
    {
        DataLayer.Children.Remove(kinderGarten[i]);
    }
}

...DataLayer is not recognized.

And when I try to replace "DataLayer" with "userControl11.myMap.Children" I get this:

enter image description here

What do I need to do?


Solution

  • It depends to how you have added the pushpins. You need to remove them from the same Children collection to which you have added them.

    Assuming you have added your pushpins directly to the Children collection of the map, then you can find all the pushpins and remove them like this:

    var map = this.userControl11.myMap;
    map.Children.OfType<Pushpin>().ToList().ForEach(pin => {
        map.Children.Remove(pin);           
    });
    

    Or to remove all children of the map, including pushpin, shape, layer, and etc. you can use:

    var map = this.userControl11.myMap;
    map.Children.Clear();