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

拟墨画扇 提交于 2021-01-28 17:01:53

问题


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.

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

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

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


回答1:


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; by making sure I only remove Pushpins, it works well.



来源:https://stackoverflow.com/questions/26940692/how-can-i-clear-all-the-pushpins-from-my-bing-map-and-still-be-able-to-add-more

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