Removing / Adding Overlays

Topics: Windows Forms
Jan 23, 2011 at 5:25 PM

Hi, 

My application (VB)  using GMap.Net has a number of layers, each layer having a number of markers and routes.

I allow the user to Add and Remove each layer, as they choose, using a CheckBox for each Overlay, for map clarity, using the following code.

 

 

Private Sub cboxRunUpSights_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboxRunUpSights.CheckedChanged

Dim pos As GMap.NET.PointLatLng = map.Position

        If cboxRunUpSights.Checked Then

            map.Overlays.Add(ovSightsRun) 

       Else

            map.Overlays.Remove(ovSightsRun) 

       End If

       map.Invalidate()

       map.Refresh()

End Sub

 

Provided the map is not Panned, or Zoom changed, between removing and re-adding an overlay, this works a treat.

However, if an Overlay removed, the map zoom changed, or panned, then the Overlay added again, it appears in the wrong place relative to other overlays; UNTIL the map zoom is again changed OR the map is again panned, where after the Overlay is redrawn in correct relation to other Overlays?

Coordinator
Jan 23, 2011 at 5:53 PM

i recommend instead of adding/removing overlay, use IsVisibile property:

Private Sub cboxRunUpSights_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboxRunUpSights.CheckedChanged
    ovSightsRun.IsVisibile = cboxRunUpSights.Checked;
End Sub

Jan 23, 2011 at 6:13 PM

Had not come across the IsVisible property

Your suggestion works perfectly. Thank you

Jan 24, 2011 at 5:43 PM

I have experienced this behavior as well.

In our application, we have a bank of available overlays that the user can add to their map.  Once added, the user can (via the IsVisible property) hide and show the individual overlays.

So from a structuring perspective, it makes sense for us to maintain a distinction between an overlay that is on/off the map, and an overlay that is visible/invisible on the map.  So then, would it make sense to expose a method on the overlay class, similar to UpdatePolygonLocalPosition method for the polygon class?  The method would recalculate the local position of all objects on the overlay.  Perhaps the underlying code could call this method implicitly when an overlay is added to the map?

 

Coordinator
Jan 24, 2011 at 7:09 PM

yes, i'll fix it later

Aug 22, 2014 at 10:00 AM
i got a problem with removing overlays from the gmapscontrol.
gMapControl1.Overlays.Remove(gmoOverlay); 
this doesn't work for me... when i try removing with the index, the Overlays.IndexOf(gmoOverlay) alwas return -1, even the overlay is 5 times in it... please help me.