How to draw circle on the MAP using GMAP.NET in C#

后端 未结 8 1751
鱼传尺愫
鱼传尺愫 2020-12-21 11:06

I am using GMAP.NET in c#. I am able to display the map on the form, now i am trying to draw a CIRCLE mouse by clicking on a certian point, keeping the left mouse button an

8条回答
  •  有刺的猬
    2020-12-21 11:28

            GMapOverlay markers = new GMapOverlay("markers");
            private void CreateCircle(Double lat, Double lon, double radius, int segments)
            {
                markers.Polygons.Clear();
                PointLatLng point = new PointLatLng(lat, lon);
    
                List gpollist = new List();
                for (int i = 0; i < segments; i++)
                    gpollist.Add(FindPointAtDistanceFrom(point, i, radius / 1000));
    
                List gpollistR = new List();
                List gpollistL = new List();
                foreach (var gp in gpollist)
                {
                    if (gp.Lng > lon)
                    {
                        gpollistR.Add(gp);
                    }
                    else
                    {
                        gpollistL.Add(gp);
                    }
                }
                gpollist.Clear();
    
                List gpollistRT = new List();
                List gpollistRB = new List();
                foreach (var gp in gpollistR)
                {
                    if (gp.Lat > lat)
                    {
                        gpollistRT.Add(gp);
                    }
                    else
                    {
                        gpollistRB.Add(gp);
                    }
                }
                gpollistRT.Sort(new LngComparer());
                gpollistRB.Sort(new Lng2Comparer());
                gpollistR.Clear();
                List gpollistLT = new List();
                List gpollistLB = new List();
                foreach (var gp in gpollistL)
                {
                    if (gp.Lat > lat)
                    {
                        gpollistLT.Add(gp);
                    }
                    else
                    {
                        gpollistLB.Add(gp);
                    }
                }
                //gpollistLT.Sort(new LngComparer());
                gpollistLB.Sort(new Lng2Comparer());
                gpollistLT.Sort(new LngComparer());
                gpollistL.Clear();
    
    
                gpollist.AddRange(gpollistRT);
                gpollist.AddRange(gpollistRB);
                gpollist.AddRange(gpollistLB);
                gpollist.AddRange(gpollistLT);
                GMapPolygon gpol = new GMapPolygon(gpollist, "pol");
                gpol.Stroke = new Pen(Color.Red, 1);
                markers.Polygons.Add(gpol);
            }
    
            public static GMap.NET.PointLatLng FindPointAtDistanceFrom(GMap.NET.PointLatLng startPoint, double initialBearingRadians, double distanceKilometres)
            {
                const double radiusEarthKilometres = 6371.01;
                var distRatio = distanceKilometres / radiusEarthKilometres;
                var distRatioSine = Math.Sin(distRatio);
                var distRatioCosine = Math.Cos(distRatio);
    
                var startLatRad = DegreesToRadians(startPoint.Lat);
                var startLonRad = DegreesToRadians(startPoint.Lng);
    
                var startLatCos = Math.Cos(startLatRad);
                var startLatSin = Math.Sin(startLatRad);
    
                var endLatRads = Math.Asin((startLatSin * distRatioCosine) + (startLatCos * distRatioSine * Math.Cos(initialBearingRadians)));
    
                var endLonRads = startLonRad + Math.Atan2(
                              Math.Sin(initialBearingRadians) * distRatioSine * startLatCos,
                              distRatioCosine - startLatSin * Math.Sin(endLatRads));
    
                return new GMap.NET.PointLatLng(RadiansToDegrees(endLatRads), RadiansToDegrees(endLonRads));
            }
    
            public static double DegreesToRadians(double degrees)
            {
                const double degToRadFactor = Math.PI / 180;
                return degrees * degToRadFactor;
            }
    
            public static double RadiansToDegrees(double radians)
            {
                const double radToDegFactor = 180 / Math.PI;
                return radians * radToDegFactor;
            }
    

    and this class

    class LngComparer : IComparer
        {
            #region IComparer Members
    
            public int Compare(PointLatLng x, PointLatLng y)
            {
                if (x == null || y == null)
                    throw new ArgumentException("At least one argument is null");
                if (x.Lng == y.Lng)
                {
                    if (x.Lat > y.Lat)
                    {
                        return 1;
                    }
                    else if (x.Lat < y.Lat)
                    {
                        return -1;
                    }
                    else
                    {
                        return 0;
                    }
                }
                if (x.Lng < y.Lng) return -1;
                return 1;
            }
    
            #endregion
        }
        class Lng2Comparer : IComparer
        {
            #region IComparer Members
    
            public int Compare(PointLatLng x, PointLatLng y)
            {
                if (x == null || y == null)
                    throw new ArgumentException("At least one argument is null");
                if (x.Lng == y.Lng)
                {
                    if (x.Lat > y.Lat)
                    {
                        return 1;
                    }
                    else if (x.Lat > y.Lat)
                    {
                        return -1;
                    }
                    else
                    {
                        return 0;
                    }
                }
                if (x.Lng > y.Lng) return -1;
                return 1;
            }
    
            #endregion
        }
    

提交回复
热议问题