Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GMap.Net marker initially in incorrect position

Tags:

c#

gmap.net

I have added a marker using GMap with the lat/long specified. When the application starts, the marker is placed in the incorrect position(at the center of the GMap control) and then when I zoom, it then goes 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);
}   
like image 898
Vaibhav Pandya Avatar asked Dec 09 '22 03:12

Vaibhav Pandya


1 Answers

The trick is to first add overlay and then the marker:

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

like image 86
Joe Avatar answered Dec 30 '22 22:12

Joe