目录
编辑
1.环境
2. NuGet导入依赖
3. 添加MapControl类
4. 编辑MainView.xaml.cs
5. 编辑MainView.xaml
6. 启动验证
源码: https://github.com/liugang198409/WpfDemo/tree/master/GMapDemo
视频:WPF编程--地图控件GMap_哔哩哔哩_bilibili
1.环境
VVisual Studio 2019 + .NET Framework 4.8.1
2. NuGet导入依赖
导入依赖GMap.NET.Presentation
文章来源:https://www.toymoban.com/news/detail-497718.html
3. 添加MapControl类
using GMap.NET;
using GMap.NET.MapProviders;
using GMap.NET.Projections;
using GMap.NET.WindowsPresentation;
using System;
namespace GMapDemo
{
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}";
}
}
4. 编辑MainView.xaml.cs
using GMap.NET;
using System;
using System.Windows;
using System.Windows.Input;
namespace GMapDemo
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Rect rc = SystemParameters.WorkArea; //获取工作区大小
this.Left = 0; //设置位置
this.Top = 0;
this.Width = rc.Width;
this.Height = rc.Height;
this.Map_Loaded();//加载地图
}
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);
}
}
}
5. 编辑MainView.xaml
<Window x:Class="GMapDemo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:src="clr-namespace:GMapDemo"
xmlns:local="clr-namespace:GMapDemo"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<src:MapControl Grid.Row="0" x:Name="MainMap" Zoom="13" MaxZoom="24" MinZoom="1"/>
</Grid>
</Window>
6. 启动验证
文章来源地址https://www.toymoban.com/news/detail-497718.html
到了这里,关于WPF编程--地图控件GMap使用的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!