GMap - cannot detect clicking on polygon
Asked Answered
B

1

5

IsMouseOverMarker property detects clicking on marker just fine, but when trying to use IsMouseOverPolygon property of GMap Control to detect if user clicked on polygon line - it doesn't seem to be working.

Note: PolygonEnabled property of GMap control is set to True.

The OnPolygonClick event doesn't even fire:

private void gMap_OnPolygonClick(GMapPolygon item, MouseEventArgs e) {
        double pLat = item.From.Value.Lat;
}

Map Click event does fire, but the 'IsMouseOverPolygon` never gets True value:

private void gMap_Click(object sender, EventArgs e) {
   if (gMap.IsMouseOverMarker) {
       MessageBox.Show("Clicked on marker and it works!");
   }

   if (gMap.IsMouseOverPolygon) {
       MessageBox.Show("clicked on line - never works");
   } 
}

I wonder if there is something wrong in a way I'm adding polygons or is it because in my case it's just lines:

GMapOverlay polyOverlay  = new GMapOverlay("polygons");
gMap.Overlays.Add(polyOverlay);
List<PointLatLng> points = new List<PointLatLng>();
points.Add(start);
points.Add(end);
polygon = new GMapPolygon(points, "mypolygon");
polygon.Stroke = new Pen(Color.Blue, 5);
polyOverlay.Polygons.Add(polygon);

So, the question is: how should I go about detecting mouse click on those lines?

Banky answered 24/11, 2015 at 18:34 Comment(2)
It is going to be difficult for anyone to help you without a minimal reproducible exampleVassal
Sure, I'll try to add some code to my question. Thank you for your comment.Banky
E
7

I can see two issues within the code. First you need to explicitely define the polygon as HitTestVisible:

polygon.IsHitTestVisible = true;

Second, to set up a polygon add at least three points that are not aligned and actually spawn an area. I've found that the click will only be noticed on an actual area, where in theory a polygon can consist of two points.

With the hints above the check for gMap.IsMouseOverPolygon should return true then.

Ellisellison answered 2/12, 2015 at 13:55 Comment(1)
I found that the fact that my "lines" are not real polygons are most likely the problem. When I use the same coordinates to create routes instead, then the control is able to detect click (route being clicked) without any issues. Thank you for your answer!Banky

© 2022 - 2024 — McMap. All rights reserved.