GMap.Net标记最初位置不正确

时间:2015-05-11 17:22:43

标签: c# gmap.net

我使用GMap添加了一个标记,并指定了lat / long。当应用程序启动时,标记放置在错误的位置(在GMap控件的中心),然后当我缩放时,它将转到指定的坐标。这是GMap中的错误还是我做错了什么?这是代码。

GMapOverlay markersOverlay, mo2;
GMarkerGoogle marker, marker5;
GMapOverlay polyOverlay;
List<PointLatLng> points;
GMapRoute gr;
Graphics g;
bool start = true;
double move = .0001;
double lt = 73, lg = -180;

public Form1()
{
    AllocConsole();
    InitializeComponent();
    try
    {
        System.Net.IPHostEntry e = System.Net.Dns.GetHostEntry("www.google.com");
    }
    catch
    {
        gmap.Manager.Mode = AccessMode.CacheOnly;
        MessageBox.Show("No internet connection avaible, going to CacheOnly mode.", "GMap.NET - Demo.WindowsForms", MessageBoxButtons.OK, MessageBoxIcon.Warning);
    }

    gmap.MapProvider = GMapProviders.BingHybridMap;
    gmap.Position = new PointLatLng(32, -100);
    gmap.MinZoom = 3;
    gmap.MaxZoom = 15;
    gmap.Zoom = 9;
    markersOverlay = new GMapOverlay("markers");
    mo2 = new GMapOverlay("markers5");
    marker5 = new GMarkerGoogle(new PointLatLng(lt, lg), GMarkerGoogleType.orange_small);
    g = this.CreateGraphics();
}

private void Form1_Load(object sender, EventArgs e)
{
    gmap.DragButton = MouseButtons.Left;
    gmap.ShowCenter = false;
    points = new List<PointLatLng>();
    polyOverlay = new GMapOverlay("polygons");
    GMapPolygon polygon = new GMapPolygon(points, "mypolygon");
    polygon.Fill = new SolidBrush(Color.FromArgb(50, Color.Magenta));
    polygon.Stroke = new Pen(Color.Magenta, 2);
}

protected void OnMouseMove(object sender, MouseEventArgs e)
{
    PointLatLng p = gmap.FromLocalToLatLng(e.X, e.Y);
    MouseLatLong.Text = Convert.ToString(p);
}

private void SubmitButton_Click(object sender, EventArgs e)
{
    marker = new GMarkerGoogle(new PointLatLng(double.Parse(LattextBox.Text), double.Parse(LongtextBox.Text)), new Bitmap(@"C:\Users\Vaib\Documents\Visual Studio 2013\Projects\testGmap\testGmap\Resources\wpt.png"));
    mo2.Markers.Add(marker);
    gmap.Overlays.Add(mo2);
    marker.ToolTip = new GMapToolTip(marker);
    marker.ToolTipText = NametextBox.Text;
    marker.ToolTipMode = MarkerTooltipMode.Always;

    if (start)
    {
        gmap.Position = new PointLatLng(marker.Position.Lat, marker.Position.Lng);
        start = false;
    }

    points.Add(new PointLatLng(marker.Position.Lat, marker.Position.Lng));
    gr = new GMapRoute(points, "route");
    gr.Stroke = new Pen(Color.Magenta, 2);
    polyOverlay.Routes.Add(gr);
    gmap.Overlays.Add(polyOverlay);
    ga = new GMarkerArrow(new PointLatLng(gr.From.Value.Lat, gr.From.Value.Lng));

    if (points.Count >= 2)
    {
        ga.Bearing = (float)final(gr.From.Value.Lat, gr.From.Value.Lng, points[1].Lat, points[1].Lng);
    }

    markersOverlay.Clear();
    markersOverlay.Markers.Add(ga);
    gmap.Overlays.Add(markersOverlay);
}   

3 个答案:

答案 0 :(得分:9)

诀窍是首先添加叠加层然后添加标记:

gMapControl.Overlays.Add(markersOverlay); markersOverlay.Markers.Add(标记);

答案 1 :(得分:3)

<强> 解决方案

您可以在评论中阅读:添加

gmap.Overlays.Clear()

在方法的最开始

private void SubmitButton_Click(object sender, EventArgs e)

是他问题的答案。

答案 2 :(得分:1)

我在WinForms应用程序上使用MSVC2010(C ++)并遇到同样的问题 - 下午大部分时间都要解决。

这个帖子很有用,但是我发现你需要做的就是(抱歉它不是C#)在第一次添加标记时被注释掉 - 参见

// DO NOT ADD... line
// Make marker
WindowsForms::Markers::GMarkerGoogle ^MyMarker; 
WindowsForms::Markers::GMarkerGoogleType MyType = safe_cast<WindowsForms::Markers::GMarkerGoogleType>(3); // Blue marker 3
MyMarker = gcnew WindowsForms::Markers::GMarkerGoogle( PointLatLng(40.7, -74.0), MyType);
// MyOverlay->Markers->Add(MyMarker); // DO NOT ADD THE MARKER!!!
gMapControl1->Overlays->Add(MyOverlay);
MyMarker = gcnew WindowsForms::Markers::GMarkerGoogle( PointLatLng(40.7, -74.0), MyType);
MyOverlay->Markers->Add(MyMarker);
gMapControl1->Overlays->Add(MyOverlay);
gMapControl1->ReloadMap();