如何在c#中添加多个制作者?

时间:2015-07-09 18:15:04

标签: c# gmap.net

我有一个带有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();
    }

2 个答案:

答案 0 :(得分:2)

标记可以进入Markers集合:

public readonly ObservableCollection<GMapMarker> Markers;

只需将标记添加到集合中,就像使用单个marker一样。

修改

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

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);

答案 1 :(得分: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);