Denis Catovic
Denis Catovic

Reputation: 3

c# gmap.net google marker

I made an application that uses gmap.net. On the map I have three markers. Now what I'm trying to do is to click on a marker opens a new form, click on the second marker to open another form. This is the code:

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

        }
    }
}

Upvotes: 0

Views: 3182

Answers (2)

Mohsen
Mohsen

Reputation: 1

From the properties of GMapControl, go to Events. There is an event called OnMarkerClick. Ckick on it to create an event as follows:

private void gmap_OnMarkerClick(GMapMarker item, MouseEventArgs e) {

    if (item.Equals(marker1))
    {
       MessageBox.Show("marker1 clicked");
       // do something else
     
    }
    else if (item.Equals(marker2))
    {
        MessageBox.Show("marker2 clicked");
        // do something else
    }
   ...........
    
}

Upvotes: 0

Bearcat9425
Bearcat9425

Reputation: 1598

The gMapControl has a event called OnMarkerClick that you can subscribe to to listen for click events on your makers. You can right click your GmapControl and then select properties. Then Click the lightening bolt button and that will list your events and in there is a OnMarkerClick Event you can double click it and it will build a event handler for you, or you can set it like so.

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

Upvotes: 1

Related Questions