我有这段代码:
DataLayer.Children.Clear();
...从设计时MapLayer中删除图钉,声明如下:
<bm:Map Credentials="CuellarDobsonMcNallyPalmer420s" x:Name="photraxMap" Margin="0,0,0,0" >
<bm:Map.Children>
<!-- Data Layer-->
<bm:MapLayer Name="DataLayer"/>
. . .
但我还以编程方式创建了其他MapLayers:
MapLayer mapLayerToUse = DataLayer;
if (Cre8DynamicMapLayer)
{
mapLayerToUse = new MapLayer();
mapLayerToUse.Name = String.Empty;
photraxMap.Children.Insert(0, mapLayerToUse);
}
...那么如何清除这些动态创建的MapLayers呢?
我可以做一些事情,例如给动态地图图层随机名称,将它们存储在列表中,然后遍历该列表,在它们上面调用Children.Clear(在找到具有该名称的MapLayer之后),但我想象必须有一种比这更简单,更优雅的方式。有人知道吗?
// App.xaml.cs
sealed partial class App : Application
{
public static List<MapLayer> dynamicMapLayers { get; set; }
. . .
protected override void OnLaunched(LaunchActivatedEventArgs e)
{
. . .
dynamicMapLayers = new List<MapLayer>();
//MainPage.xaml.cs (update of previously shown code):
MapLayer mapLayerToUse = DataLayer;
if (Cre8DynamicMapLayer)
{
mapLayerToUse = new MapLayer();
mapLayerToUse.Name = String.Empty;
dynamicMapLayers.Add(mapLayerToUse); // <= this is the new line
photraxMap.Children.Insert(0, mapLayerToUse);
}
// Finally, insted of doing all that looping as shown in my proposed answer, simply do this instead:
foreach (MapLayer ml in dynamicMapLayers)
{
ml.Children.Clear();
}
......你的想法是什么?
答案 0 :(得分:1)
我希望会有一些非常简洁和精辟的东西,但可能毕竟我必须做这样的事情:
// Adapted the following from http://www.codeease.com/only-remove-pushpins-from-a-layer-in-bing-map.html
private void ClearPushpins()
{
List<UIElement> elementsToRemove = new List<UIElement>();
List<UIElement> pushpinToRemove = new List<UIElement>();
foreach (String photoset in App.CurrentlyMappedPhotosets)
{
foreach (UIElement element in photraxMap.Children)
{
if (element.GetType() == typeof(MapLayer))
{
MapLayer Lay = (MapLayer)element;
if (Lay.Name == photoset)
{
foreach (UIElement p in Lay.Children)
{
if (p.GetType() == typeof(Pushpin))
{
pushpinToRemove.Add(p);
}
}
foreach (UIElement pin in pushpinToRemove)
{
Lay.Children.Remove(pin);
}
elementsToRemove.Add(Lay);
}
}
foreach (UIElement e in elementsToRemove)
{
photraxMap.Children.Remove(e);
}
}
}
}
答案 1 :(得分:1)
如果你确实map.Children.Clear它将删除地图上的所有MapLayers,包括动态添加的MapLayers。如果你想清除每个MapLayer而不删除它,那么你必须递归遍历MapLayer中的所有项目并检查项目是否是MapLayer,因为MapLayers可以有子MapLayer。如果一个项目不是MapLayer,那么删除它,如果是,则递归地通过子MapLayer。
答案 2 :(得分:1)
您可以使用功能删除
删除地图图层 int number_of_layers = map.Layers.Count;
for (var i = 0; i < number_of_layers; i++)
{
map.Layers.RemoveAt(0);
}