2

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

4

2 回答 2

2

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

所以这里是windows phone 7.1的一些代码。这假设您的地图的子集合中只有图钉。如果您还有其他 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 在应用程序主页中定义;像这样的 XAML:

    <my:Map  HorizontalAlignment="Stretch"  Name="map1" VerticalAlignment="Stretch"  />
于 2012-12-29T05:25:19.297 回答
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;
        }
    }
}
于 2012-12-24T23:31:17.057 回答