尝试在WPF应用程序中创建显示路线的Map

x9ybnkn6  于 2023-10-22  发布在  其他
关注(0)|答案(1)|浏览(157)

我正在创建一个应用程序,它从CSV导入一组值,包括测量时的一组坐标。我从CSV文件中创建了一个对象列表,并试图创建一个提取的坐标列表来在Map上创建路线,因此我试图从其他人那里找到如何创建路线的方法,但在我的情况下没有任何效果

public partial class RouteWindow : Window
{
    private GMapControl gmapControl;

    public RouteWindow(List<BaseCsvData> list)
    {
        InitializeComponent();

        List<(double Latitude, double Longitude)> coordinates = 
            list.Select(data => (data.Latitude, data.Longitude)).ToList();

        gmapControl = new GMapControl();

        gmapControl.Width = 800;
        gmapControl.Height = 460;

        // init
        gmapControl.MapProvider = GMapProviders.OpenStreetMap;
        GMaps.Instance.Mode = AccessMode.ServerOnly;
        gmapControl.MinZoom = 1;
        gmapControl.MaxZoom = 18;
        gmapControl.Zoom = 12;
        gmapControl.Position = new PointLatLng(coordinates[0].Latitude, coordinates[0].Longitude);

        // markers
        foreach (var (latitude, longitude) in coordinates)
        {
            GMapMarker marker = new GMapMarker(new PointLatLng(latitude, longitude));
            gmapControl.Markers.Add(marker);
        }

        // list of points
        List<PointLatLng> routePoints = coordinates.Select(c => new PointLatLng(c.Latitude, c.Longitude)).ToList();

        // add GMapControl to grid
        Grid grid = new Grid();
        grid.Children.Add(gmapControl);
        this.Content = grid;

        // zooming
        gmapControl.MouseWheel += GmapControl_MouseWheel;

        this.Closed += (sender, e) =>
        {
            DisposeMapControl();
        };
    }

    private void GmapControl_MouseWheel(object sender, MouseWheelEventArgs e)
    {
        e.Handled = true;

        if (e.Delta > 0)
        {
            // Zoom in
            gmapControl.Zoom += 1;
        }
        else if (e.Delta < 0)
        {
            // Zoom out, with a minimum zoom level
            if (gmapControl.Zoom > 1)
            {
                gmapControl.Zoom -= 1;
            }
        }
    }

    public void DisposeMapControl()
    {
        gmapControl.Dispose();
    }
}

我尝试了一些其他在线项目的代码,一些来自WinForms,我试图向ChatGPT寻求帮助,但它返回

// Create a route
        GMapRoute route = new GMapRoute(routePoints, "Route");
        route.Stroke = new Pen(Brushes.Blue, 3).Frozen; // Set the color and thickness of the route line
        gmapControl.Overlays.Add(route);

Stroke 'GMapRoute'不包含'Stroke'的定义,并且找不到接受类型'GMapRoute'的第一个参数的可访问扩展方法'Stroke'(您是否缺少using指令或程序集引用?)和叠加的错误。我对C#比较陌生,正在使用一些Map库,所以我非常感谢任何建议

epggiuax

epggiuax1#

要将任何对象添加到map,可以使用gmapControl.Markers.Add。尽管看起来只能添加GMapMarker对象,但其他对象也继承自GMapMarker(在本例中为GMapRoute)。
可以通过GMapRoute.Shape属性更改视觉样式。

GMapRoute route = new GMapRoute(routePoints);
route.Shape = new Path() { Stroke = new SolidColorBrush(Colors.Blue), StrokeThickness = 3 };
gmapControl.Markers.Add(route);

可以以类似方式设置点标记样式:

// markers
foreach (var (latitude, longitude) in coordinates)
{
    GMapMarker marker = new GMapMarker(new PointLatLng(latitude, longitude));
    marker.Shape = new Ellipse() 
    { 
        Fill = new SolidColorBrush(Colors.Red),
        Width = 8,
        Height = 8,
        // Without Margin, circles would not be centered.
        Margin = new Thickness(-4, -4, 0, 0) 
    };

    gmapControl.Markers.Add(marker);
}

相关问题