private void gMapControl1_Load(object sender, EventArgs e) { // Initialize map gmap.MapProvider = GMapProviders.GoogleHybridMap; GMap.NET.GMaps.Instance.Mode = GMap.NET.AccessMode.ServerOnly; // Setup base single ui crop List <PointLatLng> uiCropPts = _properties[0].uiCrop; // Iterate over each shapefile foreach (CropProperties property in _properties) { gmap.Position = property.maxExtent; gmap.MinZoom = 1; gmap.MaxZoom = 24; gmap.Zoom = 10; gmap.ShowCenter = false; // Initialize shapefile boundaries List <PointLatLng> points = new List <PointLatLng>(); // Create polygons + add to map Lat = Y, Lng = X points.Add(property.minExtent); points.Add(new PointLatLng(property.maxExtent.Lat, property.minExtent.Lng)); points.Add(property.maxExtent); points.Add(new PointLatLng(property.minExtent.Lat, property.maxExtent.Lng)); // Creates extents and style GMapOverlay extentOverlay = new GMapOverlay("Extent overlay"); GMapPolygon polygon = new GMapPolygon(points, "Extents"); Color col = property.color; polygon.Fill = new SolidBrush(Color.FromArgb(80, col.R, col.G, col.B)); polygon.Stroke = new Pen(Color.FromArgb(col.R, col.G, col.B), 2); extentOverlay.Polygons.Add(polygon); gmap.Overlays.Add(extentOverlay); // Add colour and shapefile names to list view ListViewItem item = new ListViewItem(property.shp.Name); item.BackColor = Color.FromArgb(50, col.R, col.G, col.B); item.Checked = true; listView1.Items.Add(item); } // Create dynamic input crop CropProperties firstCrop = _properties[0]; // Parametric input if (firstCrop != null) { List <PointLatLng> cropPts = new List <PointLatLng>(); cropPts.Add(firstCrop.minCrop); cropPts.Add(new PointLatLng(firstCrop.maxCrop.Lat, firstCrop.minCrop.Lng)); cropPts.Add(firstCrop.maxCrop); cropPts.Add(new PointLatLng(firstCrop.minCrop.Lat, firstCrop.maxCrop.Lng)); // ADD POLYGON TO MAP var rand = new Random(); GMapPolygon crop = new GMapPolygon(cropPts, "Crop"); crop.Fill = new SolidBrush(Color.FromArgb(80, Color.Red)); crop.Stroke = new Pen(Color.Red, 2); cropOverlay.Polygons.Add(crop); gmap.Overlays.Add(cropOverlay); } }
private void gmap_MouseClick(object sender, MouseEventArgs e) { // Setup map overlay CropProperties crop = _properties[0]; List <PointLatLng> pts = crop.uiCrop; // Register left mouse button if (e.Button == System.Windows.Forms.MouseButtons.Left) { PointLatLng pt = gmap.FromLocalToLatLng(e.X, e.Y); pts.Add(pt); if (pts.Count > 2) { pts.Clear(); cropOverlay.Clear(); uiOverlay.Clear(); pts.Add(pt); } // Add a bounding box to map if (crop.uiCrop.Count == 2) { // Reorder pts to ensure max and min are correct. List <double> Lats = new List <double>(); List <double> Lngs = new List <double>(); Lats.Add(pts[0].Lat); Lats.Add(pts[1].Lat); Lngs.Add(pts[0].Lng); Lngs.Add(pts[1].Lng); Lats.Sort(); Lngs.Sort(); PointLatLng min = new PointLatLng(Lats[0], Lngs[0]); PointLatLng max = new PointLatLng(Lats[1], Lngs[1]); pts.Clear(); pts.Add(min); pts.Add(max); // Clear the overlay and create rectangle polygon uiOverlay.Clear(); List <PointLatLng> finalPts = new List <PointLatLng>(); PointLatLng pt0 = pts[0]; PointLatLng pt1 = new PointLatLng(pts[1].Lat, pts[0].Lng); PointLatLng pt2 = pts[1]; PointLatLng pt3 = new PointLatLng(pts[0].Lat, pts[1].Lng); finalPts.Add(pt0); finalPts.Add(pt1); finalPts.Add(pt2); finalPts.Add(pt3); // Render the polygon cropOverlay.Clear(); gmap.Overlays.Remove(uiOverlay); GMapPolygon cropB = new GMapPolygon(finalPts, "Crop"); cropB.Fill = new SolidBrush(Color.FromArgb(80, Color.Red)); cropB.Stroke = new Pen(Color.Red, 4); gmap.Overlays.Add(uiOverlay); uiOverlay.Polygons.Add(cropB); // Set the crop boundary max and min foreach (CropProperties cropBoundary in _properties) { cropBoundary.minCrop = pt0; cropBoundary.maxCrop = pt2; } } } }