GMap學習筆記

来源:http://www.cnblogs.com/yaohuimo/archive/2017/01/09/6255396.html
-Advertisement-
Play Games

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 laye ...


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 += 1;
}

private void tsbZoomOut_Click(object sender, EventArgs e)
{
this.mapControl1.Zoom -= 1;
}

//定義的地圖控制項縮放變化後對應的事件

private void mapControl1_OnMapZoomChanged()
{
double zoom = this.mapControl1.Zoom - Convert.ToDouble(5);
this.mapControl2.Zoom = zoom; //設置地圖縮放大小

}
View Code

 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(5);
    this.gMapControl2.Zoom = zoom; //設置地圖縮放大小
}
View Code

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中心點
    }
}
View Code

 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);
        }
View Code
  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(20, 20);
                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;
        }

    }
View Code

 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 / 2, -Size.Height / 2);
            Image = image;
            HighlightPen = new System.Drawing.Pen(Brushes.Red, 2);
            radius = Size.Width >= Size.Height ? Size.Width : Size.Height;
            flashTimer.Interval = 10;
            flashTimer.Tick += new EventHandler(flashTimer_Tick);
        }

        void flashTimer_Tick(object sender, EventArgs e)
        {
            if (FlashPen == null)
            {
                FlashPen = new Pen(Brushes.Red, 3);
                flashRadius = radius;
            }
            else
            {
                flashRadius += radius / 4;
                if (flashRadius >= 2 * radius)
                {
                    flashRadius = radius;
                    FlashPen.Color = Color.FromArgb(255, Color.Red);
                }
                else
                {
                    Random rand = new Random();
                    int alpha = rand.Next(255);
                    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 / 2 + Size.Width / 2, LocalPosition.Y - flashRadius / 2 + Size.Height / 2, flashRadius, flashRadius));
            }
        }
        //public override void Dispose()
        //{
        //    if (HighlightPen != null)
        //    {
        //        HighlightPen.Dispose();
        //        HighlightPen = null;
        //    }
        //    if (FlashPen != null)
        //    {
        //        FlashPen.Dispose();
        //        FlashPen = null;
        //    }
        //}
    }
}
View Code

 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);
            }
        }
View Code

 2.7 保存緩存

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

 

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

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • Linux,1991年,系統安全,良好的可移植性,多用戶,多任務,良好的相容性,良好的用戶界面, 主流的是RedHat或者CentOS, CentOS 設置的網關 192.168.2.2 Windows 設置的網關 192.168.2.1 取消命令行一直運行:Ctrl+c 在命令行中切換root用戶 ...
  • 本節內容 1.github介紹 很多人都知道,Linus在1991年創建了開源的Linux,從此,Linux系統不斷發展,已經成為最大的伺服器系統軟體了。 Linus雖然創建了Linux,但Linux的壯大是靠全世界熱心的志願者參與的,這麼多人在世界各地為Linux編寫代碼,那Linux的代碼是如何 ...
  • 觀察系統當前進程的運行情況的命令是( ):A、freeB、dmesgC、topD、last 答案:http://hovertree.com/tiku/bjag/foxg5n0q.htm Linux系統通過( )命令給其他用戶發消息?A.lessB.mesg yC.writeD.echo to 答案: ...
  • nginx工作模式-->1個master+n個worker進程 安裝nginx的所需pcre庫【用於支持rewrite模塊】 下載軟體方法: 搜索 pcre download 網址:http://pcre.org 下載pcre包 wget ftp://ftp.csx.cam.ac.uk/pub/so ...
  • 1、在/etc/init.d/目錄下創建 nginx 文件,內容如下: 2.設置/etc/init.d/nginx 執行許可權 3.設置開機預設啟動 5.nginx控制命令 ...
  • 曾經六六我也是一個初級開發的實習生,當年的我初入農田,一心要做一個高產量高品質的碼農,做碼農界的袁隆平!然而,現實總是無比的殘酷,我一個剛入門的碼農,剛進農田就跌了許多跟頭。BUG天天困擾著我,不過這也是一種磨練。不然我六六也難以成為一個合格的碼農! 不過現在時代不同了,軟體公司越來越多,軟體開發行 ...
  • 委托這個東西不是很好理解,可是工作中又經常用到,你隨處可以看到它的身影,真讓人有一種又愛又恨的感覺,我相信許多人被它所困擾過。 一提到委托,如果你學過C語言,你一定會馬上聯想到函數指針。 什麼是委托?委托是C#中類型安全的,可以訂閱一個或多個具有相同簽名方法的函數指針。委托可以把函數做為參數傳遞,其 ...
  • 使用NHibernate實現一對多,多對一的關聯很是簡單,可如果要用複合主鍵實現確實讓人有些淡淡的疼。雖然很淡疼但還是要去抹平這個坑,在下不才,願意嘗試。 以示例進入正文,源碼下載地址: 一、數據表關係圖 很明顯,他是一個自引用數表,實現無限級樹結構的存儲。 二、關鍵步驟 註解如何實現複合主鍵 根據 ...
一周排行
    -Advertisement-
    Play Games
  • 前言 本文介紹一款使用 C# 與 WPF 開發的音頻播放器,其界面簡潔大方,操作體驗流暢。該播放器支持多種音頻格式(如 MP4、WMA、OGG、FLAC 等),並具備標記、實時歌詞顯示等功能。 另外,還支持換膚及多語言(中英文)切換。核心音頻處理採用 FFmpeg 組件,獲得了廣泛認可,目前 Git ...
  • OAuth2.0授權驗證-gitee授權碼模式 本文主要介紹如何筆者自己是如何使用gitee提供的OAuth2.0協議完成授權驗證並登錄到自己的系統,完整模式如圖 1、創建應用 打開gitee個人中心->第三方應用->創建應用 創建應用後在我的應用界面,查看已創建應用的Client ID和Clien ...
  • 解決了這個問題:《winForm下,fastReport.net 從.net framework 升級到.net5遇到的錯誤“Operation is not supported on this platform.”》 本文內容轉載自:https://www.fcnsoft.com/Home/Sho ...
  • 國內文章 WPF 從裸 Win 32 的 WM_Pointer 消息獲取觸摸點繪製筆跡 https://www.cnblogs.com/lindexi/p/18390983 本文將告訴大家如何在 WPF 裡面,接收裸 Win 32 的 WM_Pointer 消息,從消息裡面獲取觸摸點信息,使用觸摸點 ...
  • 前言 給大家推薦一個專為新零售快消行業打造了一套高效的進銷存管理系統。 系統不僅具備強大的庫存管理功能,還集成了高性能的輕量級 POS 解決方案,確保頁面載入速度極快,提供良好的用戶體驗。 項目介紹 Dorisoy.POS 是一款基於 .NET 7 和 Angular 4 開發的新零售快消進銷存管理 ...
  • ABP CLI常用的代碼分享 一、確保環境配置正確 安裝.NET CLI: ABP CLI是基於.NET Core或.NET 5/6/7等更高版本構建的,因此首先需要在你的開發環境中安裝.NET CLI。這可以通過訪問Microsoft官網下載並安裝相應版本的.NET SDK來實現。 安裝ABP ...
  • 問題 問題是這樣的:第三方的webapi,需要先調用登陸介面獲取Cookie,訪問其它介面時攜帶Cookie信息。 但使用HttpClient類調用登陸介面,返回的Headers中沒有找到Cookie信息。 分析 首先,使用Postman測試該登陸介面,正常返回Cookie信息,說明是HttpCli ...
  • 國內文章 關於.NET在中國為什麼工資低的分析 https://www.cnblogs.com/thinkingmore/p/18406244 .NET在中國開發者的薪資偏低,主要因市場需求、技術棧選擇和企業文化等因素所致。歷史上,.NET曾因微軟的閉源策略發展受限,儘管後來推出了跨平臺的.NET ...
  • 在WPF開發應用中,動畫不僅可以引起用戶的註意與興趣,而且還使軟體更加便於使用。前面幾篇文章講解了畫筆(Brush),形狀(Shape),幾何圖形(Geometry),變換(Transform)等相關內容,今天繼續講解動畫相關內容和知識點,僅供學習分享使用,如有不足之處,還請指正。 ...
  • 什麼是委托? 委托可以說是把一個方法代入另一個方法執行,相當於指向函數的指針;事件就相當於保存委托的數組; 1.實例化委托的方式: 方式1:通過new創建實例: public delegate void ShowDelegate(); 或者 public delegate string ShowDe ...