6

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?

Matan Givoni
  • 1,080
  • 12
  • 34
  • You should probably ask that question on their [Discussion Page](https://greatmaps.codeplex.com/discussions/topics/4903/wpf). – Clemens Jun 19 '15 at 07:12
  • And this is the corresponding [thread](https://greatmaps.codeplex.com/discussions/640141) – rdoubleui Jul 14 '15 at 11:55

2 Answers2

5

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.

Matan Givoni
  • 1,080
  • 12
  • 34
  • 1
    Sorry for waking an old thread. I was also reading the same example, and was wondering two things. One of it is what you asked, the other question is how to use `GMarkerGoogleType` enum? I thought it would be nice if I can just grab the markers right from API, rather than drawing it out myself, or downloaded image from somewhere else. – Jai Jun 27 '16 at 03:50
2

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.

bab7lon
  • 343
  • 1
  • 10