Drawing Circles

Jul 23, 2010 at 3:04 PM

How can I draw a circle? So, for example, I give it the center Lat/Long and the radius.

Thanks

Coordinator
Jul 23, 2010 at 6:43 PM

check wpf demo

Jul 26, 2010 at 9:06 AM

Thanks for the quick reply radioman. I forgot to mention that I an working on a WinForm app... I don't know if the WPF demo will help me? I'll take a look anyways... could you please post me the link to where the demos are?

 

Kind Regards

Fabricio Rodriguez

Pretoria, South Africa

Jul 26, 2010 at 10:00 AM

U Can use a custom marker, like this:

To use it, give 2 points on the map, or simple the centerpoint ant the distance in kilometers.

 

 

using System;
using System.Drawing;
using GMap.NET.WindowsForms;
using GMap.NET;
using System.Windows.Forms;

namespace GMap.NET.WindowsForms.CustomMarkers
{
    public class GMapMarkerCircle : GMapMarker
    {
        public Pen Pen;

        public GMap.NET.PointLatLng maxDistancePoint;
        private System.Drawing.Size drawingSize;

        private GMapControl MainMap;

        public GMapMarkerCircle(GMap.NET.PointLatLng p, GMap.NET.PointLatLng distancePoint, GMapControl mainMap, Brush brush, int penSize)
            : base(p)
        {
            Pen = new Pen(brush, penSize);
            maxDistancePoint = distancePoint;
            MainMap = mainMap;
        }

        public GMapMarkerCircle(GMap.NET.PointLatLng p, GMap.NET.PointLatLng distancePoint, GMapControl mainMap)
            : this(p, distancePoint, mainMap, Brushes.Red, 5)
        {
        }

        public GMapMarkerCircle(GMap.NET.PointLatLng p, double distance, GMapControl mainMap, Brush brush, int penSize)
            : base(p)
        {
            Pen = new Pen(brush, penSize);
            MainMap = mainMap;
            maxDistancePoint = createPointLatLngFromCenterDistance(p, distance);
        }

        public GMapMarkerCircle(GMap.NET.PointLatLng p, double distance, GMapControl mainMap)
            : this(p, distance, mainMap, Brushes.Red, 5)
        {
        }

        public static PointLatLng createPointLatLngFromCenterDistance(PointLatLng center, double xMeter)
        {
            return createPointLatLngFromCenterDistance(center, xMeter, xMeter);
        }

        public static PointLatLng createPointLatLngFromCenterDistance(PointLatLng center, int direction, double xMeter)
        {
            return createPointLatLngFromCenterDistance(center, direction, xMeter, xMeter);
        }

        public static PointLatLng createPointLatLngFromCenterDistance(PointLatLng center, double xMeter, double yMeter)
        {
            return createPointLatLngFromCenterDistance(center, 360, xMeter, yMeter);
        }

        public static PointLatLng createPointLatLngFromCenterDistance(PointLatLng center, int direction, double xMeter, double yMeter)
        {
            PointLatLng distancePoint = new PointLatLng();

            xMeter = xMeter / 1000;

            double earthRadius = 6367.0;
            double lat = center.Lat * (Math.PI / 180);
            double lon = center.Lng * (Math.PI / 180);

            double d = xMeter / earthRadius;
            double brng = direction * (Math.PI / 180);

            var latRadians = Math.Asin(Math.Sin(lat) * Math.Cos(d) + Math.Cos(lat) * Math.Sin(d) * Math.Cos(brng));
            var lngRadians = lon + Math.Atan2(Math.Sin(brng) * Math.Sin(d) * Math.Cos(lat), Math.Cos(d) - Math.Sin(lat) * Math.Sin(latRadians));

            distancePoint.Lat = latRadians * 180 / Math.PI;
            distancePoint.Lng = lngRadians * 180 / Math.PI;

            return distancePoint;
        }
        
        // calculates circle radius
        public void autoScaleToZoom()
        {
            var pxCenter = MainMap.FromLatLngToLocal(this.Position);
            var pxBounds = MainMap.FromLatLngToLocal(maxDistancePoint);

            double a = (double)(pxBounds.X - pxCenter.X);
            double b = (double)(pxBounds.Y - pxCenter.Y);
            var pxCircleRadius = Math.Sqrt(a * a + b * b);

            
            drawingSize = new System.Drawing.Size(
                (int) Math.Round(pxCircleRadius * 2),
                (int) Math.Round(pxCircleRadius * 2)
            );
        }

        public void setDistance(PointLatLng p)
        {
            maxDistancePoint = p;
        }

        public void setDistance(double range)
        {
            maxDistancePoint = createPointLatLngFromCenterDistance(this.Position,range);
        }

        public override void OnRender(Graphics g)
        {
            autoScaleToZoom();
            g.DrawEllipse(Pen, new System.Drawing.Rectangle(LocalPosition.X - drawingSize.Width / 2, LocalPosition.Y - drawingSize.Height / 2, drawingSize.Width, drawingSize.Height));
        }
    }
}

 

greetz, Manuel

Jul 26, 2010 at 10:57 AM
Edited Jul 26, 2010 at 10:59 AM
;;
Jul 26, 2010 at 11:03 AM
// Or another way, Radius in Meters - Cheers


using System.Drawing;
using System.Drawing.Drawing2D;

namespace GMap.NET.WindowsForms.Markers
{
    public class GMapMarkerCircle : GMapMarker
    {
        public int Radius;  //In Meters
        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.Core.Projection.GetGroundResolution(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));
        }
    }
}
Jul 26, 2010 at 12:04 PM

ah, didn't know the GetGroundResolution method

 

thx!

Jul 26, 2010 at 12:21 PM
The Object Browser is your friend:)
Jul 26, 2010 at 3:18 PM

That's perfect! Thank you guys sooo much! I will try the code tonight. Thank you for your support!