如何删除指定的图钉?

时间:2012-12-17 01:43:57

标签: c# bing-maps pushpin

我动态地将图钉添加到Bing地图中。我还想删除某些(基于其Tag属性中嵌入的值)。为了做到这一点,我是否需要识别图钉的MapOverlay,如果是这样,我该怎么做呢?

2 个答案:

答案 0 :(得分:2)

我不确定你在谈论哪个环境,但看起来它不是Windows 8。

所以这里有一些Windows Phone 7.1的代码。这假设您在地图的子集合中只有Pushpins。如果您还有其他UI元素,则必须在转到Tag属性之前对其进行过滤;)

        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]);
        } 

map1在apps主页面中定义; XAML是这样的:

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

答案 1 :(得分:1)

我认为这可行:

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);
        }
    }
}

//我从别人那里得到了这个,但忘了记下谁/哪里

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;
        }
    }
}