我使用WPF中的mapsui控件在地图上显示5个点之间的4条线。我实现了一种通过单击地图来显示所选线路的方法。现在我需要能够同时选择所有 4 行。最好是当拖动光标时,形成一个多边形并选择其中的所有线条。我如何使用 Mapsui Control 实现这样的方法?不幸的是,我在网站上的mapsui文档中找不到此信息。
public Task<Map> CreateMapAsync(Map map)
{
map.Layers.Add(Mapsui.Tiling.OpenStreetMap.CreateTileLayer());
var polylineLayer = CreatePolylineLayer();
map.Layers.Add(polylineLayer);
map.Navigator.CenterOnAndZoomTo(polylineLayer.Extent!.Centroid, map.Navigator.Resolutions[6]);
map.Info += (s, a) => ToggleSelected(a.MapInfo?.Feature);
map.Widgets.Add(new MapInfoWidget(map));
return Task.FromResult(map);
}
public static ILayer CreatePolylineLayer()
{
return new Layer("Polyline")
{
DataSource = new MemoryProvider(CreatePolylines(CreatePoinst()).Select(p => new GeometryFeature { Geometry = p })),
Style = CreateLineStringStyle(),
IsMapInfoLayer = true
};
}
private static List<Point> CreatePoinst()
{
var berlin = new Point(13.4050, 52.52);
var dresden = new Point(13.7373, 51.0504);
var leipzig = new Point(12.3712900, 51.33962);
var stuttgard = new Point(9.1829321, 48.7758459);
var hannover = new Point(9.7332, 52.3705);
List<Point> points = new List<Point>() { berlin, dresden, leipzig, stuttgard, hannover };
return points;
}
private static List<LineString> CreatePolylines(List<Point> points)
{
List<LineString> ListPolylines = new List<LineString>();
for (int i = 0; i < points.Count-1; i++)
{
var netTopSu = NetTopologySuite.NtsGeometryServices.Instance.CreateGeometryFactory(4326);
var lnsAURtoLER = netTopSu.CreateLineString(new[] {
new NetTopologySuite.Geometries.Coordinate(points[i].Y, points[i].X),
new NetTopologySuite.Geometries.Coordinate(points[i+1].Y, points[i+1].X)});
var stringOfGeometry = lnsAURtoLER.ToString();
var lineString = (LineString)new WKTReader().Read(stringOfGeometry);
lineString = new LineString(lineString.Coordinates.Select(v => SphericalMercator.FromLonLat(v.Y, v.X).ToCoordinate()).ToArray());
ListPolylines.Add(lineString);
}
return ListPolylines;
}
public static IStyle CreateLineStringStyle()
{
return new ThemeStyle(f =>
{
if (f["selected"]?.ToString().Length > 0 ) //f["selected"]?.ToString() == "true"
return new VectorStyle
{
Fill = null,
Outline = null,
Line = { Color = Mapsui.Styles.Color.Blue, Width = 4 }
};
return new VectorStyle { Fill = null, Outline = null, Line = { Color = Mapsui.Styles.Color.Red, Width = 5 } };
});
}```
我不知道你到底想做什么,但如果你想威胁将线集合作为一个几何体,你可以使用 NTS MultiLineString。所以现在称为 CreatePolylines 的方法应该返回一个 MultiLineString。
如果您需要在一个场景中选择一行,而在另一个场景中选择所有行,那么您需要在自己的代码中解决这个问题,Mapsui 没有现成的解决方案。