本章目的主要介绍GMap.NET在实际开发应用当中基本用法,并介绍相关的代码操作:
首先,使用初始化一些变量和事件,初始化代码如下所示:
基本参数介绍:
this.gMapControl1.Zoom 是地图缩放大小
this.gMapControl1.MapType = MapType.GoogleMapChina;地图加载类型
02 | this.gMapControl1.Manager.Mode = AccessMode.ServerAndCache; |
04 | this.gMapControl1.MapType = MapType.GoogleMapChina; |
05 | this.gMapControl1.MaxZoom = 18; |
06 | this.gMapControl1.MinZoom = 8; |
07 | this.gMapControl1.Zoom = 13; |
08 | //下面是主要是手动加载GMap.NET控件所以的时间方法 |
09 | this.gMapControl1.MouseMove += new MouseEventHandler(gMapControl1_MouseMove); |
10 | this.gMapControl1.DoubleClick += new EventHandler(gMapControl1_DoubleClick); |
11 | this.gMapControl1.MouseDown += new MouseEventHandler(this.MainMap_MouseDown); |
12 | this.gMapControl1.MouseUp += new MouseEventHandler(this.MainMap_MouseUp); |
13 | this.gMapControl1.OnCurrentPositionChanged += new CurrentPositionChanged(this.MainMap_OnCurrentPositionChanged); |
14 | this.gMapControl1.OnTileLoadStart += new TileLoadStart(this.MainMap_OnTileLoadStart); |
15 | this.gMapControl1.OnTileLoadComplete += new TileLoadComplete(this.MainMap_OnTileLoadComplete); |
16 | this.gMapControl1.OnMarkerClick += new MarkerClick(this.MainMap_OnMarkerClick); |
17 | this.gMapControl1.OnMapZoomChanged += new MapZoomChanged(this.MainMap_OnMapZoomChanged); |
18 | this.gMapControl1.OnMapTypeChanged += new MapTypeChanged(this.MainMap_OnMapTypeChanged); |
当需要在地图中,生产坐标点或是绘制线路时,就需要生成GMapOverlay图层,初始化图层代码如下所示:
基本参数介绍:

GMapOverlay 是图层类,当需要在地图中设置坐标点绘制线路时,需要生成图层类。
2 | GMapOverlay routes = new GMapOverlay(this.gMapControl1, "routes"); |
3 | this.gMapControl1.Overlays.Add(this.routes); |
5 | GMapOverlay marked= new GMapOverlay(this.gMapControl1, "marked"); |
6 | this.gMapControl1.Overlays.Add(this.marked); |
初始化图层完成后,需要实例化图层里和路线类,代码如下所示:
基本参数介绍:
GMapMarkerGoogleRed 实例化标准图层,为红色图标。
GMapMarkerCross 实例化标准图层,为绿色图标。
MapRoute 实例化线路类
GMapRoute 实例化线路图层类,控制线路图层类参数
02 | GMapMarkerGoogleRed currentMarker = new GMapMarkerGoogleRed(this.gMapControl1.CurrentPosition); |
04 | Marked.Markers.Add(this.currentMarker); |
06 | GMapMarkerCross center = new GMapMarkerCross(this.gMapControl1.CurrentPosition); |
07 | Marked.Markers.Add(this.center); |
09 | //简单介绍需要绘制路线时,需要MapRoute 设置开始点和结束点 |
10 | MapRoute route = Singleton<GMaps>.Instance.GetRouteBetweenPoints(this.start, this.end, false, (int)this.gMapControl1.Zoom); |
12 | GMapRoute item = new GMapRoute(route.Points, route.Name); //参数:坐标点数组,名称 |
13 | item.Color = Color.Blue; |
14 | this.routes.Routes.Add(item); //添加到路线图层中 |
注意:添加标注图层用 Markers 添加路线用 Routes
GMap.NET提供了各种鼠标的操作事件,我们重载即可实现特殊的控制处理了:
01 | private void gMapControl1_DoubleClick(object sender, EventArgs e) |
03 | this.gMapControl1.Zoom += 1; //地图显示大小 |
04 | this.gMapControl1.CurrentPosition = lastPosition; //地图中心点 |
07 | private void gMapControl1_MouseMove(object sender, MouseEventArgs e) |
09 | //获取鼠标移动点,需要转换成PointLatLng 格式 |
10 | PointLatLng latLng = this.gMapControl1.FromLocalToLatLng(e.X, e.Y); |
11 | this.tsslPosition.Text = string.Format("经度:{0}, 纬度:{1} ", latLng.Lng, latLng.Lat); |
14 | private void MainMap_MouseDown(object sender, MouseEventArgs e) |
16 | if (e.Button == MouseButtons.Left) |
18 | this.isMouseDown = true; |
19 | lastPosition = this.gMapControl1.FromLocalToLatLng(e.X, e.Y); |
23 | private void MainMap_MouseUp(object sender, MouseEventArgs e) |
25 | if (e.Button == MouseButtons.Left) |
27 | this.isMouseDown = false; |
下面介绍一下,如果保存截图的操作如下所示:
03 | using (SaveFileDialog dialog = new SaveFileDialog()) |
05 | dialog.Filter = "PNG (*.png)|*.png"; |
06 | dialog.FileName = "GMap.NET image"; |
07 | Image image = this.gMapControl1.ToImage(); |
12 | if (dialog.ShowDialog() == DialogResult.OK) |
14 | string fileName = dialog.FileName; |
15 | if (!fileName.EndsWith(".png", StringComparison.OrdinalIgnoreCase)) |
20 | MessageBox.Show("图片已保存: " + dialog.FileName, "GMap.NET", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); |
26 | catch (Exception exception) |
28 | MessageBox.Show("图片保存失败: " + exception.Message, "GMap.NET", MessageBoxButtons.OK, MessageBoxIcon.Hand); |
(12484)
标签: GMap.NET
好久不见发帖,GMap.net网上的东西不少,都是简单的片面的,真正的重点都没有讲解,需要楼主这样的,我会一直关注的
It above my understanding!
弱弱的问一下,不论采用哪种地图源,如何限定地图的加载范围,比如只加载中国??