How can I remove specified pushpins?

 ̄綄美尐妖づ 提交于 2020-01-05 02:52:30

问题


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?


回答1:


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"  />



回答2:


I think this might work:

var pushPins = SOs_Classes.SOs_Utils.FindVisualChildren<Pushpin>(bingMap);
foreach (var pushPin in pushPins)
{
    if (pushPin.Tag is SOs_Locations)
    {
        SOs_Locations locs = (SOs_Locations) pushPin.Tag;
        if (locs.GroupName == groupToAddOrRemove)
        {
            bingMap.Children.Remove(pushPin);
        }
    }
}

// I got this from somebody somewhere, but forgot to note who/where

public static IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject
{
    if (depObj == null)
    {
        yield break;
    }

    for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
    {
        var child = VisualTreeHelper.GetChild(depObj, i);
        if (child != null && child is T)
        {
            yield return (T)child;
        }

        foreach (var childOfChild in FindVisualChildren<T>(child))
        {
            yield return childOfChild;
        }
    }
}


来源:https://stackoverflow.com/questions/13907333/how-can-i-remove-specified-pushpins

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!