我有一个位置,我想在地图上放置一个指针。问题是如何计算这个指针在vb.Net.中的位置
我可以有3个位置:
- 单个点
- 一个航路点,2个或多个点
- 多边形,多个点
所有的点都是纬度和经度。现在我想为这些情况做一个标记。
- 很简单,标记的位置就是点
- 标记应位于线条的中间
- 多边形是最难的。我可以找到它的质心(重心),但它可以在多边形之外。所以这对标记没有用处
我不知道如何计算这些点(除了nr.1;-))
假设多边形不自相交,则可以:
- 以某种方式将多边形三角化。这可以在O(n log n)时间内完成
- 选择其中一个三角形作为"代表"。我的建议是:面积最大的三角形,或者角度最小的三角形最大,或者质心最接近点的质心的三角形
- 使用此三角形的质心,该质心必须位于三角形内部,因此也必须位于多边形内部
我发现了一个能发挥所有魔力的库:NetTopologySuite您可以将其作为Nuget软件包安装,文档在这里拿到包裹后,一切都很简单。
多边形中心:
首先,通过将所有纬度和经度添加到坐标阵列中来创建多边形。
'The first point needs to be the last point so it becomes a closed shape
'Becouse I loop through all my coordinates I set a flag for the first point
'so I can add it at the end
Dim firstPoint As GeoAPI.Geometries.Coordinate = Nothing
'create an coordinatearray
Dim coordinates() As GeoAPI.Geometries.Coordinate = {}
'Loop trough all the coordinates you have (you can do a dataset loop etc)
'For the example the coordinates are in a pointF array
For Each Point As PointF In points
'Save the first point so we can add it in the end
If firstPoint Is Nothing Then
firstPoint = New GeoAPI.Geometries.Coordinate(Point.X, Point.Y)
End If
'Create a coordinate so we can add it to the coordinate array
Dim coordinate As New GeoAPI.Geometries.Coordinate(Point.X, Point.Y)
'Adding it to the array
Array.Resize(coordinates, coordinates.Length + 1)
coordinates(coordinates.Length - 1) = coordinate
Next
'Now all the coordinates are in the array we need to add the first one at the end
Array.Resize(coordinates, coordinates.Length + 1)
coordinates(coordinates.Length - 1) = firstPoint
'Now we create a linearRing with these coordinates
Dim ring As New NetTopologySuite.Geometries.LinearRing(coordinates)
'And use the ring to get a center point inside it
Dim insidePoint As IPoint = ring.InteriorPoint
'If you want a centroid you can do the following
Dim polygon As New NetTopologySuite.Geometries.Polygon(ring)
Dim centroidPoint As IPoint = polygon.centroid