Drawing A Circle

Topics: Windows Forms
Feb 17 at 9:20 AM
Edited Feb 17 at 9:22 AM
Hi All,

I'm learning GMap for use in a project and I'm pretty new to C#. I have run into a problem and I'd be grateful for a little help if possible.

This is the code I'm using
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using GMap.NET;
using GMap.NET.WindowsForms;
using GMap.NET.WindowsForms.Markers;
using GMap.NET.MapProviders;
using System.Drawing.Drawing2D;

namespace GMapTest2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public class GMapMarkerCircle : GMapMarker
        {
            public int Radius;
            public Pen OutlinePen;
            public Brush FillBrush;
            public bool Fill;

            public GMapMarkerCircle(PointLatLng p)
                : base(p)
            {
                OutlinePen = new Pen(Brushes.Orange, 2);
                FillBrush = new SolidBrush(Color.FromArgb(60, Color.Orange));
                Radius = 0;
            }

            public override void OnRender(Graphics g)
            {
                g.SmoothingMode = SmoothingMode.AntiAlias;

                int R = (int)((Radius) / Overlay.Control.MapProvider.Projection.GetGroundResolution((int)Overlay.Control.Zoom, Position.Lat)) * 2;

                if (Fill == true)
                {
                    g.FillEllipse(FillBrush, new System.Drawing.Rectangle(LocalPosition.X - R / 2, LocalPosition.Y - R / 2, R, R));
                }
                g.DrawEllipse(OutlinePen, new System.Drawing.Rectangle(LocalPosition.X - R / 2, LocalPosition.Y - R / 2, R, R));
            }
        }



        private void gmap_Load(object sender, EventArgs e)
        {
            // Selects the provider and loads the map
            gmap.MapProvider = OpenStreetMapProvider.Instance;
            GMapProvider.UserAgent = "";
            GMaps.Instance.Mode = AccessMode.ServerOnly;


            // Sets initial position of the map
            gmap.SetPositionByKeywords("EGBB, UK");
            //EGBB 52.4524, 1.7435
            //gmap.Position = new GMap.NET.PointLatLng(52.4524, -1.7435);




            //  Full code that creates a marker
            GMapOverlay markers = new GMapOverlay("markers");
            GMapMarker marker = new GMarkerGoogle(new PointLatLng(52.45, -1.74), GMarkerGoogleType.yellow_dot);
            markers.Markers.Add(marker);
            gmap.Overlays.Add(markers);



            // Full code that creates polygons
            GMapOverlay polygons = new GMapOverlay("polygons");
            List<PointLatLng> points = new List<PointLatLng>();
            points.Add(new PointLatLng(52.487, -1.731));  // top left
            points.Add(new PointLatLng(52.488, -1.696));  // top right
            points.Add(new PointLatLng(52.469, -1.695));  // bottom right
            points.Add(new PointLatLng(52.468, -1.732));  // bottom left
            GMapPolygon polygon = new GMapPolygon(points, "JCT 7a");
            polygons.Polygons.Add(polygon);
            gmap.Overlays.Add(polygons);
            polygon.Fill = new SolidBrush(Color.FromArgb(50, Color.Red));
            polygon.Stroke = new Pen(Color.Red, 1);



            // Full code to show a route
            GMapOverlay routes = new GMapOverlay("routes");
            List<PointLatLng> point = new List<PointLatLng>();
            point.Add(new PointLatLng(52.485, -1.809));
            point.Add(new PointLatLng(52.412, -1.779));
            point.Add(new PointLatLng(52.437, -1.649));
            GMapRoute route = new GMapRoute(point, "Route Test");
            route.Stroke = new Pen(Color.Blue, 3);
            routes.Routes.Add(route);
            gmap.Overlays.Add(routes);



            // Full code to show a circle
            GMapOverlay circleTest = new GMapOverlay("circleTest");
            gmap.Position = new PointLatLng(52.478, -1.653);
            GMapMarkerCircle gmc = new GMapMarkerCircle(gmap.Position);
            gmc.Radius = 2500;
            gmc.IsVisible = true;
            gmap.Overlays.Add(circleTest);
        }
    }
}
I don't seem to be able to post the result but the marker, polygon and the route all show as expected but the circle does not. Unfortunately I'm not able to understand what I have done wrong. I would be grateful if anyone could show me what the error is. Many thanks
Mar 10 at 6:19 PM
What are you trying to do? If you want to draw a circle, draw first and then convert the coords to lat long