C#

WPF GMap.Net开发

王先生
2020-04-26 / 0 评论 / 7 阅读 / 正在检测是否收录...

GMap.NET是什么?

来看看它的官方说明:GMap.NET is great and Powerful, Free, cross platform, open source .NET control. Enable use routing, geocoding, directions and maps from Coogle, Yahoo!, Bing, OpenStreetMap, ArcGIS, Pergo, SigPac, Yendux, Mapy.cz, Maps.lt, iKarte.lv, NearMap, OviMap, CloudMade, WikiMapia, MapQuest in Windows Forms & Presentation, supports caching and runs on windows mobile!

GMap.NET是一个强大、免费、跨平台、开源的.NET控件,它在Windows Forms 和WPF环境中能够使用来自Google, Yahoo!, Bing, OpenStreetMap, ArcGIS, Pergo, SigPac等地图,并可以实现寻找路径、地理编码以及地图展示功能,并支持缓存和运行在Mobile环境中。

项目主页:https://greatmaps.codeplex.com/

Gmap下载
下载GMap.Net,编译三个核心项目:

GMap.Net.Core:核心DLL

GMap.Net.WindowsForms:WinForm中使用的DLL

GMap.NET.WindowsPresentation:WPF中使用的DLL

如何在WPF中使用GMap.Net

1、新建一个Visual C# 的WPF程序。添加对GMap.Net.Core.DLL和GMap.NET.WindowsPresentation.DLL的引用。

2、由于WPF的UserControl不能修改继承的基类,所以添加一个新的类,为MapControl.cs,代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GMap.NET.WindowsPresentation;

namespace _7LocationNavigation
{
    class MapControl: GMapControl
    {
    }
}

只需要继承GMapControl就行了,基本功能都可以由GMapControl提供。

3、在我们的MainWindow.xaml中,添加项目的namespace:xmlns:src="clr-namespace:GMapWPFDemo",在XML代码中添加对MapControl.cs的使用:

<Window
        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:_7LocationNavigation"
        xmlns:WindowsPresentation="clr-namespace:GMap.NET.WindowsPresentation;assembly=GMap.NET.WindowsPresentation" x:Class="_7LocationNavigation.MainWindow"
        mc:Ignorable="d"
        xmlns:src="clr-namespace:_7LocationNavigation"
        Title="MainWindow" Height="450" Width="800">
    
    <Grid>
        <GroupBox Header="GroupBox" HorizontalAlignment="Left" Height="401" Margin="10,10,0,0" VerticalAlignment="Top" Width="773">
            <src:MapControl x:Name="mapControl" Zoom="13" MaxZoom="24" MinZoom="1" />
        </GroupBox>

    </Grid>
</Window>

4、在MainWindow中添加相关的代码如下:


引用

using GMap.NET;
using GMap.NET.MapProviders;
using GMap.NET.WindowsPresentation;

代码

            try
            {
                System.Net.IPHostEntry e = System.Net.Dns.GetHostEntry("map.baidu.com");
            }
            catch
            {
                mapControl.Manager.Mode = AccessMode.CacheOnly;
                MessageBox.Show("No internet connection avaible, going to CacheOnly mode.", "GMap.NET Demo", MessageBoxButton.OK, MessageBoxImage.Warning);
            }

            mapControl.MapProvider = GMapProviders.GoogleChinaMap; //google china 地图
            mapControl.MinZoom = 2;  //最小缩放
            mapControl.MaxZoom = 17; //最大缩放
            mapControl.Zoom = 5;     //当前缩放
            mapControl.ShowCenter = false; //不显示中心十字点
            mapControl.DragButton = MouseButton.Left; //左键拖拽地图
            mapControl.Position = new PointLatLng(32.064, 118.704); //地图中心位置:南京

            mapControl.MouseLeftButtonDown += new MouseButtonEventHandler(mapControl_MouseLeftButtonDown);
        }

        void mapControl_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            Point clickPoint = e.GetPosition(mapControl);
            PointLatLng point = mapControl.FromLocalToLatLng((int)clickPoint.X, (int)clickPoint.Y);
            GMapMarker marker = new GMapMarker(point);
            mapControl.Markers.Add(marker);
        }

参考文章:
https://www.cnblogs.com/luxiaoxun/p/3463250.html
https://blog.csdn.net/wshk918/article/details/103076060

评论 (0)

取消