如何删除指定的图钉? [英] How can I remove specified pushpins?
问题描述
我将图钉动态添加到Bing地图.我还想删除某些内容(基于其Tag属性中嵌入的值).为此,我是否需要标识图钉的MapOverlay,如果是,我应该怎么做?
I add pushpins dynamically to a Bing map. I also want to remove certain ones (based on a value embedded in their Tag property). In order to do this, do I need to identify the pushpin's MapOverlay, and if so, how might I go about that?
推荐答案
我不确定您所讨论的环境是什么,但看起来好像不是Windows 8.
I am not sure which environment you are talking about, but it looks like its not windows 8.
因此,这是Windows Phone 7.1的一些代码.假设您的地图的子级集合中只有图钉.如果您还有其他UI元素,则必须先过滤掉这些元素,然后再使用Tag属性;)
So here is some code for windows phone 7.1. This assumes that you only have Pushpins in your map's children collection. If you also have other UI elements you would have to filter those out before going for the Tag property ;)
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:
The map1 is defined in the apps main page; XAML like this:
<my:Map HorizontalAlignment="Stretch" Name="map1" VerticalAlignment="Stretch" />
这篇关于如何删除指定的图钉?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!