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);
        }
原文地址:https://www.cnblogs.com/legion/p/8416459.html