问题
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);
}
}
回答1:
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.
回答2:
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;
}
};
回答3:
Just use this code:
myMap.UpdateMarkerLocalPosition(marker)
回答4:
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);
}
}
来源:https://stackoverflow.com/questions/30636745/gmap-marker-is-placed-in-wrong-position