GMap.Net 在 winform 中调用高德地图
参考资料:
[GMap 在winform 中调用高德地图]((https://blog.csdn.net/qq_35699528/article/details/71599521)
1、创建 UserControl 为MapControl
public partial class MapControl : GMapControl { /** * mapControl : 显示主地图 * overlay :显示轨迹 * makers :显示关键点 */ public MapControl() { InitializeComponent(); } }
2、创建类 GMapProvider
public abstract class AMapProviderBase : GMapProvider { public AMapProviderBase() { MaxZoom = null; RefererUrl = "http://www.amap.com/"; Copyright = string.Format("©{0} 高德 Corporation, ©{0} NAVTEQ, ©{0} Image courtesy of NASA", DateTime.Today.Year); } public override PureProjection Projection { get { return MercatorProjection.Instance; } } GMapProvider[] overlays; public override GMapProvider[] Overlays { get { if (overlays == null) { overlays = new GMapProvider[] { this }; } return overlays; } } }
3、创建类 AMapProvider 继承 AMapProviderBase
class AMapProvider : AMapProviderBase { public static readonly AMapProvider Instance; readonly Guid id = new Guid("EF3DD303-3F74-4938-BF40-232D0595EE88"); public override Guid Id { get { return id; } } readonly string name = "AMap"; public override string Name { get { return name; } } static AMapProvider() { Instance = new AMapProvider(); } public override PureImage GetTileImage(GPoint pos, int zoom) { string url = MakeTileImageUrl(pos, zoom, LanguageStr); return GetTileImageUsingHttp(url); } string MakeTileImageUrl(GPoint pos, int zoom, string language) { //http://webrd04.is.autonavi.com/appmaptile?x=5&y=2&z=3&lang=zh_cn&size=1&scale=1&style=7 string url = string.Format(UrlFormat, pos.X, pos.Y, zoom); Console.WriteLine("url:" + url); return url; } static readonly string UrlFormat = "http://webrd04.is.autonavi.com/appmaptile?x={0}&y={1}&z={2}&lang=zh_cn&size=1&scale=1&style=7"; }
4、在主Form中
public partial class Form1 : Form { private GMapOverlay markersOverlay = new GMapOverlay("markers"); //放置marker的图层 // GMapOverlay polyOverlay = new GMapOverlay(“polygons”); GMapOverlay polyOverlay = new GMapOverlay("Routes"); public Form1() { InitializeComponent(); try { // System.Net.IPHostEntry e = System.Net.Dns.GetHostEntry(“ditu.amap.com”); } catch { gMapControl.Manager.Mode = AccessMode.CacheOnly; MessageBox.Show("No internet connection avaible, going to CacheOnly mode.", "GMap.NET Demo", MessageBoxButtons.OK, MessageBoxIcon.Warning); } gMapControl.CacheLocation = Environment.CurrentDirectory + "\\GMapCache\\"; //缓存位置 // gMapControl.MapProvider = GMapProviders.GoogleChinaMap; //google china 地图 //详情看类AMapProvider与GMapProviders即可明白 gMapControl.MapProvider = AMapProvider.Instance; gMapControl.MinZoom = 2; //最小比例 gMapControl.MaxZoom = 24; //最大比例 gMapControl.Zoom = 10; //当前比例 gMapControl.ShowCenter = false; //不显示中心十字点 gMapControl.DragButton = System.Windows.Forms.MouseButtons.Left; //左键拖拽地图 gMapControl.Position = new PointLatLng(36.0561596067835,103.738231658936); //地图中心位置:兰州理工大学 GMapMarker marker = new GMarkerGoogle(gMapControl.Position, GMarkerGoogleType.green); markersOverlay.Markers.Add(marker); gMapControl.Overlays.Add(markersOverlay); // 添加多边形节点 List<PointLatLng> points = new List<PointLatLng>(); points.Add(new PointLatLng(37.1412768409666, 79.6562057733536)); points.Add(new PointLatLng(37.1403574434934, 79.6564793586731)); points.Add(new PointLatLng(37.1405669815751, 79.6581155061722)); points.Add(new PointLatLng(37.1416146632773, 79.6577936410904)); points.Add(new PointLatLng(37.1412768409666, 79.6562057733536)); //绘制折线用GMapRoute GMapRoute route = new GMapRoute(points, ""); route.Stroke = new Pen(Color.Red, 1); polyOverlay.Routes.Add(route); //绘制多边形用GMapPolygon GMapPolygon polygon = new GMapPolygon(points, "polygons"); polygon.Fill = new SolidBrush(Color.FromArgb(50, Color.Green)); polygon.Stroke = new Pen(Color.Red, 1); polyOverlay.Polygons.Add(polygon); gMapControl.Overlays.Add(polyOverlay); // 创建鼠标按键事件 gMapControl.MouseClick += new MouseEventHandler(gMapControl_MouseClick); } void gMapControl_MouseClick(object sender, MouseEventArgs e) { if (e.Button == System.Windows.Forms.MouseButtons.Right) { PointLatLng point = gMapControl.FromLocalToLatLng(e.X, e.Y); GMapMarker marker = new GMarkerGoogle(point, GMarkerGoogleType.green); markersOverlay.Markers.Add(marker); Console.WriteLine(point.ToString()); } } /// <summary> /// 改变 Polygons 填充颜色 /// </summary> /// <param name="name">Polygons 名称</param> void changePolygonsColor(String name) { for (int i = 0; i < gMapControl.Overlays.Count; i++) { for (int j = 0; j < gMapControl.Overlays[i].Polygons.Count; j++) { if (gMapControl.Overlays[i].Polygons[j].Name == name) { gMapControl.Overlays[i].Polygons[j].Fill = new SolidBrush(Color.FromArgb(50, Color.Red)); } } } } }