4

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);
}   
ˈvɔlə
  • 9,204
  • 10
  • 63
  • 89
Vaibhav Pandya
  • 115
  • 2
  • 10
  • Is there a refresh() method on gmap? – Nattrass May 11 '15 at 17:25
  • I only ask because it sounds like your display is being invalidated and then redrawn when you zoom. So maybe you can force this to happen – Nattrass May 11 '15 at 17:43
  • Unlike forum sites, we don't use "Thanks", or "Any help appreciated", or signatures on [so]. See "[Should 'Hi', 'thanks,' taglines, and salutations be removed from posts?](http://meta.stackexchange.com/questions/2950/should-hi-thanks-taglines-and-salutations-be-removed-from-posts). – John Saunders May 11 '15 at 21:37
  • Same error here ... I'm just curious: do you use WinForm or WPF version of Gmaps.NET – ˈvɔlə May 19 '15 at 08:55
  • I am using winforms. I found out that this only happens when adding the first marker. so what i did was added a marker before the application starts at a crazy position (like Antarctica so it was not noticeable). then add your markers, it will work. – Vaibhav Pandya May 19 '15 at 13:33
  • Okay, this sounds very strange O_o – ˈvɔlə May 19 '15 at 14:04
  • Have a look at your other question. The issue is that you'll (still) need to add the overlay to the map first, and after that start adding a marker to that specific overlay. It's as easy as switching around two statements. – rdoubleui Aug 20 '15 at 09:39

3 Answers3

11

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

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

Joe
  • 118
  • 2
  • 6
3

Solution

Like 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.

ˈvɔlə
  • 9,204
  • 10
  • 63
  • 89
  • I tried but didn't work. This is what I have. GMapOverlay markersOverlay = new GMapOverlay("markers"); GMapOverlay mo2 = new GMapOverlay("markers5"); – Vaibhav Pandya May 19 '15 at 13:39
  • Are you calling these lines of codes multiple times? Maybe you do execute them inside an event (for example click) which is accidently called too often. I am pretty sure I am able to help you, since I had _exactly_ the same misbehaviour like you. – ˈvɔlə May 19 '15 at 14:02
  • It is also possible that you are calling `gmap.Overlays.Add(markerOverlay);` too often ... Was also part of my mistake :/ – ˈvɔlə May 19 '15 at 14:05
  • Thanks for helping Wolle. I have added more code to the original post if you can figure out the problem. – Vaibhav Pandya May 19 '15 at 14:25
  • @VaibhavPandya I am working with it since last week. I am totally in love with it, although I cannot find any useful documentation for it :/ I already tested lot of features and everything is working for me. I am not the best programmer, but I am able to get used to new frameworks/project/architectures/libraries very fast. – ˈvɔlə May 19 '15 at 14:44
  • @VaibhavPandya - Try adding `gmap.Overlays.Clear()` at the very beginning of the method `private void SubmitButton_Click(object sender, EventArgs e)`. Hope it helps :/ – ˈvɔlə May 19 '15 at 14:51
  • @Wolle Ok it works, thanks a bunch. You think you can help me with other problems with GMap. I am trying to add images to the map but having trouble. – Vaibhav Pandya May 19 '15 at 15:16
  • @VaibhavPandya Was my solution the answer to the question? I don't know if I am able to help you with this problem. Just post a new question :) – ˈvɔlə May 20 '15 at 06:43
  • @VaibhavPandya So maybe you should consider to mark my post as the answer :) – ˈvɔlə May 20 '15 at 14:06
1

I'm working in MSVC2010 (C++) on a WinForms app and had the same problem - took most of the afternoon to resolve.

This thread was useful, but I find all you need to do is (sorry it's not C#) is comment out the first time you add the 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();
MakePeaceGreatAgain
  • 35,491
  • 6
  • 60
  • 111
Joao
  • 11
  • 1
  • I've been trying to figure out how to refresh the map display. This was helpful, especially the ReloadMap (I had thought it should have been Refresh). – blearyeye Jul 22 '16 at 11:35