GMap marker is placed in the wrong position

Im uses winforms and GMap.NET to find out how to use it.

I have a mouse action on the Gmap controller and when the user clicks on some place on the map, I get the xy coordinates, converting them to latitude and longitude, and then draw a marker on the map. But the marker does not fit in the actual location of the mouse cursor, it looks like the marker has a default location and that it is. I tried to move the mouse to another place, and when I clicked the marker was also created in the wrong place (it was the same as the first marker)

I tried using gmap.Overlays.clear () before getting the coordinates and place the marker, but that didn't help.

 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);
      }
}
0
source share
4 answers

, . .

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

, . , . "" . .

+1

, . Obj.defaultOrigin - LatLong.

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

Just use this code:

myMap.UpdateMarkerLocalPosition(marker)
0
source

You must declare an overlay outside the event mouseclick:

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

      }
}
0
source

Source: https://habr.com/ru/post/1598929/


All Articles