c#gmap.net google marker

时间:2017-05-22 20:46:26

标签: c# gmap.net

我创建了一个使用gmap.net的应用程序。在地图上我有三个标记。现在我要做的是点击一个标记打开一个新表格,点击第二个标记打开另一个表格。这是代码:

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: \n Latituda: {0} \n     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: \n Latituda: {0} \n 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: \n Latituda: {0} \n 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: \n Latituda {0} \n 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)
        {

        }
    }
}

1 个答案:

答案 0 :(得分:1)

gMapControl有一个名为OnMarkerClick的事件,您可以订阅该事件来监听您的制造商的点击事件。您可以右键单击GmapControl,然后选择属性。然后单击闪电按钮,这将列出您的事件,并且有一个OnMarkerClick事件,您可以双击它,它将为您构建一个事件处理程序,或者您可以这样设置它。

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