C#的WPF下通过使用GMap.NET引入高德地图
参考https://www.cnblogs.com/enjoyeclipse/archive/2013/01/27/2859029.html
首先需要引用GMap.NET
using GMap.NET; using GMap.NET.MapProviders; using GMap.NET.WindowsPresentation;
AMapProvider.cs
using System; using GMap.NET.Internals; using GMap.NET.Projections; namespace GMap.NET.MapProviders { public class AMapProvider : AMapProviderBase { private readonly string name = "AMap"; private readonly string language = "zh_cn"; private readonly Guid id = new Guid("F81F5FB4-0902-4686-BF5B-B2B1E4D47922"); public static readonly AMapProvider Instance; private Random ran = new Random(); private static string UrlFormat = "http://webrd0{0}.is.autonavi.com/appmaptile?lang=zh_cn&size=1&style=7&x={1}&y={2}&z={3}&scale=1<ype=3"; public string Caption { get { return "高德地图"; } } public override Guid Id { get { return this.id; } } public override string Name { get { return this.name; } } static AMapProvider() { Instance = new AMapProvider(); } public AMapProvider() { } public override PureImage GetTileImage(GPoint pos, int zoom) { string url = MakeTileImageUrl(pos, zoom, language); return GetTileImageUsingHttp(url); } //http://wprd0{0}.is.autonavi.com/appmaptile?lang=zh_cn&size=1&style=7&x={1}&y={2}&z={3}&scl=2<ype=3 private string MakeTileImageUrl(GPoint pos, int zoom, string language) { int serverID = ran.Next(1, 5);//1-4 return string.Format(UrlFormat, 4, pos.X, pos.Y, zoom); } } public abstract class AMapProviderBase : GMapProvider { protected GMapProvider[] overlays; public AMapProviderBase() { RefererUrl = "http://www.amap.com/"; Copyright = string.Format("©{0} 高德地图 GPRS(@{0})", DateTime.Today.Year); MinZoom = 1; MaxZoom = 20; } public override GMapProvider[] Overlays { get { if (overlays == null) { overlays = new GMapProvider[] { this }; } return overlays; } } public override PureProjection Projection { get { return MercatorProjection.Instance; } } } }
MapControl.cs
using GMap.NET.WindowsPresentation; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace WpfApp.Controls { class MapControl : GMapControl { } }
AMapWindow.xaml
<Window x:Class="WpfApp.Views.AMapWindow" 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:local="clr-namespace:WpfApp.Views" xmlns:control="clr-namespace:WpfApp.Controls" mc:Ignorable="d" Title="AMapWindow" Height="450" Width="800"> <Grid> <GroupBox Name="mapgroup" VerticalContentAlignment="Stretch" HorizontalContentAlignment="Stretch"> <control:MapControl x:Name="mapControl" Zoom="13" MaxZoom="24" MinZoom="1" /> </GroupBox> </Grid> </Window>
AMapWindow.xaml.cs
using GMap.NET; using GMap.NET.MapProviders; using GMap.NET.WindowsPresentation; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Shapes; namespace WpfApp.Views { /// <summary> /// AMapWindow.xaml 的交互逻辑 /// </summary> public partial class AMapWindow : Window { public AMapWindow() { InitializeComponent(); mapControl.MapProvider = AMapProvider.Instance; //google china 地图 mapControl.Manager.Mode = AccessMode.ServerAndCache; mapControl.MinZoom = 2; //最小缩放 mapControl.MaxZoom = 17; //最大缩放 mapControl.Zoom = 5; //当前缩放 mapControl.ShowCenter = false; //不显示中心十字点 mapControl.DragButton = MouseButton.Left; //左键拖拽地图 mapControl.Position = new PointLatLng(32.064, 118.704); //地图中心位置:南京 } } }