The custom marker use method of gmap.net development

Source: Internet
Author: User
Tags foreach

This article mainly introduces the use of marker in Gmap, a friend in need can refer to

custom Marker, which can be understood as customizing icons on a map (custom Marker), first look at the Gmap map and the way icons are displayed : The   map control can add overlay (layers), you can add multiple layers, and the layers you add are shown below.   Layer can add gmapmarker, of course, can also add Gmappolygon and Gmaproute, follow the introduction.   In the use of the map is often required function is to add a custom icon, you can click on the icon, delete icon, drag icon, highlighting icon.   below describes the implementation of these features (mainly based on WinForm, WPF can refer to the official demo implementation):   1, custom icons, using the official marker:   Code as follows: Bitmap Bitmap = Bitmap.fromfile ("F:projectsgmapdemogmapdemoimagea.png") as Bitmap; Gmapmarker marker = new Gmarkergoogle (point, Bitmap);     You can use a custom image to make an icon by using the Gmarkergoogle in GMap.NET.WindowsForms.Markers and passing in a bitmap.   2, Inherit Gmapmarker, custom marker:   Code as follows: 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     {      & nbsp private image Image;         Public image Image         {            get            {                return image;            }             set             {          &NB Sp     image = value;                 if (image!= null)             &NBSP ;   {                    this. size = new Size (image. Width, image. Height);                            }                   public Pen pen         {        &NBS P   get;             set;        }           public Pen outpen         {  &N Bsp         get;             set;        }           public gmapmarkerimage (GMap.NET.PointLatLng p, Image imag E            : Base (p)         {          &N Bsp 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)     & nbsp   {            if (image = null)               &NB Sp Return               Rectangle rect = new Rectangle (localposition.x, LocalpOsition. Y, Size.width, size.height);             g.drawimage (image, Rect);               if (Pen!= null)             {  &NBSP ;             G.drawrectangle (Pen, rect);                           if (outpen!= null)   &N Bsp         {                G.drawellipse (Outpen, rect);             {       }           public override void Dispose ()         {            if (Pen!= null)       & nbsp     {                Pen.dispose ()    ,             Pen = null;            } &NBSp             if (outpen!= null)             {    &N Bsp           outpen.dispose ();                 outpen = null;                           base. Dispose ();        }    }}       Introduction gmapmarkerimage three properties:   Image: Save picture of icon.   Pen: In the picture outside the DrawRectangle pen, when it is not NULL, will draw a rectangle on the periphery of the picture to achieve the effect of highlighting (highlight).   Outpen: DrawEllipse Pen is painted on the periphery of the picture, when it is not NULL, it will draw an ellipse on the periphery of the picture, setting this value can be implemented flashing.   3, moving icon (move Marker) Implementation:   Add the following event response in Mapcontrol:     Code as follows: 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 and MouseUp to determine whether the left button is pressed (left to move the icon).   Onmarkerenter to set the selected marker, and set the pen value to achieve highlighting.   Uncheck the marker in Onmarkerleave, cancel the pen value and remove the highlight.   MouseMove to select the position on the marker.   4, the implementation of flashing icon:   Need a timer: Using the form of the timer, timer response event:   Code as follows: void Blinktimer_tick (object sender, EventArgs e)         {            foreach (Gmapmarker m in objects.) Markers)             {                if (M is GMap Markerimage)                 {              &N Bsp     Gmapmarkerimage marker = m as gmapmarkerimage;                     if (marker. Outpen = null)                        marker. Outpen = new Pen (brushes.red, 2);                     else             &NBSP ;       {                        marker. Outpen.dispose ();                         marker. Outpen = null;                                   & nbsp {           }             Mapcontrol.refresh ();        }     Update all marker outpen (of course you can only update a marker), by drawing circles on the icon to achieve flashing, Of course, you can also set the marker IsVisible properties to achieve the desired effect ...   Effect chart is as follows:       All codes are as follows: The   code is as follows: 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"); Place marker layers         private gmapmarkerimage currentmarker;         private bool Isleftbuttondown = false;           private Timer Blinktimer = new timer ();           public mainform ()         {          &NBS P InitializeComponent ();               try             {      &NBS P         System.Net.IPHostEntry e = System.Net.Dns.GetHostEntry ("www.google.com.hk");       &NBsp    }             catch             {                MapControl.Manager.Mode = accessmode.cacheonly;                 MessageBox.Show ("No Internet connection avaible, going to cacheonl Y mode. "," Gmap.net Demo ", MessageBoxButtons.OK, messageboxicon.warning);                           mapcontrol.cachelocation = En Vironment. CurrentDirectory + "Gmapcache"; Cache location             Mapcontrol.mapprovider = Gmapproviders.googlechinamap; Google Maps             mapcontrol.minzoom = 2;  //minimum ratio             mapcontrol.maxzoom = 17; Max ratio             mapcontrol.zoom = 5;    //Current ratio             Mapcontrol.showcenter= false; Do not show center cross             Mapcontrol.dragbutton = System.Windows.Forms.MouseButtons.Left; Left-dragging map             mapcontrol.position = new POINTLATLNG (32.064,118.704); Map Center Location: Nanjing               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.MouseButton S.left && Isleftbuttondown)             {                if (currentmarker!= null)                 {      &N Bsp             POINTLATLNG point = MAPCONTROL.FROMLOCALTOLATLNG (e.x, e.y);                     currentmarker.position = point;                 &NBSp   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;   &NB Sp        }         {          void Mapcontrol_mousedown (o Bject sender, MouseEventArgs e)         {            if (E.button = = Sy Stem. Windows.Forms.MouseButtons.Left)             {            &NB Sp   Isleftbuttondown = true;           }         {          void Mapcontrol_onmarkerlea ve (Gmapmarker Item)         {            if (item is gmapmarkerimage) & nbsp           {                currentmarker = null;                 gmapmarkerimage m = Item as Gmapmarkerimage;                 m.pen.dispose ();                 m.pen = null;                     {          void mapcontr Ol_onmarkerenter (Gmapmarker Item)         {            if (item is GMAP Markerimage)             {                CURRENTMA Rker = Item as GmapmarkerImage;                 Currentmarker.pen = new Pen (brushes.red, 2);                     {          void mapcontr Ol_onmarkerclick (Gmapmarker item, MouseEventArgs e)         {       }   &NB Sp       void Mapcontrol_mouseclick (object sender, MouseEventArgs e)         {  &NBS P         if (E.button = System.Windows.Forms.MouseButtons.Right)           &NB Sp {               //objects. Markers.clear ();                 POINTLATLNG point = MAPCONTROL.FROMLOCALTOLATLNG (E.X,E.Y);                //gmapmarker marker = new Gmarkergoogle (Point, gmarkergoogletype.g Reen);                 Bitmap bItmap = Bitmap.fromfile ("F:projectsgmapdemogmapdemoimagea.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 mapcontr Ol_onmapzoomchanged ()         {       }           Priva te void Buttonbeginblink_click (object sender, EventArgs e)         {        &NBSP ;   BlinktimEr. Interval = 1000;             Blinktimer.tick + + new EventHandler (Blinktimer_tick);             Blinktimer.start ();        }           void Blinktimer_tick (object sender, EventArgs e)   & nbsp     {            foreach (Gmapmarker m in objects.) Markers)             {                if (M is GMap Markerimage)                 {              &N Bsp     Gmapmarkerimage marker = m as gmapmarkerimage;                     if (marker. Outpen = null)                         marker. Outpen = new Pen (brushes.red, 2);                     ELSE                     {            &NBS P           marker. Outpen.dispose ();                         marker. Outpen = null;                                   & nbsp {           }             Mapcontrol.refresh ();        }           private void Buttonstopblink_click (object sender, Eventar GS e)         {            blinktimer.stop ()       &NBS P     foreach (Gmapmarker m in objects. Markers)             {                if (M is GMap Markerimage)                {                    gmapmarkerimage marker = m as Gmapma Rkerimage;                     marker. Outpen.dispose ();                     marker. Outpen = null;                            }             Mapcontrol.refresh ();        }    }}  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.