简体   繁体   English

如何清除Bing地图中的所有图钉,之后仍然可以添加更多图钉?

[英]How can I clear all the pushpins from my Bing Map and still be able to add more after that?

I can add a bunch of pushpins to my map, then add more, than add more (etc.), but if I want to "start over" and call this: 我可以在地图上添加一堆图钉,然后添加更多而不是添加更多(等等),但是如果我想“重新开始”并调用它:

photraxMap.Children.Clear();

(photraxMap is the name of my Bing Map), it does indeed clear all the pushpins, but subsequent attempts to add pushpins fail. (photraxMap是我的Bing地图的名称),确实确实清除了所有图钉,但是随后添加图钉的尝试失败。

That is to say, it fails visually . 也就是说,它在视觉上会失效。 Stepping through the code, the pushpins are created, they just don't display on the map. 逐步执行代码,即可创建图钉,但图钉不会显示在地图上。

Each time I add pushpins, this code is used: 每次添加图钉时,都会使用以下代码:

private async void Gener8MapMarkers(IEnumerable<PhotraxBaseData> _pbd)
{
    MapLayer ml = new MapLayer();
    App.dynamicMapLayers.Add(ml);
    photraxMap.Children.Insert(0, ml);
      . . .

..yet, once the map has been cleared, it won't show pushpins any longer. ..但是,一旦地图被清除,它将不再显示图钉。

Is it because the design-time map layer also get cleared? 是因为设计时地图层也被清除了吗? That is, this one: 也就是说,这个:

<bm:Map x:Name="photraxMap"  Credentials="MyOtherPetIsADuckbilledPlatypus" Margin="0,0,0,0" MapType="Birdseye" >
        <bm:Map.Children>
            <!-- Data Layer-->
            <bm:MapLayer Name="DataLayer"/>

?

If so, how can I prevent it from being cleared with the call to photraxMap.Children.Clear(); 如果是这样,如何防止通过调用photraxMap.Children.Clear();清除它? or how can I resurrect it? 或我该如何复活?

UPDATE 更新

It must be a problem with the map not refreshing or something. 地图不刷新或其他问题一定是有问题的。 Just as I can step through the code 正如我可以逐步执行代码

and see the pushpins being created, but then don't see them on the map (after clearing the map), so it is too with this code to clear the pushpins: 并查看正在创建的图钉,但随后在地图上(在清除地图后)看不到它们,因此使用以下代码清除图钉也是如此:

private void ClearMap()
{
    var pushPins = FindVisualChildren<Pushpin>(photraxMap);
    foreach (var pushPin in pushPins)
    {
        photraxMap.Children.Remove(pushPin);
    }
    . . .

public static IEnumerable FindVisualChildren(DependencyObject depObj) where T : DependencyObject { if (depObj == null) { yield break; 公共静态IEnumerable FindVisualChildren(DependencyObject depObj),其中T:DependencyObject {如果(depObj == null){产生中断; } }

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

I step through the "photraxMap.Children.Remove(pushPin);" 我逐步完成了“ photraxMap.Children.Remove(pushPin);” which is hit the right amount of times (once for each pushpin), but when it is finished running, I still see the pushpins there. 它被击中了正确的时间(每个图钉一次),但是当它运行完毕时,我仍然在那里看到图钉。

At least with this code: 至少使用以下代码:

photraxMap.Children.Clear();

The pushpins do actually disappear (but then disallow any subsequent adding of (visible) pushpins. 图钉实际上已消失(但后来禁止任何后续的(可见)图钉增加。

UPDATE 2 更新2

Don't ask me why, but the pushpins do disappear with this code: 不要问我为什么,但是图钉确实会随着以下代码消失:

var ps = from p in photraxMap.Children
         select p;
var psa = ps.ToArray();
for (int i = 0; i < psa.Count(); i++)
{
    photraxMap.Children.Remove(psa[i]);
}

...which I got from a cat here: How can I remove specified pushpins? ...这是我从猫那里得到的: 如何删除指定的图钉?

Something is still preventing pushpins added subsequently from displaying, though... 仍然有一些东西阻止显示随后添加的图钉,尽管...

This is what works: 这是可行的:

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

I think DataLayer itself was being destroyed previously; 我认为DataLayer本身之前已被破坏; by making sure I only remove Pushpins, it works well. 确保只移除图钉,效果很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM