zoukankan      html  css  js  c++  java
  • 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);
            }
  • 相关阅读:
    .Net Discovery系列之深入理解平台机制与性“.NET技术”能影响(下) 狼人:
    MEF——.NE“.NET技术”T中值得体验的精妙设计 狼人:
    .NET中的异步编程 IO完“.NET技术”成端口以及FileStream.BeginRead 狼人:
    Entity Fr“.NET技术”amework 4.1 Code First 学习之路(二) 狼人:
    也玩MVC3.0 Razor自定义视图引擎“.NET技术”来修改默认的Views目录结构 狼人:
    引用类型赋值“.NET技术”为null与加速垃圾回收 狼人:
    在C#“.NET技术”中选择正确的集合进行编码 狼人:
    “.NET技术”Ajax和WEB服务数据格式:自定义返回格式 狼人:
    C#权限管理和设计浅“.NET技术”谈 狼人:
    带你走进缓“.NET技术”存世界 狼人:
  • 原文地址:https://www.cnblogs.com/legion/p/8416459.html
Copyright © 2011-2022 走看看