How to add multiple makers on Gmap in c#

六月ゝ 毕业季﹏ 提交于 2019-12-08 05:13:55

问题


I have a text file with a list of GPS coordinates. I am trying to place a marker on each of the coordinates from the document. The problem is that the lengths of the documents change and the way I have it, the marker gets replaced with every iteration. How do I add a marker for each lat/lon point?

Here's the relevant code:

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

回答1:


The markers can go into the Markers collection:

public readonly ObservableCollection<GMapMarker> Markers;

Just add the markers to the collection as you do with your single marker.

EDIT

I was assuming a WPF client, so there's no Observable Collection if you are using WinForms. Have you tried to add a new marker to the collection as you do with your original marker? So in your loop:

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


来源:https://stackoverflow.com/questions/31325205/how-to-add-multiple-makers-on-gmap-in-c-sharp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!