WPF GMap使用高德地图

文章目录

  • 前言
  • 一、Nuget下载Gmap
  • 二、代码
    • 1.添加类
    • 2.加载高德地图
  • 总结


前言

近日在项目中用到了GMap,并且Gmap中使用了高德地图,特此记录一下。


一、Nuget下载Gmap

在Nuget中搜索GMap,选择GMap.NET.Presentatiom进行下载安装。(请注意版本)
WPF GMap使用高德地图_第1张图片

二、代码

1.添加类

添加一个类,让其GMapControl类。
代码如下:

using GMap.NET;
using GMap.NET.MapProviders;
using GMap.NET.Projections;
using GMap.NET.WindowsPresentation;
using System;

namespace WpfRiUi.gmap
{
    public class MapControl : GMapControl {
        public long ElapsedMilliseconds;
    }
}

2.加载高德地图

  1. 如果使用高德地图,需要实现GMapProvider接口,并添加一些方法
public abstract class AMapProviderBase : GMapProvider
    {
        public AMapProviderBase() 
        {
            MaxZoom = null;
            RefererUrl = "https://www.amap.com/";
        }

        public override PureProjection Projection 
        { 
            get { return MercatorProjection.Instance; }
        }

        GMapProvider[] overlays;
        
        public override GMapProvider[] Overlays 
        {
            get {
                if (overlays == null) { 
                    overlays = new GMapProvider[] { this };
                }
                return overlays;
            }
        }
    }
  1. 实现AMapProviderBase
using GMap.NET;
using GMap.NET.MapProviders;
using GMap.NET.Projections;
using GMap.NET.WindowsPresentation;
using System;

namespace WpfRiUi.gmap
{
    public class MapControl : GMapControl {
        public long ElapsedMilliseconds;
    }
    public abstract class AMapProviderBase : GMapProvider
    {
        public AMapProviderBase() 
        {
            MaxZoom = null;
            RefererUrl = "https://www.amap.com/";
        }

        public override PureProjection Projection 
        { 
            get { return MercatorProjection.Instance; }
        }

        GMapProvider[] overlays;
        
        public override GMapProvider[] Overlays 
        {
            get {
                if (overlays == null) { 
                    overlays = new GMapProvider[] { this };
                }
                return overlays;
            }
        }
    }

	
    public 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)
        {
            //string url = string.Format(UrlFormat, num, pos.X, pos.Y, zoom);
            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";
        static readonly string UrlFormat = "http://webrd01.is.autonavi.com/appmaptile?lang=zh_cn&size=1&scale=1&style=7&x={0}&y={1}&z={2}";
    }        
   
}

  1. 在xaml中使用

在window标签中引入xmlns:src=“clr-namespace:WpfRiUi.gmap”,也就前面我们写的类。然后在显示区域加入如下代码。

 <src:MapControl Grid.Row="0" x:Name="MainMap" Zoom="13" MaxZoom="24" MinZoom="1"/>
  1. 在主窗口中加载地图
    首先先写一个地图加载方法
        private void Map_Loaded()
        {
            try
            {
                System.Net.IPHostEntry e = System.Net.Dns.GetHostEntry("ditu.google.cn");
            }
            catch
            {
                MainMap.Manager.Mode = AccessMode.CacheOnly;
                System.Windows.MessageBox.Show("没有可用的internet连接,正在进入缓存模式!", "提示", MessageBoxButton.OK, MessageBoxImage.Warning);
            }
            MainMap.CacheLocation = Environment.CurrentDirectory + "\\GMapCache\\"; //缓存位置
            MainMap.MapProvider = AMapProvider.Instance; //加载高德地图
            MainMap.MinZoom = 2;  //最小缩放
            MainMap.MaxZoom = 17; //最大缩放
            MainMap.Zoom = 8;     //当前缩放
            MainMap.ShowCenter = false; //不显示中心十字点
            MainMap.DragButton = MouseButton.Left; //右键拖拽地图
            MainMap.Position = new PointLatLng(39.909149, 116.397486); //地图中心位置:北京
            //MainMap.MouseLeftButtonDown += new MouseButtonEventHandler(mapControl_MouseLeftButtonDown);
        }

然后不要忘了在窗口初始化时调用

 public MainWindow()
        {
            InitializeComponent();
            Rect rc = SystemParameters.WorkArea; //获取工作区大小
            this.Left = 0; //设置位置
            this.Top = 0;
            this.Width = rc.Width;
            this.Height = rc.Height;
            this.Map_Loaded();//加载地图
        }

总结

到此GMap已经可以成功的使用高德地图了,非常好用。如果您发现什么问题,请告诉我,本人万分感谢。

你可能感兴趣的:(WPF,高德地图,wpf)