画鋲をBingマップに動的に追加します。また、特定のものを削除したいと思います(Tagプロパティに埋め込まれた値に基づいて)。これを行うには、画鋲のMapOverlayを特定する必要がありますか?その場合、どのように対処すればよいですか?
2728 次
2 に答える
2
どの環境について話しているのかわかりませんが、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" />
于 2012-12-29T05:25:19.297 に答える
1
私はこれがうまくいくと思います:
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;
}
}
}
于 2012-12-24T23:31:17.057 に答える