6

我正在尝试将 ac# 程序与 GMap 放在一起,我希望鼠标显示在屏幕底部的坐标。我在表单中添加了一个 OnMouseMove 方法,并且我确实得到了坐标,但前提是鼠标不在地图本身上方。如果鼠标在地图上,它不会响应。我对 c# 相当陌生,所以我可能遗漏了一些相当简单的东西。有任何想法吗?下面是我现在正在使用的代码。

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

    }
}
4

2 回答 2

3
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); 



    }
于 2015-04-15T09:50:00.283 回答
1

您使用的鼠标移动事件是针对 Form 而不是 Gmap。只需将您的代码粘贴到 Gmap.Net 鼠标移动事件中即可。此外,您不应该在加载事件中初始化 Gmap,看起来您设置了两次地图类型和两次地图位置(一次在突尼斯,一次在 35.571458,-85.547961)。参考以下:

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

此代码已经过测试,因此如果您有任何问题,请告诉我。如果您还没有,请记住参考 GMap.NET.Core 和 GMap.NET.WindowsForms。

于 2013-09-24T11:35:58.797 回答