C# Gmap.net控件的使用

来源:互联网 发布:python程序化交易 编辑:程序博客网 时间:2024/06/09 20:01

1,首先要先添加引用Gmap.net的动态链接库.dll文件

2,初始化地图信息

 public void MapShow()        {            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\\"; //缓存位置            MyInvoke my = new MyInvoke(Init);            this.BeginInvoke(my);        }

 public delegate void MyInvoke();        public void Init()        {            gMap_control.MinZoom = 4;  //最小比例            gMap_control.MaxZoom = 22; //最大比例            gMap_control.Zoom = 4;            gMap_control.DragButton = System.Windows.Forms.MouseButtons.Left; //左键拖拽地图            gMap_control.Position = new PointLatLng(32.064, 118.704); //地图中心位置:南京        }
gMap_control.MapProvider = GMapProviders.GoogleChinaMap; //google china 地图        new Thread(MapShow).Start();


3,添加一个market

 GMapOverlay MyMarkss = new GMapOverlay(this.gMap_control, "MyMarkss");        GMapMarkerGoogleGreen gn = new GMapMarkerGoogleGreen(gMap_control.Position);        MyMarkss.Markers.Add(gn);        this.gMap_control.Overlays.Add(MyMarkss);        gn.ToolTipMode = MarkerTooltipMode.Always;        gn.ToolTipText = "提示信息"


4,添加一个自定义的market

class GMapMarkerDirection : GMapMarker    {        public float Ang;        private Image image;        public Image Image        {            get            {                return image;            }            set            {                image = value;                if (image != null)                {                    this.Size = new Size(image.Width, image.Height);                }            }        }        public GMapMarkerDirection(PointLatLng p, Image image, float angle)            : base(p)        {            Ang = angle - 90;            if (Ang > 360)            {                Ang -= 360;            }            if (Ang < 0)            {                Ang += 360;            }            Image = image;            Size = new System.Drawing.Size(image.Width, image.Height);            Offset = new System.Drawing.Point(-Size.Width / 2, -Size.Height / 2);        }        public override void OnRender(Graphics g)        {            g.DrawImageUnscaled(RotateImage(Image, Ang), LocalPosition.X, LocalPosition.Y);        }        private Bitmap RotateImage(Image image, float angle)        {            if (image == null)                throw new ArgumentNullException("image");            const double pi2 = Math.PI / 2.0;            double oldWidth = (double)image.Width;            double oldHeight = (double)image.Height;            double theta = ((double)angle) * Math.PI / 180.0;            double locked_theta = theta;            while (locked_theta < 0.0)                locked_theta += 2 * Math.PI;            double newWidth, newHeight;            int nWidth, nHeight;            #region Explaination of the calculations            #endregion            double adjacentTop, oppositeTop;            double adjacentBottom, 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 = new Bitmap(nWidth, nHeight);            using (Graphics g = Graphics.FromImage(rotatedBmp))            {                Point[] points;                if (locked_theta >= 0.0 && locked_theta < pi2)                {                    points = new Point[] {                                              new Point( (int) oppositeBottom, 0 ),                                              new Point( nWidth, (int) oppositeTop ),                                             new Point( 0, (int) adjacentBottom )                                         };                }                else if (locked_theta >= pi2 && locked_theta < Math.PI)                {                    points = new Point[] {                                              new Point( nWidth, (int) oppositeTop ),                                             new Point( (int) adjacentTop, nHeight ),                                             new Point( (int) oppositeBottom, 0 )                                                                  };                }                else if (locked_theta >= Math.PI && locked_theta < (Math.PI + pi2))                {                    points = new Point[] {                                              new Point( (int) adjacentTop, nHeight ),                                              new Point( 0, (int) adjacentBottom ),                                             new Point( nWidth, (int) oppositeTop )                                         };                }                else                {                    points = new Point[] {                                              new Point( 0, (int) adjacentBottom ),                                              new Point( (int) oppositeBottom, 0 ),                                             new Point( (int) adjacentTop, nHeight )                                                 };                }                g.DrawImage(image, points);            }            return rotatedBmp;        }    }


Image image = Image.FromFile(Application.StartupPath + "\\markert.png");//location是坐标,image是market图片,max是旋转角度GMapMarkerDirection MyMarkss = new GMapMarkerDirection(location, image, max);        GMapMarkerGoogleGreen gn = new GMapMarkerGoogleGreen(gMap_control.Position);        MyMarkss.Markers.Add(gn);        this.gMap_control.Overlays.Add(MyMarkss);        gn.ToolTipMode = MarkerTooltipMode.Always;        gn.ToolTipText = "提示信息"


5,在两点之间画线

PointLatLng star = new PointLatLng();PointLatLng end= new PointLatLng();List<PointLatLng> list = new List<PointLatLng>();list.Add(star);list.Add(end);GMapOverlay routes = new GMapOverlay(this.gMap_control, "routes");this.gMap_control.Overlays.Add(routes);//添加到图层列表中  GMapRoute route = new GMapRoute(list, "action");routes.Routes.Add(route);



6,计算A点在B点的方向

double angleOfLine = Math.Atan2((end.Lng - star.Lng), (end.Lat - star.Lat)) * 180 / Math.PI;        int max = (int)angleOfLine;        if (max > 360)        {           max -= 360;        }        if (max < 0)        {           max += 360;        }



0 0
原创粉丝点击