Initially GMap.Net marker is in the wrong position

I added a marker using a GMap with latitude / longitude. When the app starts, the marker is placed in the wrong position (in the center of the GMap control), and then when I scale it, it jumps to the specified coordinates. Is this a bug in GMap or am I doing something wrong? Here is the code

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

      

+4


source to share


3 answers


The trick is to add an overlay first and then a marker:



gMapControl.Overlays.Add (markersOverlay); markersOverlay.Markers.Add (marker);

+9


source


Decision

As you can read in the comments: Adding

gmap.Overlays.Clear()

      



at the very beginning of the method

private void SubmitButton_Click(object sender, EventArgs e)

      

was the answer to his problem.

+3


source


I am working in MSVC2010 (C ++) in a WinForms application and had the same problem - most of the day decided to solve it.

This thread was helpful, but I find all you need to do (sorry not C #) is a comment when you first add a marker - see

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

      

+1


source







All Articles