VS2017环境下GMap的学习及开发(二)

在GMapcontrol控件中显示地图

接着上一篇,在命名空间加入:

using GMap.NET.MapProviders;

在public Form1() 函数中加入

public Form1()
        {
            InitializeComponent();
            ///gMapControl1  为GMapcontrol控件的变量
            this.gMapControl1.CacheLocation = System.Windows.Forms.Application.StartupPath;//指定地图缓存存放路径
            this.gMapControl1.MapProvider = GMapProviders.GoogleChinaHybridMap;//指定地图类型
            this.gMapControl1.Manager.Mode = AccessMode.ServerAndCache;//地图加载模式
            this.gMapControl1.MinZoom = 1;   //最小比例
            this.gMapControl1.MaxZoom = 23; //最大比例
            this.gMapControl1.Zoom = 12; //当前比例
            this.gMapControl1.ShowCenter = false; //不显示中心十字点
            this.gMapControl1.DragButton = System.Windows.Forms.MouseButtons.Left;//左键拖拽地图
            this.gMapControl1.Position = new PointLatLng(39.92, 116.4);
            
        }

运行结果:
在这里插入图片描述

在地图上标记Marker

这里我们要用到marker,所以首先要加入相应的命名空间:

using GMap.NET.WindowsForms.Markers;

然后定义一个覆盖物层变量:

public GMapOverlay overlay = new GMapOverlay("Marker");

在public Form1()中加入如下代码:

this.gMapControl1.Overlays.Add(overlay);//将图层加入到地图中
this.gMapControl1.MouseDown += new MouseEventHandler(gMapControl1_MouseClick);//为地图添加鼠标点击事件

这里里面的 “gMapControl1_MouseClick”为自定义的鼠标的响应函数,所以我们要加入此函数:

 private void gMapControl1_MouseClick(object sender, MouseEventArgs e)
        {
            PointLatLng p = this.gMapControl1.FromLocalToLatLng(e.X, e.Y);
            GMarkerGoogle marker = new GMarkerGoogle(p, GMarkerGoogleType.arrow);
            marker.ToolTipText = "创建的点";
            this.overlay.Markers.Add(marker);//将点添加到图层
        }

然后运行就可以在地图上打出点来了。

猜你喜欢

转载自blog.csdn.net/Tasteshrimp/article/details/83343936