国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

在WinForm和WPF中使用GMap.Net地圖插件簡單教程

2020-01-17 23:39:06
字體:
來源:轉載
供稿:網友

如何在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
{
    /// <summary>
    /// MainWindow.xaml 的交互邏輯
    /// </summary>
    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的使用:

復制代碼 代碼如下:

<Window x:Class="GMapWPFDemo.MainWindow"
        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">
    <Grid>
            <GroupBox Name="mapgroup" VerticalContentAlignment="Stretch" HorizontalContentAlignment="Stretch">
            <src:MapControl x:Name="mapControl" Zoom="13" MaxZoom="24" MinZoom="1" />
            </GroupBox>
    </Grid>
</Window>

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
{
    /// <summary>
    /// MainWindow.xaml 的交互邏輯
    /// </summary>
    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作為自定義的圖標)。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 宝丰县| 易门县| 岳阳县| 怀安县| 东平县| 古交市| 晋中市| 沂水县| 汶上县| 中江县| 福清市| 上犹县| 嘉善县| 常山县| 新河县| 营山县| 察雅县| 江川县| 建德市| 洛浦县| 惠来县| 平定县| 德清县| 工布江达县| 红桥区| 罗定市| 长沙县| 乌拉特中旗| 连山| 沁源县| 西贡区| 平潭县| 竹溪县| 柏乡县| 秦安县| 广德县| 洛浦县| 饶河县| 衡阳县| 遂昌县| 邵武市|