1

我有一个带有 GPS 坐标列表的文本文件。我正在尝试在文档中的每个坐标上放置一个标记。问题是文档的长度和我拥有它的方式发生了变化,每次迭代都会替换标记。如何为每个纬度/经度点添加标记?

以下是相关代码:

    private GMapOverlay gMapOverlay;
    private GMapMarker marker;
        gmap.MapProvider = GMap.NET.MapProviders.GoogleMapProvider.Instance;
        gmap.MinZoom = 2;
        gmap.MaxZoom = 25;
        gmap.Zoom = 5;
        gmap.ShowCenter = false;
        gmap.DragButton = MouseButtons.Left;

        //setup the map overlay for displaying routes/points
        gMapOverlay = new GMapOverlay("Path");
        gmap.Overlays.Add(gMapOverlay);
        gMapOverlay.Markers.Clear();
        gMapOverlay.Routes.Clear();

        //GMarkerGoogle marker = new GMarkerGoogle(new PointLatLng(0, 0), GMarkerGoogleType.green);
        marker = new GMarkerGoogle(new PointLatLng(0, 0), GMarkerGoogleType.green);
        marker.IsVisible = false;
        marker.ToolTipMode = MarkerTooltipMode.OnMouseOver;
        marker.ToolTipText = "Starting Point";
        gMapOverlay.Markers.Add(marker);

    private void btn_KMLFile_Click(object sender, EventArgs e)
    {
        DialogResult result = openFileDialog4.ShowDialog();
        if (result == DialogResult.OK)
        {
            string filename = openFileDialog4.FileName;
            string[] lines = System.IO.File.ReadAllLines(filename);
            foreach (string line in lines)
            {
                string[] Data_Array = line.Split(',');
                Double londecimal = Convert.ToDouble(Data_Array[0]);
                Double latdecimal = Convert.ToDouble(Data_Array[1]);
                marker.Position = new PointLatLng(latdecimal, londecimal);
                marker.IsVisible = true;
                gmap.Update();


            }
        }
    }

    private void openFileDialog4_FileOk(object sender, CancelEventArgs e)
    {
        OpenFileDialog openFileDialog4 = new OpenFileDialog();
    }
4

2 回答 2

2

标记可以进入Markers集合:

public readonly ObservableCollection<GMapMarker> Markers;

只需像使用单曲一样将标记添加到集合中marker

编辑

我假设一个 WPF 客户端,所以Observable Collection如果您使用的是 WinForms,则没有。您是否尝试像使用原始标记一样将新标记添加到集合中?所以在你的循环中:

string[] Data_Array = line.Split(',');
Double londecimal = Convert.ToDouble(Data_Array[0]);
Double latdecimal = Convert.ToDouble(Data_Array[1]);
// add a new one here
var marker = new GMarkerGoogle(new PointLatLng(latdecimal, londecimal), GMarkerGoogleType.green);
marker.IsVisible = true;
gMapOverlay.Markers.Add(marker);
于 2015-07-09T19:38:08.420 回答
0

我可以使用列表修复

  List<GMapMarker> lMarks = new List<GMapMarker>();
            int[] SelectedRows = gvProjects.GetSelectedRows();
            map.Overlays.Clear();
            GMapOverlay markers = new GMapOverlay("markers");
            for (int i = 0; i < SelectedRows.Length; i++)
            {
                if (SelectedRows[i] >= 0)
                {
                    double lat = Convert.ToDouble(gvProjects.GetRowCellValue(i-2,"Altitude"));
                    double lng = Convert.ToDouble(gvProjects.GetRowCellValue(i-2, "Longitude"));
                    
                    PointLatLng point = new PointLatLng(lat, lng);
                    GMapMarker marker = new GMarkerGoogle(new PointLatLng(lat,lng),GMarkerGoogleType.yellow_pushpin);
                    marker.ToolTipMode = MarkerTooltipMode.Always;
                    marker.Tag = gvProjects.GetRowCellValue(i-2, "ID").ToString();
                    marker.ToolTipText = gvProjects.GetRowCellValue(i-2, "ProjectName").ToString();
                    lMarks.Add(marker);                       
                }
            }
            markers.Markers.AddRange(lMarks);
            map.Overlays.Add(markers);       
于 2020-08-05T14:23:27.457 回答