Search code examples
c#bing-mapspushpin

How can I remove specified pushpins?


I add pushpins dynamically to a Bing map. I also want to remove certain ones (based on a value embedded in their Tag property). In order to do this, do I need to identify the pushpin's MapOverlay, and if so, how might I go about that?


Solution

  • I am not sure which environment you are talking about, but it looks like its not windows 8.

    So here is some code for windows phone 7.1. This assumes that you only have Pushpins in your map's children collection. If you also have other UI elements you would have to filter those out before going for the Tag property ;)

            Pushpin t1 = new Pushpin();
            t1.Tag = "t1";
            map1.Children.Add(t1);
    
            Pushpin t2 = new Pushpin();
            t2.Tag = "t2";
            map1.Children.Add(t2);
    
            Pushpin t3 = new Pushpin();
            t3.Tag = "t3";
            map1.Children.Add(t3);
    
            // LINQ query syntax
            var ps = from p in map1.Children 
                     where ((string)((Pushpin)p).Tag) == "t1" 
                     select p;
            var psa= ps.ToArray();
            for (int i = 0; i < psa.Count();i++ )
            {
                map1.Children.Remove(psa[i]);
            }
            // or using method syntax
            var psa2= map1.Children.Where(y => ((string)((Pushpin)y).Tag) == "t2").ToArray();
            for (int i = 0; i < psa2.Count(); i++)
            {
                map1.Children.Remove(psa2[i]);
            } 
    

    The map1 is defined in the apps main page; XAML like this:

        <my:Map  HorizontalAlignment="Stretch"  Name="map1" VerticalAlignment="Stretch"  />