GMap学习笔记

1、GMap体系详解

  • What is the map control (GMapControl)? This is the control which renders the map.
  • What is an Overlay (GMapOverlay)? This is a layer on top of the map control. You can have several layers on top of a map, each layer representing, say, a route with stops, a list    of stores etc.
  • What are Markers (GMapMarker)? These are the points on a layer, each representing a specific geo location (Lat,Lon) e.g. each drop point on a route.
  • What is a route (GMapRoute)? This is the path or direction between two or more poin

2、c# 使用GMap 实现具体的功能(加载地图、放大、缩小、鹰眼、添加点线面、自定义marker、截图、下载缓存)

注:添加GMap.NET.Core.dll 和 GMap.NET.WindowsForms.dll文件,引用后使用GMap的控件。

2.1 加载地图

这里直接调用了SuperMap iServer REST服务。调用第三方地图服务参考 http://www.cnblogs.com/luxiaoxun/p/3364107.html

2.2 放大、缩小地图

private void tsbZoomIn_Click(object sender, EventArgs e)
{
this.mapControl1.Zoom += ;
} private void tsbZoomOut_Click(object sender, EventArgs e)
{
this.mapControl1.Zoom -= ;
} //定义的地图控件缩放变化后对应的事件 private void mapControl1_OnMapZoomChanged()
{
double zoom = this.mapControl1.Zoom - Convert.ToDouble();
this.mapControl2.Zoom = zoom; //设置地图缩放大小 }

2.3 鹰眼

需要2个地图控件,同时缩放、移动,实现联动的效果。

gMapControl1 事件设置代码:

private bool Mapleft1 = false;
private void gMapControl1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
Mapleft1 = true;
}
} private void gMapControl1_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
Mapleft1 = false;
}
} private void gMapControl1_OnPositionChanged(PointLatLng point)
{
if (Mapleft1)
{
this.gMapControl2.Position = point; //设置小地图中心点
}
}
private void gMapControl1_OnMapZoomChanged()
{
double zoom = this.gMapControl1.Zoom - Convert.ToDouble();
this.gMapControl2.Zoom = zoom; //设置地图缩放大小
}

gMapControl2 事件设置代码:

private bool Mapleft2 = false;
private void gMapControl2_MouseMove(object sender, MouseEventArgs e)
{
lastPosition1 = this.gMapControl1.FromLocalToLatLng(e.X, e.Y);
} private void gMapControl2_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button==MouseButtons.Left)
{
Mapleft2 = false;
}
} private void gMapControl2_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
this.gMapControl1.Position = lastPosition1; //鼠标单击设置gMapControl1.中心点
Mapleft2 = true;
}
} private void gMapControl2_OnPositionChanged(PointLatLng point)
{
if (Mapleft2)
{
this.gMapControl1.Position = point; //设置gMapControl1中心点
}
}

2.4 添加点线面

以添加点对象为例:

IAction _editAddAlarmAction = null;

        //添加告警源
private void tsbAddAlarm_Click_1(object sender, EventArgs e)
{
if (_editAddAlarmAction == null)
{
_editAddAlarmAction = new AddAlarmAction();
}
this.mapControl1.CurrentAction = _editAddAlarmAction;
ToolCheckChanged((sender as ToolStripItem).Name);
}
  public class AddAlarmAction : Action
{ List<Feature> targetFeatures = null; private GMapControl _gMapControl = null;
private GMapOverlay markerOverlay = new GMapOverlay("addalarm");
private bool _start = false;
List<PointLatLng> _points = null;
List<Point2D> _point2Ds = new List<Point2D>();
private string _mapUrl = string.Empty;
private string _mapName = string.Empty;
Map _map = null; public override void OnLoad(GMapControl gMapControl)
{
_gMapControl = gMapControl;
_gMapControl.Overlays.Add(markerOverlay);
_points = new List<PointLatLng>();
_start = false;
this._mapUrl = ((SuperMapProvider)gMapControl.MapProvider).ServiceUrl;
this._mapName = ((SuperMapProvider)gMapControl.MapProvider).MapName;
this._map = new Map(this._mapUrl);
}
public override void OnMapMouseDown(object sender, MouseEventArgs e)
{
PointLatLng currentPoint = this._gMapControl.FromLocalToLatLng(e.X, e.Y);
double mercatorX, mercatorY;
Helper.LonLat2Mercator(currentPoint.Lng, currentPoint.Lat, out mercatorX, out mercatorY);
Point2D point2D = new Point2D(mercatorX, mercatorY); _point2Ds.Add(point2D);
_points.Add(currentPoint);
if (_start)
{
GMapMarker marker = new GMapMarkerImage(currentPoint, new Bitmap("C:\\Users\\yaohui\\Desktop\\iClient-for-DotNet-master\\iClient-for-DotNet-master\\Demo\\demo.winform\\Resources\\sign-warning-icon.png"));
markerOverlay.Markers.Add(marker);
marker.ToolTipText = "告警源编号:";
marker.ToolTip.Fill = Brushes.Blue;
marker.ToolTip.Foreground = Brushes.White;
marker.ToolTip.Stroke = Pens.Black;
marker.ToolTip.TextPadding = new Size(, );
marker.ToolTipMode = MarkerTooltipMode.OnMouseOver;
}
_start = true; }
public override void OnMapMouseDoubleClick(object sender, MouseEventArgs e)
{
if (!_start) return;
PointLatLng currentPoint = this._gMapControl.FromLocalToLatLng(e.X, e.Y);
double mercatorX, mercatorY;
Helper.LonLat2Mercator(currentPoint.Lng, currentPoint.Lat, out mercatorX, out mercatorY);
Point2D point2D = new Point2D(mercatorX, mercatorY);
markerOverlay.Markers.Clear();
_points.Clear();
_point2Ds.Clear();
_start = false;
} }

2.5 自定义marker

通过继承gmap的marker类,进行扩展:(这里添加了符号高亮的画笔)

using GMap.NET.WindowsForms;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms; namespace gmap.demo.winform
{
class GMapMarkerImage : GMapMarker
{
private Image image;
public Image Image
{
get
{
return image;
}
set
{
image = value;
if (image != null)
{
this.Size = new Size(image.Width, image.Height);
}
}
} public bool IsHighlight = true;
public Pen HighlightPen
{
set;
get;
} public Pen FlashPen
{
set;
get;
}
public Pen OutPen
{
get;
set;
}
private Timer flashTimer = new Timer(); private int radius;
private int flashRadius; public GMapMarkerImage(GMap.NET.PointLatLng p, Image image)
: base(p)
{
Size = new System.Drawing.Size(image.Width, image.Height);
Offset = new System.Drawing.Point(-Size.Width / , -Size.Height / );
Image = image;
HighlightPen = new System.Drawing.Pen(Brushes.Red, );
radius = Size.Width >= Size.Height ? Size.Width : Size.Height;
flashTimer.Interval = ;
flashTimer.Tick += new EventHandler(flashTimer_Tick);
} void flashTimer_Tick(object sender, EventArgs e)
{
if (FlashPen == null)
{
FlashPen = new Pen(Brushes.Red, );
flashRadius = radius;
}
else
{
flashRadius += radius / ;
if (flashRadius >= * radius)
{
flashRadius = radius;
FlashPen.Color = Color.FromArgb(, Color.Red);
}
else
{
Random rand = new Random();
int alpha = rand.Next();
FlashPen.Color = Color.FromArgb(alpha, Color.Red);
}
}
}
//this.Overlay.Control.Refresh();
//this.mapControl1.Refresh(); public void StartFlash()
{
flashTimer.Start();
}
public void StopFlash()
{
flashTimer.Stop();
if (FlashPen != null)
{
FlashPen.Dispose();
FlashPen = null;
}
}
//this.mapControl1.Refresh(); public override void OnRender(Graphics g)
{
if (image == null)
return; Rectangle rect = new Rectangle(LocalPosition.X, LocalPosition.Y, Size.Width, Size.Height);
g.DrawImage(image, rect); if (IsMouseOver && IsHighlight)
{
g.DrawRectangle(HighlightPen, rect);
} if (FlashPen != null)
{
g.DrawEllipse(FlashPen,
new Rectangle(LocalPosition.X - flashRadius / + Size.Width / , LocalPosition.Y - flashRadius / + Size.Height / , flashRadius, flashRadius));
}
}
//public override void Dispose()
//{
// if (HighlightPen != null)
// {
// HighlightPen.Dispose();
// HighlightPen = null;
// }
// if (FlashPen != null)
// {
// FlashPen.Dispose();
// FlashPen = null;
// }
//}
}
}

2.6 截图

//地图保存为图片
private void toolStripButton6_Click(object sender, EventArgs e)
{
try
{
using (SaveFileDialog dialog = new SaveFileDialog())
{
dialog.Filter = "PNG (*.png)|*.png";
dialog.FileName = "GMap.NET image";
Image image = this.mapControl1.ToImage();
if (image != null)
{
using (image)
{
if (dialog.ShowDialog() == DialogResult.OK)
{
string fileName = dialog.FileName;
if (!fileName.EndsWith(".png", StringComparison.OrdinalIgnoreCase))
{
fileName += ".png";
}
image.Save(fileName);
MessageBox.Show("图片已保存: " + dialog.FileName, "GMap.NET", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
}
}
}
}
}
catch (Exception exception)
{
MessageBox.Show("图片保存失败: " + exception.Message, "GMap.NET", MessageBoxButtons.OK, MessageBoxIcon.Hand);
}
}

2.7 保存缓存

        //保存缓存
private void toolStripButton7_Click(object sender, EventArgs e)
{
if (this.mapControl1.ShowExportDialog() == true)
{
//this.gMapControl1.ShowTileGridLines = true;//显示瓦片,也就是显示方格
this.mapControl1.ReloadMap();
}
}

3、iclient for .net 模拟B/S实现报警闪烁demo展示

4、参考

http://www.cnblogs.com/luxiaoxun/p/3494756.html

http://blog.csdn.net/sunsun1203/article/details/53816464

http://www.cnblogs.com/luxiaoxun/p/3475355.html

http://blog.sina.com.cn/s/blog_819100560101dgng.html

GMap学习笔记的更多相关文章

  1. js学习笔记:webpack基础入门(一)

    之前听说过webpack,今天想正式的接触一下,先跟着webpack的官方用户指南走: 在这里有: 如何安装webpack 如何使用webpack 如何使用loader 如何使用webpack的开发者 ...

  2. PHP-自定义模板-学习笔记

    1.  开始 这几天,看了李炎恢老师的<PHP第二季度视频>中的“章节7:创建TPL自定义模板”,做一个学习笔记,通过绘制架构图.UML类图和思维导图,来对加深理解. 2.  整体架构图 ...

  3. PHP-会员登录与注册例子解析-学习笔记

    1.开始 最近开始学习李炎恢老师的<PHP第二季度视频>中的“章节5:使用OOP注册会员”,做一个学习笔记,通过绘制基本页面流程和UML类图,来对加深理解. 2.基本页面流程 3.通过UM ...

  4. 2014年暑假c#学习笔记目录

    2014年暑假c#学习笔记 一.C#编程基础 1. c#编程基础之枚举 2. c#编程基础之函数可变参数 3. c#编程基础之字符串基础 4. c#编程基础之字符串函数 5.c#编程基础之ref.ou ...

  5. JAVA GUI编程学习笔记目录

    2014年暑假JAVA GUI编程学习笔记目录 1.JAVA之GUI编程概述 2.JAVA之GUI编程布局 3.JAVA之GUI编程Frame窗口 4.JAVA之GUI编程事件监听机制 5.JAVA之 ...

  6. seaJs学习笔记2 – seaJs组建库的使用

    原文地址:seaJs学习笔记2 – seaJs组建库的使用 我觉得学习新东西并不是会使用它就够了的,会使用仅仅代表你看懂了,理解了,二不代表你深入了,彻悟了它的精髓. 所以不断的学习将是源源不断. 最 ...

  7. CSS学习笔记

    CSS学习笔记 2016年12月15日整理 CSS基础 Chapter1 在console输入escape("宋体") ENTER 就会出现unicode编码 显示"%u ...

  8. HTML学习笔记

    HTML学习笔记 2016年12月15日整理 Chapter1 URL(scheme://host.domain:port/path/filename) scheme: 定义因特网服务的类型,常见的为 ...

  9. DirectX Graphics Infrastructure(DXGI):最佳范例 学习笔记

    今天要学习的这篇文章写的算是比较早的了,大概在DX11时代就写好了,当时龙书11版看得很潦草,并没有注意这篇文章,现在看12,觉得是跳不过去的一篇文章,地址如下: https://msdn.micro ...

随机推荐

  1. Js apply方法详解

    我在一开始看到javascript的函数apply和call时,非常的模糊,看也看不懂,最近在网上看到一些文章对apply方法和call的一些示例,总算是看的有点眉目了,在这里我做如下笔记,希望和大家 ...

  2. [转]JDBC中日期时间的处理技巧

    Java中用类java.util.Date对日期/时间做了封装,此类提供了对年.月.日.时.分.秒.毫秒以及时区的控制方法,同时也提供一些工具方法,比如日期/时间的比较,前后判断等. java.uti ...

  3. PHP访问数据库

    1.原生mysql方式 <?php //最原生态的访问方式,不过SQL注入是个麻烦事 $con = mysql_connect("localhost","root& ...

  4. 更换用installshield打包生成exe文件的图标【转】

    最近在研究用installshield2010为自己做的产品打包,自己在网上找写资料,胡乱折腾,最后弄成了一个exe安装包,想要修改exe文件的图标,发现Basic MSI project 无法用in ...

  5. c 按输入的字母来输出对应效果

    输入一个大写字母,如 F,输出 比如: 输入:F 输出: F EFE DEFED CDEFEDC BCDEFEDCB ABCDEFEDCBA #include<stdio.h> int m ...

  6. rocketmq源码打包步骤

    1,从git上面克隆好源码之后,进入rocketmq目录,执行: mvn -Prelease-all -DskipTests clean install 2,打包完成之后,进入distribution ...

  7. ltp-ddt inverted_return小trick

    ./runtest/ddt/i2c_readwrite # @name I2C write read test on slave device# @desc I2C write read test o ...

  8. Requests快速上手

    发送请求 使用 Requests 发送网络请求非常简单. 一开始要导入 Requests 模块: >>> import requests 然后,尝试获取某个网页.本例子中,我们来获取 ...

  9. unity WegGL 调用js

    test.jslib文件,必须放到Assets/Plugins下,这里是:Assets/Plugins/WebGL mergeInto(LibraryManager.library, { Hello: ...

  10. loadrunner脚本001

    Action() { ; lr_start_transaction("login"); web_add_cookie("JSESSIONID=061460B7DFF2F7 ...