博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
GMap.Net开发之自定义Marker
阅读量:6162 次
发布时间:2019-06-21

本文共 6123 字,大约阅读时间需要 20 分钟。

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.WindowsForms;using GMap.NET.MapProviders;using GMap.NET.WindowsForms.Markers;namespace GMapWinFormDemo{    public partial class MainForm : Form    {        private GMapOverlay objects = new GMapOverlay("objects"); //放置marker的图层        private GMapMarkerImage currentMarker;        private bool isLeftButtonDown = false;        private Timer blinkTimer = new Timer();        public MainForm()        {            InitializeComponent();            try            {                System.Net.IPHostEntry e = System.Net.Dns.GetHostEntry("ditu.google.cn");            }            catch            {                mapControl.Manager.Mode = AccessMode.CacheOnly;                MessageBox.Show("No internet connection avaible, going to CacheOnly mode.", "GMap.NET Demo", MessageBoxButtons.OK, MessageBoxIcon.Warning);            }            mapControl.CacheLocation = Environment.CurrentDirectory + "\\GMapCache\\"; //缓存位置            mapControl.MapProvider = GMapProviders.GoogleChinaMap; //google china 地图            mapControl.MinZoom = 2;  //最小比例            mapControl.MaxZoom = 17; //最大比例            mapControl.Zoom = 5;     //当前比例            mapControl.ShowCenter = false; //不显示中心十字点            mapControl.DragButton = System.Windows.Forms.MouseButtons.Left; //左键拖拽地图            mapControl.Position = new PointLatLng(32.064,118.704); //地图中心位置:南京            mapControl.OnMapZoomChanged += new MapZoomChanged(mapControl_OnMapZoomChanged);            mapControl.MouseClick += new MouseEventHandler(mapControl_MouseClick);            mapControl.MouseDown += new MouseEventHandler(mapControl_MouseDown);            mapControl.MouseUp += new MouseEventHandler(mapControl_MouseUp);            mapControl.MouseMove += new MouseEventHandler(mapControl_MouseMove);            mapControl.OnMarkerClick += new MarkerClick(mapControl_OnMarkerClick);            mapControl.OnMarkerEnter += new MarkerEnter(mapControl_OnMarkerEnter);            mapControl.OnMarkerLeave += new MarkerLeave(mapControl_OnMarkerLeave);            mapControl.Overlays.Add(objects);        }        void mapControl_MouseMove(object sender, MouseEventArgs e)        {            if (e.Button == System.Windows.Forms.MouseButtons.Left && isLeftButtonDown)            {                if (currentMarker != null)                {                    PointLatLng point = mapControl.FromLocalToLatLng(e.X, e.Y);                    currentMarker.Position = point;                    currentMarker.ToolTipText = string.Format("{0},{1}", point.Lat, point.Lng);                }            }        }        void mapControl_MouseUp(object sender, MouseEventArgs e)        {            if (e.Button == System.Windows.Forms.MouseButtons.Left)            {                isLeftButtonDown = false;            }        }        void mapControl_MouseDown(object sender, MouseEventArgs e)        {            if (e.Button == System.Windows.Forms.MouseButtons.Left)            {                isLeftButtonDown = true;            }        }        void mapControl_OnMarkerLeave(GMapMarker item)        {            if (item is GMapMarkerImage)            {                currentMarker = null;                GMapMarkerImage m = item as GMapMarkerImage;                m.Pen.Dispose();                m.Pen = null;            }        }        void mapControl_OnMarkerEnter(GMapMarker item)        {            if (item is GMapMarkerImage)            {                currentMarker = item as GMapMarkerImage;                currentMarker.Pen = new Pen(Brushes.Red, 2);            }        }        void mapControl_OnMarkerClick(GMapMarker item, MouseEventArgs e)        {        }        void mapControl_MouseClick(object sender, MouseEventArgs e)        {            if(e.Button == System.Windows.Forms.MouseButtons.Right)            {                //objects.Markers.Clear();                PointLatLng point = mapControl.FromLocalToLatLng(e.X,e.Y);                //GMapMarker marker = new GMarkerGoogle(point, GMarkerGoogleType.green);                Bitmap bitmap = Bitmap.FromFile("F:\\Projects\\GMapDemo\\GMapDemo\\Image\\A.png") as Bitmap;                //GMapMarker marker = new GMarkerGoogle(point, bitmap);                GMapMarker marker = new GMapMarkerImage(point, bitmap);                marker.ToolTipMode = MarkerTooltipMode.OnMouseOver;                marker.ToolTipText = string.Format("{0},{1}", point.Lat, point.Lng);                objects.Markers.Add(marker);            }        }        void mapControl_OnMapZoomChanged()        {        }        private void buttonBeginBlink_Click(object sender, EventArgs e)        {            blinkTimer.Interval = 1000;            blinkTimer.Tick += new EventHandler(blinkTimer_Tick);            blinkTimer.Start();        }        void blinkTimer_Tick(object sender, EventArgs e)        {            foreach (GMapMarker m in objects.Markers)            {                if (m is GMapMarkerImage)                {                    GMapMarkerImage marker = m as GMapMarkerImage;                    if (marker.OutPen == null)                        marker.OutPen = new Pen(Brushes.Red, 2);                    else                    {                        marker.OutPen.Dispose();                        marker.OutPen = null;                    }                }            }            mapControl.Refresh();        }        private void buttonStopBlink_Click(object sender, EventArgs e)        {            blinkTimer.Stop();            foreach (GMapMarker m in objects.Markers)            {                if (m is GMapMarkerImage)                {                    GMapMarkerImage marker = m as GMapMarkerImage;                    marker.OutPen.Dispose();                    marker.OutPen = null;                }            }            mapControl.Refresh();        }    }}

 

 

 

作者:
出处:
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。http://www.cnblogs.com/luxiaoxun/p/3475355.html
你可能感兴趣的文章
Apache通过mod_php5支持PHP
查看>>
java学习:jdbc连接示例
查看>>
Silverlight 如何手动打包xap
查看>>
禁用ViewState
查看>>
HTTP缓存应用
查看>>
KubeEdge向左,K3S向右
查看>>
DTCC2013:基于网络监听数据库安全审计
查看>>
CCNA考试要点大搜集(二)
查看>>
ajax查询数据库时数据无法更新的问题
查看>>
Kickstart 无人职守安装,终于搞定了。
查看>>
linux开源万岁
查看>>
linux/CentOS6忘记root密码解决办法
查看>>
25个常用的Linux iptables规则
查看>>
集中管理系统--puppet
查看>>
Exchange 2013 PowerShell配置文件
查看>>
JavaAPI详解系列(1):String类(1)
查看>>
HTML条件注释判断IE<!--[if IE]><!--[if lt IE 9]>
查看>>
发布和逸出-构造过程中使this引用逸出
查看>>
使用SanLock建立简单的HA服务
查看>>
Subversion使用Redmine帐户验证简单应用、高级应用以及优化
查看>>