GMap study notes.
GMap Study Notes
1. GMap system details
- What isMap control(GMapControl)? This is the control which renders the map.
- What isOverlay(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 areMarkers(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 isRoute(GMapRoute)? This is the path or direction between two or more poin
2. c # Use GMap to implement specific functions (MAP loading, zoom-in, zoom-out, eagleeye, DOT/line surface adding, custom marker, and download cache)
Note: addGMap. NET. Core. dllAndGMap. NET. WindowsForms. dll file,Use the GMap control after the reference.
2.1 load map
SuperMap iServer REST service is called directly here. Call third-party map service reference http://www.cnblogs.com/luxiaoxun/p/3364107.html
2.2 zoom in and out a map
Private void tsbZoomIn_Click (object sender, EventArgs e) {this. mapControl1.Zoom + = 1;} private void tsbZoomOut_Click (object sender, EventArgs e) {this. mapControl1.Zoom-= 1;} // The Event private void mapcontrolpoliconmapzoomchanged () {double zoom = this. mapControl1.Zoom-Convert. toDouble (5); this. mapControl2.Zoom = zoom; // set the map zoom size}
View Code
2.3 eagleeye
Two map controls are required to scale and move at the same time to achieve linkage effect.
GMapControl1 event setting code:
Private bool Mapleft1 = false; private void gmapcontrol+mousedown (object sender, MouseEventArgs e) {if (e. button = MouseButtons. left) {Mapleft1 = true;} private void gmapcontrol+mouseup (object sender, MouseEventArgs e) {if (e. button = MouseButtons. left) {Mapleft1 = false;} private void gmapcontrolpoliconpositionchanged (PointLatLng point) {if (Mapleft1) {this. gMapControl2.Position = point; // set the map center point} private void gmapcontrolpoliconmapzoomchanged () {double zoom = this. gMapControl1.Zoom-Convert. toDouble (5); this. gMapControl2.Zoom = zoom; // set the map zoom size}
View Code
GMapControl2 event setting code:
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; // click Set gMapControl1. center point Mapleft2 = true;} private void gMapControl2_OnPositionChanged (PointLatLng point) {if (Mapleft2) {this. gMapControl1.Position = point; // set gMapControl1 center point }}
View Code
2.4 Add a DOT/line Surface
Take adding a vertex object as an example:
IAction _ editAddAlarmAction = null; // Add the alarm source private void handle (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 overri De 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, MouseEven TArgs 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 \ y Aohui \ Desktop \ iClient-for-DotNet-master \ Demo \ demo. winform \ Resources \ sign-warning-icon.png "); markerOverlay. markers. add (marker); marker. toolTipText = "alarm source number:"; 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. onMous EOver;} _ 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 custom marker
Extended by inheriting the marker class of gmap: (The highlighted paint brush is added here)
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
// Save the map as an image 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 ("image saved:" + dialog. fileName, "GMap. NET ", MessageBoxButtons. OK, MessageBoxIcon. asterisk) ;}}}} catch (Exception exception) {MessageBox. show ("image saving failed:" + exception. message, "GMap. NET ", MessageBoxButtons. OK, MessageBoxIcon. hand );}}
View Code
2.7 save Cache
// Save the cache private void toolStripButton7_Click (object sender, EventArgs e) {if (this. mapControl1.ShowExportDialog () = true) {// this. gMapControl1.ShowTileGridLines = true; // display the tile, that is, display the square this. mapControl1.ReloadMap ();}}
View Code
3. iclient for. net simulates B/S to display the alarm blinking demo
4. Reference
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