c# - 如何在 Gmap.net 中的多边形中心添加文本

标签 c# winforms maps gis gmap.net

我想在已有区域多边形的中心位置添加一个文本。我能够显示多边形,也能够获得多边形的中心位置,但我无法添加多边形的名称。

到目前为止,我已经完成了以下工作:

        List<PointLatLng> listVertexPoints = Get_VertexPointsFromString(zoneVertex);
        foreach (PointLatLng vertex in listVertexPoints)
        {
            GMapMarkerCircle circleVertex = new GMapMarkerCircle(vertex, 1);
            circleVertex.Radius = 1;
            circleVertex.IsVisible = false;
            overlay.Markers.Add(circleVertex);
        }

        GMapPolygon zonePolygon;
        zonePolygon = new GMapPolygon(listVertexPoints, zoneName);
        zonePolygon.Fill = new SolidBrush(Color.FromArgb(50, Color.Red));
        zonePolygon.Stroke = new Pen(zoneBoundaryColor, 1);
        zonePolygon.IsFilled = true;
        zonePolygon.IsHitTestVisible = true;
        overlay.Polygons.Add(zonePolygon);

        PointLatLng centerPoint = GetZoneCenter(listVertexPoints);

        private PointLatLng GetZoneCenter(List<PointLatLng> vertexes)
        {
          PointLatLng centerPoint = new PointLatLng();
          int sum = 0;
          double lat = 0;
          double lng = 0;
          foreach (var point in vertexes)
          {
              sum += 1;
              lat += point.Lat;
              lng += point.Lng;
          }
          lat = lat / sum;
          lng = lng / sum;
          centerPoint.Lat = lat;
          centerPoint.Lng = lng;
          return centerPoint;
      }
        var labelMarker = new GmapMarkerWithLabel(centerPoint, zoneName, GMarkerGoogleType.blue);
        markerOverlay.Markers.Add(labelMarker);
        this.MainMap.Overlays.Add(overlay);

上面的代码显示了一个标签标记,但它没有出现在正确的位置。此外,如果您有任何其他方式在 Polygon 中显示文本,那么我们非常欢迎您。

谢谢!

最佳答案

经过一番努力,我找到了解决办法。我在 GMapPolygon.cs 类的 UpdateGraphicsPath() 方法中调用 SetCenterFromVertices() 方法,并使用图形的 drawString 方法在所需位置绘制文本。这是必需的,因为有可能在调整多边形的大小时;我们也需要更改文本的位置。

    #region use this line inside OnRender(Graphics g) method
    g.DrawString(this.Name, font_PolygonName, Brushes.AliceBlue, (float)center.X, (float)center.Y);
    #endregion

    private Point_ center = new Point_();

    void SetCenterFromVertices()
    {
        List<Point_> lstPoint = new List<Point_>();
        foreach (var p in LocalPoints)
        {
            Point_ np = new Point_();
            np.X = p.X;
            np.Y = p.Y;
            lstPoint.Add(np);
        }
        center = Compute2DPolygonCentroid(lstPoint);
    }

    static Point_ Compute2DPolygonCentroid(List<Point_> vertices)
    {
        Point_ centroid = new Point_() { X = 0.0, Y = 0.0 };
        double signedArea = 0.0;
        double x0 = 0.0;
        double y0 = 0.0;
        double x1 = 0.0;
        double y1 = 0.0;
        double a = 0.0;

        // For all vertices except last
        int i = 0;
        for (i = 0; i < vertices.Count - 1; ++i)
        {
            x0 = vertices[i].X;
            y0 = vertices[i].Y;
            x1 = vertices[i + 1].X;
            y1 = vertices[i + 1].Y;
            a = x0 * y1 - x1 * y0;
            signedArea += a;
            centroid.X += (x0 + x1) * a;
            centroid.Y += (y0 + y1) * a;
        }

        // Do last vertex
        x0 = vertices[i].X;
        y0 = vertices[i].Y;
        x1 = vertices[0].X;
        y1 = vertices[0].Y;
        a = x0 * y1 - x1 * y0;
        signedArea += a;
        centroid.X += (x0 + x1) * a;
        centroid.Y += (y0 + y1) * a;

        signedArea *= 0.5;
        centroid.X /= (6 * signedArea);
        centroid.Y /= (6 * signedArea);

        return centroid;
    }

关于c# - 如何在 Gmap.net 中的多边形中心添加文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51202991/

相关文章:

c# - .NET Core 中的 Thread.Sleep 替代方案

c# - 存储为字符串的脚本的 IronPython 依赖项

c# - 如何将字符串化的 JSON 传递给 C# 方法?

javascript - 如何在 Javascript 中获取一个国家/地区的坐标?

c# - 将没有成员名称的 JSON 数组反序列化为 C# 对象

c# - ASP.NET:在 if 语句中使用 Eval

c# - 为什么在 C# 中的 RichTextBox 中选择所有文本?

winforms - 如何在 Azure 中 ping 我的网站?

C# Winforms - 设置组合框选择的值

ios - HERE map ios 的内存压力