为了更好地理解(GMapMarker)标记,让我们看一下它们是如何添加的。
设置标记的(UIElement)形状,传递以下内容:
- MainWindow实例
- 坐标(等等)
- ToolTipText
到(UserControl)CustomMarkerDemo的构造函数。
private void addMarker_Click(object sender, RoutedEventArgs e)
{
GMapMarker marker = new GMapMarker(currentMarker.Position);
{
...
marker.Shape = new CustomMarkerDemo(this, marker, ToolTipText);
marker.ZIndex = combobox.SelectedIndex;
}
MainMap.Markers.Add(marker);
}
在
示例中,我使用了
ComboBox
的
SelectedIndex
来设置新标记的
ZIndex
。正如您所看到的,标记被添加到
(ObservableCollection)MainMap.Markers
中。当我们不需要这些标记时,删除/过滤集合中的标记是很诱人的。但是这种方法的问题在于,当从视图中移除
(UIElement)Shape
时,它们会被处理并需要重新构建。这会导致不希望出现的行为,特别是如果您想能够同时显示所有覆盖层。
相反,我选择仅通过用户交互将标记添加到集合中,并根据当前的combobox.SelectedIndex
设置Shape
的Visibility
。
private void combobox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ComboBox combobox = sender as ComboBox;
if (combobox != null && MainMap != null)
{
if (combobox.SelectedIndex == combobox.Items.Count - 1)
{
foreach (GMapMarker marker in MainMap.Markers)
marker.Shape.Visibility = Visibility.Visible;
}
else
{
foreach (GMapMarker marker in MainMap.Markers)
{
if (marker.ZIndex == combobox.SelectedIndex)
marker.Shape.Visibility = Visibility.Visible;
else
marker.Shape.Visibility = Visibility.Collapsed;
}
}
currentMarker.Shape.Visibility = Visibility.Visible;
}
}
我只链接了一个最基本的设置,需要注意的是这些库中还有更多功能。