GMap.Net marker initially in incorrect position
我用GMAP添加了一个带有指定lat/long的标记。当应用程序启动时,标记被放置在错误的位置(在gmap控件的中心),然后当我缩放时,它将转到指定的坐标。这是GMAP中的错误还是我做错了什么?这是密码。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 | 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 esources\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); } |
技巧是先添加覆盖,然后添加标记:
gmapcontrol.overlays.add(标记覆盖);markersoverlay.markers.add(标记);
解决方案
就像你可以在评论中看到的:添加
1 | gmap.Overlays.Clear() |
在方法的最开始
1 | private void SubmitButton_Click(object sender, EventArgs e) |
是他问题的答案。
我在一个WiFrm应用程序中工作在MSVC2010(C++)上,同样的问题,大部分时间都在解决。
这个线程很有用,但我发现您需要做的只是(抱歉,它不是c)在您第一次添加标记时注释掉-请参见
1 2 3 4 5 6 7 8 9 10 11 | // 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(); |