• GMap使用2:Overlay


    例1:当有鼠标点击动作时,地图上实时出现一个绿色小图钉,每次点击都会更新位置。

    关键知识点:想象对图上方有一层透明的图层用以实现这种操作。

    需要using GMap.NET.WindowsForms;

    相关类GMapOverlay user_ops = new GMapOverlay("USER_OPS");

    控件初始化的时候,添加该OverLay并且设置相应函数。

    this.gMapControl1.Overlays.Add(user_ops);
    this.gMapControl1.MouseClick += gMapControl1_MouseClick;

    响应函数为

    void gMapControl1_MouseClick(object sender, MouseEventArgs e)
    {
    user_ops.Markers.Clear();
    PointLatLng p = this.gMapControl1.FromLocalToLatLng(e.X, e.Y);
    GMapMarker marker = new GMarkerGoogle(p, GMarkerGoogleType.green_pushpin);
    marker.ToolTipText = "smarttrashbin status";
    this.user_ops.Markers.Add(marker);
    }

    全部代码

    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 testGMap
    {
        public partial class mainForm : Form
        {
            GMapOverlay user_ops = new GMapOverlay("USER_OPS");
            List<PointLatLng> list = new List<PointLatLng>();
    
            public mainForm()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                this.gMapControl1.CacheLocation = System.Windows.Forms.Application.StartupPath;
                this.gMapControl1.MapProvider = GMapProviders.GoogleChinaMap;
                this.gMapControl1.Manager.Mode = AccessMode.ServerAndCache;
                this.gMapControl1.MinZoom = 1;
                this.gMapControl1.MaxZoom = 18;
                this.gMapControl1.Zoom = 5;
                this.gMapControl1.ShowCenter = false;
                this.gMapControl1.DragButton = System.Windows.Forms.MouseButtons.Right;
                this.gMapControl1.Position = new PointLatLng(30.68, 120.34);
                this.gMapControl1.Overlays.Add(user_ops);
                this.gMapControl1.MouseClick += gMapControl1_MouseClick;
    
            }
    
            void gMapControl1_MouseClick(object sender, MouseEventArgs e)
            {
                user_ops.Markers.Clear();
                PointLatLng p = this.gMapControl1.FromLocalToLatLng(e.X, e.Y);
                GMapMarker marker = new GMarkerGoogle(p, GMarkerGoogleType.green_pushpin);
                marker.ToolTipText = "smarttrashbin status";
                this.user_ops.Markers.Add(marker);
            }
    
            private void Form1_FormClosing(object sender, FormClosingEventArgs e)
            {
                this.gMapControl1.Dispose();
            }
    
           
        }
    }

     例2:右键拖动鼠标,左键点击显示一条路径曲线

    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 testGMap
    {
        public partial class mainForm : Form
        {
            GMapOverlay user_ops = new GMapOverlay("USER_OPS");
            List<PointLatLng> list = new List<PointLatLng>();
    
            public mainForm()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                this.gMapControl1.CacheLocation = System.Windows.Forms.Application.StartupPath;
                this.gMapControl1.MapProvider = GMapProviders.GoogleChinaMap;
                this.gMapControl1.Manager.Mode = AccessMode.ServerAndCache;
                this.gMapControl1.MinZoom = 1;
                this.gMapControl1.MaxZoom = 18;
                this.gMapControl1.Zoom = 5;
                this.gMapControl1.ShowCenter = false;
                this.gMapControl1.DragButton = System.Windows.Forms.MouseButtons.Right;
                this.gMapControl1.Position = new PointLatLng(30.68, 120.34);
                this.gMapControl1.Overlays.Add(user_ops);
                this.gMapControl1.MouseClick += gMapControl1_MouseClick;
    
            }
    
            void gMapControl1_MouseClick(object sender, MouseEventArgs e)
            {
                if (e.Button != MouseButtons.Left)
                    return;
                PointLatLng p = this.gMapControl1.FromLocalToLatLng(e.X, e.Y);//将鼠标点击点坐标转换为经纬度坐标
                list.Add(p);
                user_ops.Routes.Clear();
                GMapRoute route = new GMapRoute(list, "line");
                route.Stroke.Color = Color.Red;
                route.Stroke.Width = 2;  //设置画
                user_ops.Routes.Add(route);
            }
    
            private void Form1_FormClosing(object sender, FormClosingEventArgs e)
            {
                this.gMapControl1.Dispose();
            }
    
           
        }
    }

     例3:左键点击的点组成一个多边形。

    类似例2,响应函数如下

    void gMapControl1_MouseClick(object sender, MouseEventArgs e)
            {
                if (e.Button != MouseButtons.Left)
                    return;
                PointLatLng p = this.gMapControl1.FromLocalToLatLng(e.X, e.Y);//将鼠标点击点坐标转换为经纬度坐标
                list.Add(p);
                user_ops.Polygons.Clear();
    
                GMapPolygon polygon = new GMapPolygon(list, "多边形");
                polygon.Fill = new SolidBrush(Color.FromArgb(50, Color.Red));
                polygon.Stroke = new Pen(Color.Blue, 2);
                polygon.IsHitTestVisible = true;
                user_ops.Polygons.Add(polygon);
            }
  • 相关阅读:
    U盘复制文件到最后5秒会卡住怎么办解决
    解决PuTTY中文乱码
    NSA Fuzzbunch中EternalRomance工具复现过程
    VirtualBox修改现有VDI虚拟磁盘大小
    高危Windows系统 SMB/RDP远程命令执行漏洞 手工修复办法
    数据库页已标记为 RestorePending,可能表明磁盘已损坏。要从此状态恢复,请执行还原操作。
    用Partimage创建或恢复分区备份
    IIS7保存配置文件及导入、导出、备份、还原
    WINDOWS SERVER 2008远程桌面端口修改方法
    谷歌启用抓取JavaScript,应对方案!
  • 原文地址:https://www.cnblogs.com/legion/p/8416459.html
Copyright © 2020-2023  润新知