-1

I am adding multiple polygons on a map and trying to identify which one is clicked on. C# can already pick up whether a polygon was clicked, but I need to identify which one was clicked. I have tried many variations to this, but Currently I have with the error at the bottom:

{
     gMapControl1.MapProvider = GMapProviders.GoogleMap;
     gMapControl1.Position = new PointLatLng(52.2659416, 10.5267296);

     GMapOverlay polygons = new GMapOverlay("polygons");
     List<PointLatLng> points = new List<PointLatLng>();
     points.Add(new PointLatLng(52.2659416, 10.5267296));
     points.Add(new PointLatLng(62.2659416, 10.5267296));
     points.Add(new PointLatLng(62.2659416, 20.5267296));
     points.Add(new PointLatLng(52.2659416, 20.5267296));
     GMapPolygon polygon = new GMapPolygon(points, "test");
     polygons.Polygons.Add(polygon);
     gMapControl1.Overlays.Add(polygons);
}

private void gmap_OnPolygonClick(GMapPolygon item, MouseEventArgs e)
{
     Console.WriteLine(String.Format("Polygon {0} with tag {1} was clicked",
     item.Name, item.Tag));
}

Error CS0104 'MouseEventArgs' is an ambiguous reference between 'System.Windows.Forms.MouseEventArgs' and 'System.Windows.Input.MouseEventArgs'

Ian
  • 151
  • 1
  • 3
  • 9
  • Your error means you have a reference to `MouseEventArgs` and are probably `using` both `System.Windows.Forms` and `System.Windows.Input`. Change the reference to `MouseEventArgs` to `System.Windows.XXXXX.MouseEventArgs`, where `XXXXX` is the one that you intend to use. – Bob Kaufman Nov 05 '19 at 22:43
  • `System.Windows.Forms` is for WinForms and `System.Windows.Input` is for WPF and you should not be using both. Which one are you using? – Dour High Arch Nov 05 '19 at 22:44
  • I am using Forms, adding the System,Windows.Forms removed the error but it now does nothing when I click it. How should the Form.Designer.cs entry look like? – Ian Nov 05 '19 at 23:01
  • Where is the event handler assigned for the polygons? Maybe you're missing that. Something like `polygon.PolygonClicked += gmap_OnPolygonClick;` – Albert Nov 05 '19 at 23:03
  • I have an assignment in Form.designer.cs. But that is not really working. The polygon.PolygonClicked += gmap_OnPolygonClick; also does not seem to work. – Ian Nov 05 '19 at 23:06
  • Try to clean and rebuild your project and make sure that the GmapControl has this gmap_OnPolygonClick assigned to it on its events pane property. – Albert Nov 05 '19 at 23:11
  • I have tried that and this.gMapControl1.Click += new System.EventHandler(this.gmap_OnPolygonClick); in the private void InitializeComponent() of Form.Designer.cs, not sure if that is the events pane property you were referring to. – Ian Nov 05 '19 at 23:17
  • No, not directly on the designer.cs. On the form, when you click the GmapControl, and hit F4, it opens the Properties Pane, where you can go find the Events for the control. And then you can set it there. – Albert Nov 05 '19 at 23:21

1 Answers1

0

You have to specify the polygon's IsHitTestVisible to true:

polygon.IsHitTestVisible = true;

Found it here: GMap - cannot detect clicking on polygon

Albert
  • 487
  • 1
  • 4
  • 16