GMap.Net Wpf 中的叠加

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30922211/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 13:12:59  来源:igfitidea点击:

Overlays in GMap.Net Wpf

c#wpfgmap.net

提问by givo

I am building an application that needs a tool for rendering a geographic map, in addition the application needs the tool to provide a way for adding custom overlays.

我正在构建一个应用程序,该应用程序需要一个用于渲染地理地图的工具,此外该应用程序还需要该工具来提供添加自定义叠加层的方法。

I found GMap.Net to be a great tool for the job.

我发现 GMap.Net 是完成这项工作的绝佳工具。

I found a lot of WinFormexamples on the web which creates custom overlays, for example:

WinForm在网上找到了很多创建自定义叠加层的示例,例如:

GMapOverlay markersOverlay = new GMapOverlay("markers");
GMarkerGoogle marker = new GMarkerGoogle(new PointLatLng(-25.966688, 32.580528), GMarkerGoogleType.green);
markersOverlay.Markers.Add(marker);
gmap.Overlays.Add(markersOverlay);

But when I approached to the WPFversion of GMap.Net I noticed that overlays are gone and I am forced to add markers straight to the markers collection (mymap.Markers.Add(new Marker())) without the ability to composite the markers in an separate overlay.

但是当我接近WPFGMap.Net的版本时,我注意到叠加层消失了,我被迫直接将标记添加到标记集合 ( mymap.Markers.Add(new Marker())) 中,而无法在单独的叠加层中合成标记。

How do I use overlays in the Wpf version of GMap.Net?

如何在 GMap.Net 的 Wpf 版本中使用叠加层?

采纳答案by givo

The solution is to implement an overlay collections by yourself. Keep a collection of markers that share the same ZIndexas one overlay and a collection that holds all the overlays.

解决方案是自己实现一个覆盖集合。保留一组与ZIndex一个叠加层共享相同的标记,以及一个包含所有叠加层的集合。

I wish it was like in the WinFromversion.

我希望它就像在WinFrom版本中一样。

回答by bab7lon

To better understand the (GMapMarker) marker, let's have a look at how they're added.

为了更好地理解(GMapMarker) marker,让我们看看它们是如何添加的。

The (UIElement) Shapeof the marker is set, passing

所述(UIElement) Shape标记的设置,传递

  • the MainWindowinstance
  • coordinates (a.o.)
  • ToolTipText
  • MainWindow实例
  • 坐标 (ao)
  • 工具提示文本

to the (UserControl) CustomMarkerDemo's constructor

(UserControl) CustomMarkerDemo的构造函数

// add marker
private void addMarker_Click(object sender, RoutedEventArgs e)
{
    GMapMarker marker = new GMapMarker(currentMarker.Position);
    {
        ... // ToolTipText fetching logic

        marker.Shape = new CustomMarkerDemo(this, marker, ToolTipText);
        marker.ZIndex = combobox.SelectedIndex;
    }
    MainMap.Markers.Add(marker);
}

In the demoI used a ComboBox's SelectedIndexto set the ZIndexof new markers. As you can see markers are added to the (ObservableCollection) MainMap.Markers. It's tempting to remove / filter the markers in the collection when we don't need 'em. Problem with this approach is that when the (UIElement) Shapes are removed from the view, they're disposed and need to be reconstructed. This results in undesired behaviour, especially if you want to be able to show all overlays at once.

演示我用ComboBoxSelectedIndex设置ZIndex的新的标志物。如您所见,标记已添加到(ObservableCollection) MainMap.Markers. 当我们不需要它们时,很容易删除/过滤集合中的标记。这种方法的问题在于,当(UIElement) Shape从视图中删除 s时,它们会被丢弃并需要重建。这会导致不良行为,特别是如果您希望能够一次显示所有叠加层。

Instead I chose to only add markers to the collection (by user interaction) and set the Visibilityof the Shapes, based on current combobox.SelectedIndex.

相反,我选择只标记添加到集合(通过用户交互),并设置Visibility了的ShapeS,基于当前combobox.SelectedIndex

// change overlays
private void combobox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    ComboBox combobox = sender as ComboBox;
    if (combobox != null && MainMap != null)
    {
        // show all overlays
        if (combobox.SelectedIndex == combobox.Items.Count - 1)
        {
            foreach (GMapMarker marker in MainMap.Markers)
                marker.Shape.Visibility = Visibility.Visible;
        }
        // show only selected overlay
        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;
    }
}

I linked a set up with only the bare bones, note there's a lot more functionality in these libraries.

我只链接了一个简单的设置,注意这些库中有更多的功能。