1, first add the dynamic link library. dll file that references Gmap.net
2, Initialize map information
Public voidmapshow () {Try{System.Net.IPHostEntry ELA= System.Net.Dns.GetHostEntry ("www.google.com.hk"); } Catch{Gmap_control. Manager.mode=Accessmode.serverandcache; MessageBox.Show ("No Internet connection avaible, going to cacheonly mode.", "gmap.net Demo", MessageBoxButtons.OK, messageboxicon.warning); } gmap_control. CacheLocation= Environment.currentdirectory +"\\GMapCache\\";//Cache LocationMyinvoke my =NewMyinvoke (Init); This. BeginInvoke (my); }
Public Delegate voidMyinvoke (); Public voidInit () {Gmap_control. Minzoom=4;//Minimum scaleGmap_control. Maxzoom = A;//Maximum scaleGmap_control. Zoom =4; Gmap_control. Dragbutton= System.Windows.Forms.MouseButtons.Left;//left-Drag the mapGmap_control. Position=NewPOINTLATLNG (32.064,118.704);//Map Center Location: Nanjing }
// Google China map New Thread (mapshow). Start ();
3, add a market
New Gmapoverlay (this"mymarkss"); New Gmapmarkergooglegreen (Gmap_control. Position); MYMARKSS.MARKERS.ADD (GN); This . Gmap_control. Overlays.add (MYMARKSS); = markertooltipmode.always; " Prompt Information "
4, add a custom market
classGmapmarkerdirection:gmapmarker { Public floatAng; Privateimage Image; Publicimage Image {Get { returnimage; } Set{image=value; if(Image! =NULL) { This. Size =NewSize (image. Width, image. Height); } } } PublicGmapmarkerdirection (pointlatlng p, image image,floatangle):Base(p) {Ang= Angle- -; if(Ang > the) {Ang-= the; } if(Ang <0) {Ang+= the; } Image=image; Size=NewSystem.Drawing.Size (image. Width, image. Height); Offset=NewSystem.Drawing.Point (-size.width/2,-size.height/2); } Public Override voidOnRender (Graphics g) {g.drawimageunscaled (Rotateimage (Image, Ang), localposition.x, Localposition. Y); } PrivateBitmap Rotateimage (image Image,floatangle) { if(Image = =NULL) Throw NewArgumentNullException ("Image"); Const DoublePi2 = Math.PI/2.0; DoubleOldWidth = (Double) image. Width; DoubleOldHeight = (Double) image. Height; DoubleTheta = ((Double) angle) * Math.PI/180.0; DoubleLocked_theta =Theta; while(Locked_theta <0.0) Locked_theta+=2*Math.PI; DoubleNewwidth, Newheight; intnwidth, nheight; #regionExplaination of the calculations#endregion DoubleAdjacenttop, Oppositetop; DoubleAdjacentbottom, Oppositebottom; if((Locked_theta >=0.0&& Locked_theta < pi2) | |(Locked_theta>= Math.PI && Locked_theta < (Math.PI +PI2))) {Adjacenttop= Math.Abs (Math.Cos (Locked_theta)) *OldWidth; Oppositetop= Math.Abs (Math.sin (Locked_theta)) *OldWidth; Adjacentbottom= Math.Abs (Math.Cos (Locked_theta)) *OldHeight; Oppositebottom= Math.Abs (Math.sin (Locked_theta)) *OldHeight; } Else{adjacenttop= Math.Abs (Math.sin (Locked_theta)) *OldHeight; Oppositetop= Math.Abs (Math.Cos (Locked_theta)) *OldHeight; Adjacentbottom= Math.Abs (Math.sin (Locked_theta)) *OldWidth; Oppositebottom= Math.Abs (Math.Cos (Locked_theta)) *OldWidth; } newwidth= Adjacenttop +Oppositebottom; Newheight= Adjacentbottom +Oppositetop; Nwidth= (int) math.ceiling (newwidth); Nheight= (int) math.ceiling (newheight); Bitmap rotatedbmp=NewBitmap (nwidth, nheight); using(Graphics g =graphics.fromimage (rotatedbmp)) {point[] points; if(Locked_theta >=0.0&& Locked_theta <pi2) {Points=Newpoint[] {NewPoint (int) Oppositebottom,0 ), NewPoint (Nwidth, (int) oppositetop),NewPoint (0, (int) adjacentbottom)}; } Else if(Locked_theta >= pi2 && Locked_theta <Math.PI) {points=Newpoint[] {NewPoint (Nwidth, (int) oppositetop),NewPoint (int) Adjacenttop, nheight),NewPoint (int) Oppositebottom,0 ) }; } Else if(Locked_theta >= Math.PI && Locked_theta < (Math.PI +pi2)) {Points=Newpoint[] {NewPoint (int) Adjacenttop, nheight),NewPoint (0, (int) adjacentbottom),NewPoint (Nwidth, (int) oppositetop)}; } Else{points=Newpoint[] {NewPoint (0, (int) adjacentbottom),NewPoint (int) Oppositebottom,0 ), NewPoint (int) Adjacenttop, nheight)}; } g.drawimage (image, points); } returnrotatedbmp; } }
Image image = Image.FromFile (Application.startuppath +"\\markert.png"); //Location is the coordinate, image is the market picture, Max is the angle of rotationGmapmarkerdirection MYMARKSS =Newgmapmarkerdirection (location, image, Max); Gmapmarkergooglegreen GN=NewGmapmarkergooglegreen (Gmap_control. Position); MYMARKSS.MARKERS.ADD (GN); This. Gmap_control. Overlays.add (MYMARKSS); Gn. Tooltipmode=markertooltipmode.always; Gn. ToolTipText="Prompt Information"
5, draw a line between two points
POINTLATLNG star =Newpointlatlng (); POINTLATLNG End=Newpointlatlng (); List<PointLatLng> list =NewList<pointlatlng>(); List. ADD (Star); List. ADD (end); Gmapoverlay Routes=NewGmapoverlay ( This. Gmap_control,"Routes"); This. Gmap_control. Overlays.add (routes);//add to List of layersGmaproute route =NewGmaproute (list,"Action"); Routes. Routes.add (route);
6, calculate the direction of point A at point B
Double / Math.PI; int max = (int) angleofline; if the ) { n; } if 0 ) { n; }
Use of C # Gmap.net controls