Overlays in GMap.Net Wpf

9.7k views Asked by At

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.

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

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 WPF version 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.

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

2

There are 2 answers

1
Matan Givoni On BEST ANSWER

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

I wish it was like in the WinFrom version.

0
bab7lon On

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

The (UIElement) Shape of the marker is set, passing

  • the MainWindow instance
  • coordinates (a.o.)
  • ToolTipText

to the (UserControl) CustomMarkerDemo's constructor

// 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 demo I used a ComboBox's SelectedIndex to set the ZIndex of 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.

Instead I chose to only add markers to the collection (by user interaction) and set the Visibility of the Shapes, based on current 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.