我动态地将图钉添加到必应地图。我还想删除某些(基于其 Tag 属性中嵌入的值)。为此,我是否需要识别图钉的地图叠加层,如果是这样,我该怎么做?
我不确定你在说哪个环境,但它看起来不是 Windows 8。
所以这里有一些Windows Phone 7.1的代码。这假定地图的子集合中只有图钉。如果您还有其他 UI 元素,则必须在转到 Tag 属性;)之前筛选出这些元素
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 如下所示:
<my:Map HorizontalAlignment="Stretch" Name="map1" VerticalAlignment="Stretch" />
我认为这可能有效:
var pushPins = SOs_Classes.SOs_Utils.FindVisualChildren<Pushpin>(bingMap);
foreach (var pushPin in pushPins)
{
if (pushPin.Tag is SOs_Locations)
{
SOs_Locations locs = (SOs_Locations) pushPin.Tag;
if (locs.GroupName == groupToAddOrRemove)
{
bingMap.Children.Remove(pushPin);
}
}
}
我从某个地方的某个人那里得到了这个,但忘记注意谁/在哪里
public static IEnumerable<T> FindVisualChildren<T>(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;
}
}
}