How to return Lat/Long with MouseMove in GMap.net

user2791846 picture user2791846 · Sep 18, 2013 · Viewed 8.6k times · Source

I am trying to put together a c# program with GMap, and I'd like the coordinates where the mouse is to show up on the bottom of the screen. I've added an OnMouseMove method to the form, and I do get coordinates out, but only if the mouse is not over the map itself. If the mouse is over the map it does not respond. I am fairly new to c#, so I am probably missing something fairly simple. Any ideas? Below is the code I'm using right now.

    public partial class Form1 : Form
{
    protected override void OnMouseMove(System.Windows.Forms.MouseEventArgs e)
    {
        base.OnMouseMove(e);

        if(e.Button == MouseButtons.Left)
        {
            int itest=2;
        }

        double X = mapexplr.FromLocalToLatLng(e.X, e.Y).Lng;
        double Y = mapexplr.FromLocalToLatLng(e.X, e.Y).Lat;


        string longitude = X.ToString();
        string latitude = Y.ToString();
        LongStrip.Text = longitude;
        LatStrip.Text = latitude;
    }

    GMapOverlay overlayOne;

    public Form1()
    {
        InitializeComponent();
    }

    private void mapexplr_Load(object sender, EventArgs e)
    {
        //initialisation de notre map
        mapexplr.MapProvider = GMap.NET.MapProviders.BingMapProvider.Instance;
        GMap.NET.GMaps.Instance.Mode = GMap.NET.AccessMode.ServerAndCache;
        mapexplr.Position = new PointLatLng(35.571458, -85.547961);

        mapexplr.DragButton = MouseButtons.Left;
        mapexplr.SetCurrentPositionByKeywords("Tunisia");
        mapexplr.MapProvider = GMapProviders.BingMap;
        mapexplr.MinZoom = 3;
        mapexplr.MaxZoom = 17;
        mapexplr.Zoom = 5;
        mapexplr.Manager.Mode = AccessMode.ServerAndCache;
        //ajout des overlay
        overlayOne = new GMapOverlay(mapexplr, "OverlayOne");
        //ajout de Markers
        overlayOne.Markers.Add(new GMap.NET.WindowsForms.Markers.GMapMarkerGoogleGreen(new PointLatLng(36.657403, 10.327148)));
        //ajout de overlay à la map
        mapexplr.Overlays.Add(overlayOne);

    }
}

Answer

Cenk Gün picture Cenk Gün · Apr 15, 2015
private void gMapControl1_MouseMove(object sender, MouseEventArgs e)
    {
        lat = gMapControl1.FromLocalToLatLng(e.X, e.Y).Lat;
        lng = gMapControl1.FromLocalToLatLng(e.X, e.Y).Lng;
        label1.Text = "lat= " + Convert.ToString(lat)+ "   lng= " +Convert.ToString(lng);
        label1.BackColor = Color.Transparent;

        mouseY = e.Location.Y;
        mouseX = e.Location.X;
        label1.Location = new Point(mouseX, mouseY+10); 



    }