GMap.Net中Marker的使用方法-創(chuàng)新互聯(lián)

本篇內(nèi)容主要講解“GMap.Net中Marker的使用方法”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學(xué)習(xí)“GMap.Net中Marker的使用方法”吧!

十載的龍江網(wǎng)站建設(shè)經(jīng)驗,針對設(shè)計、前端、開發(fā)、售后、文案、推廣等六對一服務(wù),響應(yīng)快,48小時及時工作處理。網(wǎng)絡(luò)營銷推廣的優(yōu)勢是能夠根據(jù)用戶設(shè)備顯示端的尺寸不同,自動調(diào)整龍江建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計,從而大程度地提升瀏覽體驗。成都創(chuàng)新互聯(lián)公司從事“龍江網(wǎng)站設(shè)計”,“龍江網(wǎng)站推廣”以來,每個客戶項目都認(rèn)真落實執(zhí)行。

自定義Marker,可以理解為在地圖上自定義圖標(biāo)(Custom Marker),先看看GMap的地圖和圖標(biāo)的顯示方式:

GMap.Net中Marker的使用方法

Map控件上可以添加Overlay(圖層),可以添加多個圖層,先添加的圖層在下面顯示。

圖層上可以添加GMapMarker,當(dāng)然也可以添加GMapPolygon和GMapRoute,后續(xù)介紹。

在地圖的使用中常要求的功能就是添加自定義圖標(biāo),可以點擊圖標(biāo)、刪除圖標(biāo)、拖動圖標(biāo)、高亮圖標(biāo)等。

下面介紹這些功能的實現(xiàn)(主要是基于WinForm的,WPF的可以參考官方Demo實現(xiàn)):

1、自定義圖標(biāo),使用官方的Marker:

復(fù)制代碼 代碼如下:


Bitmap bitmap = Bitmap.FromFile("F:\\Projects\\GMapDemo\\GMapDemo\\Image\\A.png") as Bitmap;
GMapMarker marker = new GMarkerGoogle(point, bitmap);


直接使用GMap.NET.WindowsForms.Markers中的GMarkerGoogle,傳入一個Bitmap,就可以使用自定義的圖片來做圖標(biāo)。

2、繼承GMapMarker,自定義Marker:

復(fù)制代碼 代碼如下:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GMap.NET;
using GMap.NET.WindowsForms;
using System.Drawing;

namespace GMapWinFormDemo
{
    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 Pen Pen
        {
            get;
            set;
        }

        public Pen OutPen
        {
            get;
            set;
        }

        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);
            this.image = image;
            Pen = null;
            OutPen = null;
        }

        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 (Pen != null)
            {
                g.DrawRectangle(Pen, rect);
            }

            if (OutPen != null)
            {
                g.DrawEllipse(OutPen, rect);
            }
        }

        public override void Dispose()
        {
            if (Pen != null)
            {
                Pen.Dispose();
                Pen = null;
            }

            if (OutPen != null)
            {
                OutPen.Dispose();
                OutPen = null;
            }

            base.Dispose();
        }
    }
}


介紹下GMapMarkerImage三個屬性的作用:

Image:保存圖標(biāo)的圖片。

Pen:在圖片外圍畫DrawRectangle的Pen,當(dāng)其不為null的時候,會在圖片的外圍畫一個矩形,實現(xiàn)高亮(highlight)的效果。

OutPen:在圖片外圍畫DrawEllipse的Pen,當(dāng)其不為null的時候,會在圖片外圍畫一個一個橢圓,設(shè)置這個值可以實現(xiàn)閃動。

3、移動圖標(biāo)(Move Marker)的實現(xiàn):

在MapControl中添加如下事件的響應(yīng):

復(fù)制代碼 代碼如下:


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);


MouseDown和MouseUp中判斷左鍵是否按下(用左鍵來移動圖標(biāo))。

OnMarkerEnter中設(shè)置選中的Marker,同時設(shè)置Pen的值,實現(xiàn)高亮。

OnMarkerLeave中取消選中的Marker,取消Pen的值,取消高亮。

MouseMove中更新選中選中Marker的Position就可以了。

4、圖標(biāo)閃動的實現(xiàn):

需要一個定時器:使用的是Form下的Timer,定時器響應(yīng)的事件:

復(fù)制代碼 代碼如下:


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();
        }


更新所有Marker的OutPen的值(當(dāng)然你也可以只更新某個Marker),通過在圖標(biāo)上畫圈圈來實現(xiàn)閃動,當(dāng)然你也可以通過設(shè)置Marker的IsVisible屬性來實現(xiàn)自己想要的效果。。。

效果圖如下:

GMap.Net中Marker的使用方法

全部代碼如下:

復(fù)制代碼 代碼如下:


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("www.google.com.hk");
            }
            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;     //當(dāng)前比例
            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();
        }
    }
}


到此,相信大家對“GMap.Net中Marker的使用方法”有了更深的了解,不妨來實際操作一番吧!這里是創(chuàng)新互聯(lián)建站,更多相關(guān)內(nèi)容可以進入相關(guān)頻道進行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!

分享題目:GMap.Net中Marker的使用方法-創(chuàng)新互聯(lián)
標(biāo)題來源:http://muchs.cn/article20/dsjgjo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供軟件開發(fā)、企業(yè)建站、品牌網(wǎng)站設(shè)計、電子商務(wù)靜態(tài)網(wǎng)站、做網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都做網(wǎng)站