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

后端 未结 8 1868
鱼传尺愫
鱼传尺愫 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-21 11:29

    I hit the same problem and on entry I had Lon, Lat and radius, here is my solution. It works like a charm :)

    private void CreateCircle(Double lat, Double lon, double radius)
    {
       PointLatLng point = new PointLatLng(lat, lon);
       int segments = 1000;
    
       List gpollist = new List();
    
       for (int i = 0; i < segments; i++)
          gpollist.Add(FindPointAtDistanceFrom(point, i, radius / 1000));
    
       GMapPolygon gpol = new GMapPolygon(gpollist, "pol");
    
       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;
    }
    

    call

    CreateCircle(51.640980, -2.673544, 1143.899431);
    

提交回复
热议问题