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?
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.
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
MainWindow
instanceto 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) Shape
s 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 Shape
s, 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With