当用户在GMap.net中移动时,如何获得单个标记来更新它的位置

时间:2017-07-06 13:33:50

标签: c#

string lat = "40.682640,40.682671,40.682701,40.682732,40.682763,40.682794";
string lng = "-73.868470,-73.868359,-73.868247,-73.868136,-73.868025,-73.867913";
int index = 0;
private void Form1_Load(object sender, EventArgs e)
{
    Timer MyTimer = new Timer();
    MyTimer.Interval = (2000);
    MyTimer.Tick += new EventHandler(MyTimer_Tick);
    MyTimer.Start();
} 
private void MyTimer_Tick(object sender, EventArgs e)
{

    string[] Latitude = lat.Split(',');
    string[] Longitude = lng.Split(',');

    gmap.MapProvider = GMap.NET.MapProviders.BingMapProvider.Instance;
    GMap.NET.GMaps.Instance.Mode = GMap.NET.AccessMode.ServerOnly;
    GMapProvider.WebProxy = null;
    gmap.SetPositionByKeywords("Atlantic Ave,USA");
    gmap.Position = new GMap.NET.PointLatLng(40.683377, -73.865798);
    gmap.ShowCenter = false;
    GMapOverlay markers = new GMapOverlay("markers");
    GMapMarker marker = new GMarkerGoogle(new PointLatLng(float.Parse(Latitude[index]), float.Parse(Longitude[index])), GMarkerGoogleType.blue);

    gmap.Overlays.Add(markers);
    markers.Markers.Add(marker);
    index++;

    GMapMarker marker1 = new GMarkerGoogle(new PointLatLng(40.684175, -73.862904), GMarkerGoogleType.green);

    markers.Markers.Add(marker1);
}

当用户移动而另一个标记保持静止时,我需要让单个标记更新其位置(蓝色标记)。我已尝试markers.Markers.remove(marker);,但所有标记都已移除。

2 个答案:

答案 0 :(得分:0)

根据source code,以下内容应更新位置:

marker.Position = new PointLatLng(lat, lng);

GMarkerGoogle继承自GMapMarkerpublic PointLatLng PositionPosition的设定者是这样的:

set
{
    if(position != value)
    {
        position = value;
        if(IsVisible)
        {
            if(Overlay != null && Overlay.Control != null)
            {
                Overlay.Control.UpdateMarkerLocalPosition(this);
            }
        }
    }
}

注意Overlay.Control.UpdateMarkerLocalPosition(this);

答案 1 :(得分:0)

当我在MyTimer_Tick方法之外声明标记时,我得到了单个标记更新它的位置

    string lat = "40.682640,40.682671,40.682701,40.682732,40.682763,40.682794,40.682824,40.682855,40.682886,40.682916,40.682947,40.682978,40.683008,40.683039,40.683070,40.683101,40.683131,40.683162,40.683193,40.683223,40.683254,40.683285,40.683316,40.683346,40.683377,40.683408,40.683438,40.683469,40.683500,40.683530,40.683561,40.683592,40.683623,40.683653,40.683684,40.683715,40.683745,40.683776,40.683807,40.683838,40.683868,40.683899,40.683930,40.683960,40.683991,40.684022,40.684053,40.684083,40.684114,40.684145,40.684175";
    string lng = "-73.868470,-73.868359,-73.868247,-73.868136,-73.868025,-73.867913,-73.867802,-73.867691,-73.867579,-73.867468,-73.867357,-73.867246,-73.867134,-73.867023,-73.866912,-73.866800,-73.866689,-73.866578,-73.866466,-73.866355,-73.866244,-73.866132,-73.866021,-73.865910,-73.865798,-73.865687,-73.865576,-73.865464,-73.865353,-73.865242,-73.865130,-73.865019,-73.864908,-73.864797,-73.864685,-73.864574,-73.864463,-73.864351,-73.864240,-73.864129,-73.864017,-73.863906,-73.863795,-73.863683,-73.863572,-73.863461,-73.863349,-73.863238,-73.863127,-73.863015,-73.862904";
    int index = 0;

    GMapOverlay markers = new GMapOverlay("markers");
    GMapMarker marker = new GMarkerGoogle(new PointLatLng(40.682640, -73.868470), GMarkerGoogleType.blue);

    public Form2()
    {
        InitializeComponent();
    }

    private void Form2_Load(object sender, EventArgs e)
    {
        System.Windows.Forms.Timer MyTimer = new System.Windows.Forms.Timer();
        MyTimer.Interval = (1000);
        MyTimer.Tick += new EventHandler(MyTimer_Tick);
        MyTimer.Start();    //calling the timer
    }

    private void MyTimer_Tick(object sender, EventArgs e)
    { 
        //adding google map
        string[] Latitude = lat.Split(',');
        string[] Longitude = lng.Split(',');
        gmap.MapProvider = BingMapProvider.Instance;
        GMaps.Instance.Mode = GMap.NET.AccessMode.ServerOnly;
        GMapProvider.WebProxy = null;
        gmap.SetPositionByKeywords("Atlantic Ave,USA");
        gmap.Position = new GMap.NET.PointLatLng(40.683377, -73.865798);
        gmap.ShowCenter = false;
        gmap.Overlays.Add(markers);
        markers.Markers.Add(marker);  //adding the blue marker
        index++;
        marker.Position = new PointLatLng(float.Parse(Latitude[index]), float.Parse(Longitude[index]));
        GMapMarker marker1 = new GMarkerGoogle(new PointLatLng(40.684175, -73.862904), GMarkerGoogleType.green);
        markers.Markers.Add(marker1);  //adding the green marker 
    }

但地图每隔几秒钟就会重新开始。有没有办法下载地图并离线使用,以避免重绘地图。