Overlays in GMap.Net Wpf
Asked Answered
C

2

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?

Crinkleroot answered 18/6, 2015 at 17:44 Comment(2)
You should probably ask that question on their Discussion Page.Deduce
And this is the corresponding threadKraft
C
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.

Crinkleroot answered 24/7, 2015 at 13:56 Comment(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.Deciduous
U
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.

Unanimous answered 11/3, 2017 at 9:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.