【问题标题】:How to return Lat/Long with MouseMove in GMap.net如何在 GMap.net 中使用 MouseMove 返回 Lat/Long
【发布时间】:2013-09-23 08:50:35
【问题描述】:

我正在尝试用 GMap 组合一个 c# 程序,我希望鼠标的坐标显示在屏幕底部。我在表单中添加了一个 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);

    }
}

【问题讨论】:

    标签: onmousemove gmap.net


    【解决方案1】:
    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); 
    
    
    
        }
    

    【讨论】:

      【解决方案2】:

      您使用的鼠标移动事件是针对 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。

      【讨论】:

        猜你喜欢
        • 2016-09-18
        • 1970-01-01
        • 2018-11-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多