c# gmap.net google marker
我做了一个使用gmap.net的应用程序。在地图上我有三个记号笔。现在我要做的是单击一个标记打开一个新表单,单击第二个标记打开另一个表单。这是代码:
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 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 | using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using GMap.NET; using GMap.NET.MapProviders; using GMap.NET.WindowsForms; using GMap.NET.WindowsForms.Markers; namespace GMap { public partial class Form1 : Form { GMarkerGoogle marker; GMapOverlay markerOverlay; DataTable dt; int Selekcija = 0; double LatInicial = 43.1383292506958; double LngInicial = 20.5198994278908; double LatTehnicka = 43.1378458151015; double LngTehnicka = 20.5214631557465; double LatMedicinska = 43.1324426240355; double LngMedicinska = 20.5122631788254; public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { dt = new DataTable(); dt.Columns.Add(new DataColumn("Opis", typeof(string))); dt.Columns.Add(new DataColumn("Lat", typeof(double))); dt.Columns.Add(new DataColumn("Long", typeof(double))); //Ubacivanje podataka u tabelu dt.Rows.Add("Gimnazija", LatInicial, LngInicial); dt.Rows.Add("Tehnicka skola", LatTehnicka, LngTehnicka); dt.Rows.Add("Medicinska skola", LatMedicinska, LngMedicinska); dataGridView1.DataSource = dt; //Vidljivost pojedinih kolona dataGridView1.Columns[1].Visible = false; dataGridView1.Columns[2].Visible = false; gMapControl1.DragButton = MouseButtons.Left; gMapControl1.CanDragMap = true; gMapControl1.MapProvider = GMapProviders.GoogleMap; gMapControl1.Position = new PointLatLng(LatInicial, LngInicial); gMapControl1.MinZoom = 0; gMapControl1.MaxZoom = 24; gMapControl1.Zoom = 17; gMapControl1.AutoScroll = true; // Obelezivac markerOverlay = new GMapOverlay("markers"); marker = new GMarkerGoogle(new PointLatLng(LatInicial, LngInicial),GMarkerGoogleType.green); markerOverlay.Markers.Add(marker); //marker.ToolTipMode = MarkerTooltipMode.Always; marker.ToolTipText = string.Format("Gimnazija: Latituda: {0} Longituda: {1}", LatInicial, LngInicial); gMapControl1.Overlays.Add(markerOverlay); markerOverlay = new GMapOverlay("markers"); marker = new GMarkerGoogle(new PointLatLng(LatTehnicka, LngTehnicka), GMarkerGoogleType.green); markerOverlay.Markers.Add(marker); //marker.ToolTipMode = MarkerTooltipMode.Always; marker.ToolTipText = string.Format("Tehnicka skola: Latituda: {0} Longituda: {1}", LatTehnicka, LngTehnicka); gMapControl1.Overlays.Add(markerOverlay); markerOverlay = new GMapOverlay("markers"); marker = new GMarkerGoogle(new PointLatLng(LatMedicinska, LngMedicinska), GMarkerGoogleType.green); markerOverlay.Markers.Add(marker); //marker.ToolTipMode = MarkerTooltipMode.Always; marker.ToolTipText = string.Format("Medicinska skola: Latituda: {0} Longituda: {1}", LatMedicinska, LngMedicinska); gMapControl1.Overlays.Add(markerOverlay); } private void button1_Click(object sender, EventArgs e) { dt.Rows.Add(txtOpis.Text, txtLatituda.Text, txtLongituda.Text); } private void SelekcijaSkole(object sender, DataGridViewCellMouseEventArgs e) { Selekcija = e.RowIndex; txtOpis.Text = dataGridView1.Rows[Selekcija].Cells[0].Value.ToString(); txtLatituda.Text = dataGridView1.Rows[Selekcija].Cells[1].Value.ToString(); txtLongituda.Text = dataGridView1.Rows[Selekcija].Cells[2].Value.ToString(); marker.Position = new PointLatLng(Convert.ToDouble(txtLatituda.Text), Convert.ToDouble(txtLongituda.Text)); gMapControl1.Position = marker.Position; } private void gMapControl1_MouseDoubleClick(object sender, MouseEventArgs e) { double lat = gMapControl1.FromLocalToLatLng(e.X, e.Y).Lat; double lng = gMapControl1.FromLocalToLatLng(e.X, e.Y).Lng; txtLatituda.Text = lat.ToString(); txtLongituda.Text = lng.ToString(); marker.Position = new PointLatLng(lat, lng); marker.ToolTipText = string.Format("Koordinate: Latituda {0} Longituda {1}", lat, lng); } private void button2_Click(object sender, EventArgs e) { dataGridView1.Rows.RemoveAt(Selekcija); } private void button3_Click(object sender, EventArgs e) { gMapControl1.MapProvider = GMapProviders.GoogleChinaSatelliteMap; } private void button4_Click(object sender, EventArgs e) { gMapControl1.MapProvider = GMapProviders.GoogleMap; } private void button5_Click(object sender, EventArgs e) { gMapControl1.MapProvider = GMapProviders.GoogleTerrainMap; } private void gMapControl1_OnMarkerClick(GMapMarker item, MouseEventArgs e) { } } } |
gmapcontrol有一个名为onmarkerclick的事件,您可以订阅它来监听制造商的点击事件。您可以右键单击gmapcontrol,然后选择属性。然后单击闪电按钮,它将列出您的事件,在这里有一个onmarkerclick事件,您可以双击它,它将为您构建一个事件处理程序,或者您可以这样设置它。
1 2 3 4 5 6 7 8 9 10 | gMapControl1.OnMarkerClick += (marker, mouseArgs) => { // From this point marker is the clicked marker do as you wish here // Pass it to another form and use form.show to display the form. // MessageBox.Show is to show proof the event fired. MessageBox.Show(marker.ToolTipText); // If you have a marker form you can display it like so. MarkerForm form = new MarkerForm(); form.Show(); }; |