GMap marker is placed in wrong position
Asked Answered
C

4

1

Im using winforms and GMap.NET in order to learn how to use it.

I have a mouse click action on the Gmap controller and when the user clicks on some place on the map i'm getting the x y coordinates, converting them to latitude and longtitude and then draw the marker on the map. But the marker is not placed in the real mouse cursor location, it looks like the marker has a default place and that's it. I tried to move the mouse to another place and when I clicked the marker was also created at wrong place (it was the same as the first marker)

I tried to use gmap.Overlays.clear() before getting the coordinates and place the marker but this wasn't helpful.

 private void gmap_MouseClick(object sender, MouseEventArgs e)
 {

      if (e.Button == System.Windows.Forms.MouseButtons.Left)
      {
          double lat = gmap.FromLocalToLatLng(e.X, e.Y).Lat;
          double lng = gmap.FromLocalToLatLng(e.X, e.Y).Lng;

          GMapOverlay markerOverlay = new GMapOverlay("markers");

          GMarkerGoogle marker = new GMarkerGoogle(new  
                               GMap.NET.PointLatLng(lat, lng), 
                               GMarkerGoogleType.green_pushpin);

          markerOverlay.Markers.Add(marker);
          gmap.Overlays.Add(markerOverlay);
      }
}
Caspar answered 4/6, 2015 at 6:43 Comment(0)
C
2

Add the overlay first, then add the marker. No need to do extra operations.

gmap.Overlays.Add(markerOverlay);
markerOverlay.Markers.Add(marker);

By switching around the statements you'll achieve the right positioning. The guess about a default position is somewhat true, I guess. The overlay has not been "hooked" to the map and gets a marker positioned in it beforehand. That's why the position is usually off initially.

Corr answered 20/8, 2015 at 9:35 Comment(0)
I
1

Just use this code:

myMap.UpdateMarkerLocalPosition(marker)
Illsorted answered 15/7, 2015 at 6:56 Comment(0)
S
0

This is how i do it and it works fine. The Obj.defaultOrigin is just LatLong location.

gm = new GoogleMap(Obj.defaultOrigin);
overlay = new GMapOverlay(gm, "mapIcon");
marker = new GoogleMap.GMapMarkerImage(Obj.defaultOrigin, Image.FromFile(Obj.path + @"\resources\images\mapIcon.png"));
overlay.Markers.Add(marker);
gm.Overlays.Add(overlay);
gm.MouseClick += (s, e) =>
{
    if (e.Button == System.Windows.Forms.MouseButtons.Right)
    {
        GMap.NET.PointLatLng point = gm.FromLocalToLatLng(e.X, e.Y);
        marker.Position = point;
    }
};
Shrapnel answered 4/6, 2015 at 7:18 Comment(1)
thanks for the answer, but it doesn't work for me.. I gave a default values for lng and lat, and when I clicked on some place in the map, the marker was dissapeardCaspar
K
0

You should declare overlay outside mouseclick event:

GMapOverlay markersOverlay = new GMapOverlay("markers"); 

private void gmap_MouseClick(object sender, MouseEventArgs e)
 {

      if (e.Button == System.Windows.Forms.MouseButtons.Left)
      {
          double lat = gmap.FromLocalToLatLng(e.X, e.Y).Lat;
          double lng = gmap.FromLocalToLatLng(e.X, e.Y).Lng;

          // GMapOverlay markerOverlay = new GMapOverlay("markers"); Your code here

          GMarkerGoogle marker = new GMarkerGoogle(new  
                               GMap.NET.PointLatLng(lat, lng), 
                               GMarkerGoogleType.green_pushpin);
          gmap.Overlays.Add(markerOverlay); //Change position of this line first 
          markerOverlay.Markers.Add(marker); 

      }
}
Kahler answered 16/9, 2016 at 4:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.