栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > asp

在WinForm和WPF中使用GMap.Net地图插件简单教程

asp 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

在WinForm和WPF中使用GMap.Net地图插件简单教程

如何在WinForm中使用GMap.Net

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

下载GMap.Net,我下载的版本:greatmaps_81b71bf30091,编译三个核心项目:

GMap.Net.Core:核心DLL

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

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

在WinForm项目中使用GMap:

1、新建一个Visual C# 的Windows窗口程序。添加对GMap.Net.Core.DLL和GMap.Net.WindowsForms.DLL的引用。

2、在项目中添加一个UserControl,这里取名为MapControl,修改这个UserControl,使其继承于GMapControl,这就是展示地图的控件。修改如下:

复制代码 代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GMap.NET.WindowsForms;

namespace GMapWinFormDemo
{
    public partial class MapControl : GMapControl
    {
        public MapControl()
        {
            InitializeComponent();
        }
    }
}

3、编译项目,在我们的Form设计窗口下,在工具箱中(tool box)里就可以看到这个MapControl,将这个MapControl加到Form中。

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

复制代码 代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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.Navigation;
using System.Windows.Shapes;
using GMap.NET;
using GMap.NET.MapProviders;
using GMap.NET.WindowsPresentation;

namespace GMapWPFDemo
{
    ///


    /// MainWindow.xaml 的交互逻辑
    ///

    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            try
            {
                System.Net.IPHostEntry e = System.Net.Dns.GetHostEntry("www.google.com.hk");
            }
            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.onMapZoomChanged += new MapZoomChanged(mapControl_OnMapZoomChanged);
            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);
        }

        void mapControl_onMapZoomChanged()
        {
        }
    }
}

5、编译、运行项目就可以看到地图,这里使用的是在线的Google中国的地图,地图控件的几个主要属性:

MapProvider:地图服务的提供者。

MinZoom:最小缩放,最小可为1。

MaxZoom:最大缩放,最大为24.

Zoom:当前缩放。

ShowCenter:是否显示中心点(最好为false,否则地图中间会有一个红色的十字)。

DragButton:那个键拖动地图。

Position:地图中心点位置。

地图显示如下,支持左键拖动,放大缩小,可以显示左键的点击经纬度。

如何在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 GMapWPFDemo
{
    class MapControl : GMapControl
    {
    }
}

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

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

复制代码 代码如下:
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:src="clr-namespace:GMapWPFDemo"
        Title="MainWindow" Height="410" Width="618">
   
           
           
           

   


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

复制代码 代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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.Navigation;
using System.Windows.Shapes;
using GMap.NET;
using GMap.NET.MapProviders;
using GMap.NET.WindowsPresentation;

namespace GMapWPFDemo
{
    ///


    /// MainWindow.xaml 的交互逻辑
    ///

    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            try
            {
                System.Net.IPHostEntry e = System.Net.Dns.GetHostEntry("www.google.com.hk");
            }
            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.onMapZoomChanged += new MapZoomChanged(mapControl_OnMapZoomChanged);
            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);
        }

        void mapControl_onMapZoomChanged()
        {
        }
    }
}

效果图如下:

和Winform代码差不多,一些响应事件不同,WPF的GMap中没有GMapOverlay这个“图层”的概念,所以没法加多个GMapOverlay,在GMapOverlay上再加GMapMarker(可以理解为图标标注),GMapMarker只能直接加在mapControl上面。

WPF的GMapMarker可以直接实例化(WinForm中的不行),但是貌似没有默认提供的效果,而要做出一些效果,需要自己设计实现,官方Demo中已经有了一些实现,WinForm中的GMapMarker可以用GMarkerGoogle去实例化(提供的有可选的效果,也可以自己传入bitmap作为自定义的图标)。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/57361.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号