如何清除Bing地图中的所有图钉,之后仍然可以添加更多图钉? [英] How can I clear all the pushpins from my Bing Map and still be able to add more after that?

查看:72
本文介绍了如何清除Bing地图中的所有图钉,之后仍然可以添加更多图钉?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以在地图上添加一堆图钉,然后添加更多而不是添加更多(等等),但是如果我想重新开始"并称呼它:

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是我的必应地图的名称),确实确实清除了所有图钉,但是随后添加图钉的尝试失败了.

(photraxMap is the name of my Bing Map), it does indeed clear all the pushpins, but subsequent attempts to add pushpins fail.

也就是说,它在视觉上失败.逐步执行代码,即可创建图钉,但它们不会显示在地图上.

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

?

如果是这样,如何防止通过调用photraxMap.Children.Clear()清除它;或我该如何复活?

If so, how can I prevent it from being cleared with the call to photraxMap.Children.Clear(); or how can I resurrect it?

地图不刷新或某些问题一定是有问题的.正如我可以逐步完成代码

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

公共静态IEnumerable FindVisualChildren(DependencyObject depObj),其中T:依赖对象{如果(depObj == null){产量中断;}

public static IEnumerable FindVisualChildren(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;
        }
    }
}

我逐步完成了"photraxMap.Children.Remove(pushPin);"被按了正确的次数(每个图钉一次),但是当它运行完毕时,我仍然在那看到图钉.

I step through the "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.

至少使用此代码:

photraxMap.Children.Clear();

图钉 do 实际上消失了(但随后禁止再添加(可见的)图钉.

The pushpins do actually disappear (but then disallow any subsequent adding of (visible) pushpins.

不要问我为什么,但是图钉 do 会消失,并显示以下代码:

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...

推荐答案

这是有效的方法:

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

我认为DataLayer本身之前已被破坏;确保只删除图钉,效果很好.

I think DataLayer itself was being destroyed previously; by making sure I only remove Pushpins, it works well.

这篇关于如何清除Bing地图中的所有图钉,之后仍然可以添加更多图钉?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆