OnMarkerClick fires multiple times with 1 click on marker

Hi,
Im using GMap.NET.Core version 2.1.7 with the openstreetmap provider in a WIndows Forms application.

In the app I have added several markers to the map.

When I click a marker and fire the method below it fires multiple times and not just one time.
The number of fires is equal to the number of markers I have added.

What can be wrong? Why is it firing multiple times?

Here is the methods:

private void Import_Poi1_Click(object sender, EventArgs e)
        {        
            var list = new List<string>();
            string name = "Poi1.txt";
            using (var reader = new StreamReader(name))
            {
                string line;
                while ((line = reader.ReadLine()) != null)
                {
                    line = line.Replace("*",",");

                    string[] str = line.Split(',');

                    var marker = new GMap.NET.WindowsForms.Markers.GMarkerGoogle(
                        new GMap.NET.PointLatLng(Convert.ToDouble(str[1]), Convert.ToDouble(str[2])),
                        GMap.NET.WindowsForms.Markers.GMarkerGoogleType.red_small);
                    marker.ToolTipText = str[0];
                    marker.ToolTip.Fill = Brushes.Blue;
                    marker.ToolTip.Foreground = Brushes.White;
                
                    markersOverlay.Markers.Add(marker);
                    gmap.Overlays.Add(markersOverlay);
                    
                }
            }
            gmap.Update();
            gmap.Refresh();

        }
 private void gMapControl1_OnMarkerClick(GMapMarker item, MouseEventArgs e)
        {

                if (e.Button == MouseButtons.Left && item.IsMouseOver)
                {
                  
                    MessageBox.Show(item.ToolTipText );
                }

            }


Hi @roarkr51 and welcome here in the community!

While I fear that your question is out of scope here and I’d rather think you would ask it at i.e. stackoverflow, here’s my guess:

You should add the markersOverlay only once after your while-loop. With your current code you’ll get as much identical overlays as you have markers instead of one single overlay.

while ((line = reader.ReadLine()) != null)
{
    line = line.Replace("*",",");

    string[] str = line.Split(',');

    var marker = new GMap.NET.WindowsForms.Markers.GMarkerGoogle(
        new GMap.NET.PointLatLng(Convert.ToDouble(str[1]), Convert.ToDouble(str[2])),
        GMap.NET.WindowsForms.Markers.GMarkerGoogleType.red_small);
        marker.ToolTipText = str[0];
        marker.ToolTip.Fill = Brushes.Blue;
        marker.ToolTip.Foreground = Brushes.White;
                
        markersOverlay.Markers.Add(marker);
                    
}
gmap.Overlays.Add(markersOverlay);

Best regards
Vinzenz

1 Like

Thanks for the fast response and good advice.

Moved the gmap.Overlays.Add(markersOverlay); out of the loop and it is working fine now.

1 Like