private void ExtendedGMapControl_OnMarkerClick(GMapMarker item, MouseEventArgs args) { if (this.PolygonsEnabled && _allowDrawPolygon) { #region close polygon //only can create the polygon if the polygon is incomplete if (!polygonIsComplete && this.selectedVertice != null) { //the polygon only be closed if the click is on the first vertice, and is not the first time if (OverlayPolygon_Vertices.Markers.First() == this.selectedVertice && OverlayPolygon_Vertices.Markers.Count > 1) { this.OverlayPolygon_Vertices.Markers.Add(this.selectedVertice); //add the vertices to polygon (make polygon) _polygon.Points.AddRange(OverlayPolygon_Vertices.Markers.Select(m => m.Position)); this.UpdatePolygonLocalPosition(_polygon); polygonIsComplete = true; OverlayPolygon_Auxiliary.Markers.Clear(); //add new intermediate points between the vertices for (int i = 0; i < OverlayPolygon_Vertices.Markers.Count; i++) { if (i != OverlayPolygon_Vertices.Markers.Count - 1) { PointLatLng middle = CalculateMiddlePoint(OverlayPolygon_Vertices.Markers[i], OverlayPolygon_Vertices.Markers[i + 1]); GMapMarkerGraySquare intermediatePoint = new GMapMarkerGraySquare(middle); OverlayPolygon_Auxiliary.Markers.Add(intermediatePoint); } } } } #endregion } }
public void SetDrawingPolygonCustom(CustomPolygon polygon) { ClearDrawingPolygon(); OverlayFirst = this.Overlays[0]; _polygon = polygon; { OverlayPolygon_Auxiliary = new GMapOverlay("auxiliary"); this.Overlays.Add(OverlayPolygon_Auxiliary); OverlayPolygon_Vertices = new GMapOverlay("vertices"); this.Overlays.Add(OverlayPolygon_Vertices); } //draw the polygon if (polygon.Points.Count > 2) { for (int i = 0; i < polygon.Points.Count; i++) { GMapMarkerRedCircle vertice = null; if (i == polygon.Points.Count - 1) { vertice = (GMapMarkerRedCircle)OverlayPolygon_Vertices.Markers[0]; } else { vertice = new GMapMarkerRedCircle(polygon.Points[i]); } OverlayPolygon_Vertices.Markers.Add(vertice); if (i > 0) { PointLatLng intermedium = CalculateMiddlePoint(OverlayPolygon_Vertices.Markers[i - 1], OverlayPolygon_Vertices.Markers[i]); GMapMarkerGraySquare intermediatePoint = new GMapMarkerGraySquare(intermedium); OverlayPolygon_Auxiliary.Markers.Add(intermediatePoint); } } polygonIsComplete = true; } if (!OverlayFirst.Polygons.Contains(_polygon)) { OverlayFirst.Polygons.Add(_polygon); } }
private void ExtendedGMapControl_MouseUp(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) { isMouseDown = false; if (this.PolygonsEnabled && _allowDrawPolygon) { #region OnDrop vertice if (selectedVertice != null) { //until the poligon is complete, make auxiliary lines joining the vertices if (!polygonIsComplete && OverlayPolygon_Vertices.Markers.Count > 1) { int verticeIndex = OverlayPolygon_Vertices.Markers.IndexOf(selectedVertice); Pen pen = new Pen(Brushes.DarkGray, 3); GMapMarkerLine auxLine = new GMapMarkerLine(selectedVertice.Position, OverlayPolygon_Vertices.Markers[verticeIndex - 1].Position, pen); OverlayPolygon_Auxiliary.Markers.Add(auxLine); } if (isDraggingVertice) { isDraggingVertice = false; if (polygonIsComplete) { _polygon.Points.Clear(); _polygon.Points.AddRange(OverlayPolygon_Vertices.Markers.Select(m => m.Position)); this.UpdatePolygonLocalPosition(_polygon); //rearrangement intermediate points int selectedIndex = OverlayPolygon_Vertices.Markers.IndexOf(selectedVertice); //previous point if (selectedIndex == 0) { GMapMarker intermediatePoint = OverlayPolygon_Auxiliary.Markers.Last(); intermediatePoint.Position = CalculateMiddlePoint(OverlayPolygon_Vertices.Markers[OverlayPolygon_Vertices.Markers.Count - 2], OverlayPolygon_Vertices.Markers[selectedIndex]); } else { OverlayPolygon_Auxiliary.Markers[selectedIndex - 1].Position = CalculateMiddlePoint(OverlayPolygon_Vertices.Markers[selectedIndex - 1], OverlayPolygon_Vertices.Markers[selectedIndex]); } //next point OverlayPolygon_Auxiliary.Markers[selectedIndex].Position = CalculateMiddlePoint(OverlayPolygon_Vertices.Markers[selectedIndex], OverlayPolygon_Vertices.Markers[selectedIndex + 1]); } } selectedVertice = null; } #endregion #region OnDrop intermediate point //if dragging intermediate point dragging is finish if (selectedIntermediatePoint != null) { if (isDraggingIntermediatePoint) { isDraggingIntermediatePoint = false; //make a new vertice GMapMarkerRedCircle newVertice = new GMapMarkerRedCircle(selectedIntermediatePoint.Position); //remove the old intermediate point int selectedIndex = OverlayPolygon_Auxiliary.Markers.IndexOf(selectedIntermediatePoint); OverlayPolygon_Auxiliary.Markers.Remove(selectedIntermediatePoint); //add the new vertice, in the correct position of the vertices collection int newVerticeIndex = selectedIndex + 1; OverlayPolygon_Vertices.Markers.Insert(newVerticeIndex, newVertice); //update polygon _polygon.Points.Clear(); _polygon.Points.AddRange(OverlayPolygon_Vertices.Markers.Select(m => m.Position)); this.UpdatePolygonLocalPosition(_polygon); //make and add new intermediate points PointLatLng intermediatePosition1 = CalculateMiddlePoint(OverlayPolygon_Vertices.Markers[newVerticeIndex - 1], OverlayPolygon_Vertices.Markers[newVerticeIndex]); GMapMarkerGraySquare intermediatePoint1 = new GMapMarkerGraySquare(intermediatePosition1); PointLatLng intermediatePosition2 = CalculateMiddlePoint(OverlayPolygon_Vertices.Markers[newVerticeIndex], OverlayPolygon_Vertices.Markers[newVerticeIndex + 1]); GMapMarkerGraySquare intermediatePoint2 = new GMapMarkerGraySquare(intermediatePosition2); OverlayPolygon_Auxiliary.Markers.Insert(selectedIndex, intermediatePoint1); OverlayPolygon_Auxiliary.Markers.Insert(selectedIndex + 1, intermediatePoint2); } selectedIntermediatePoint = null; } #endregion } } }