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);
}
}
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);
}
The mouse move event that you used is for the Form not the Gmap. Just take your code and paste is into the Gmap.Net mouse move event. Also you shouldnt initialize your Gmap in a load event and it looks like you were setting the map type twice and your map location twice (once in tunisia and once at 35.571458, -85.547961). Refer below:
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 Code_Test
{
public partial class Form1 : Form
{
GMapOverlay overlayOne = new GMapOverlay();
public Form1()
{
InitializeComponent();
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.MinZoom = 3;
mapexplr.MaxZoom = 17;
mapexplr.Zoom = 5;
}
private void Form1_Load(object sender, EventArgs e)
{
GMarkerGoogle marker = new GMarkerGoogle(new PointLatLng(36.657403, 10.327148), GMarkerGoogleType.green);
overlayOne.Markers.Add(marker);
mapexplr.Overlays.Add(overlayOne);
}
private void mapexplr_MouseMove(object sender, MouseEventArgs e)
{
base.OnMouseMove(e);
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;
}
}
}
This code is tested so if you have any problems let me know. Remember to make a reference to GMap.NET.Core and GMap.NET.WindowsForms if you havent already.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With