• (转载)在WinForm中使用GMap.Net地图插件


    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/

                 http://webchat.freenode.net/?channels=#GMap.NET

    如何在WinForm中使用GMap.Net

    下载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.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using GMap.NET;
    using GMap.NET.WindowsForms;
    using GMap.NET.MapProviders;
    using GMap.NET.WindowsForms.Markers;
    
    namespace GMapDemo
    {
    public partial class MapForm : Form
    {
    private GMapOverlay markersOverlay = new GMapOverlay("markers"); //放置marker的图层
    
    public MapForm()
    {
    InitializeComponent();
    
    try
    {
    System.Net.IPHostEntry e = System.Net.Dns.GetHostEntry("ditu.google.cn");
    }
    catch
    {
    mapControl.Manager.Mode = AccessMode.CacheOnly;
    MessageBox.Show("No internet connection avaible, going to CacheOnly mode.", "GMap.NET Demo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
    }
    
    mapControl.CacheLocation = Environment.CurrentDirectory + "\GMapCache\"; //缓存位置
    mapControl.MapProvider = GMapProviders.GoogleChinaMap; //google china 地图
    mapControl.MinZoom = 2; //最小比例
    mapControl.MaxZoom = 24; //最大比例
    mapControl.Zoom = 10; //当前比例
    mapControl.ShowCenter = false; //不显示中心十字点
    mapControl.DragButton = System.Windows.Forms.MouseButtons.Left; //左键拖拽地图
    mapControl.Position = new PointLatLng(32.064,118.704); //地图中心位置:南京
    
    mapControl.Overlays.Add(markersOverlay);
    
    mapControl.MouseClick += new MouseEventHandler(mapControl_MouseClick);
    }
    
    void mapControl_MouseClick(object sender, MouseEventArgs e)
    {
    if (e.Button == System.Windows.Forms.MouseButtons.Right)
    {
    PointLatLng point = mapControl.FromLocalToLatLng(e.X, e.Y);
    GMapMarker marker = new GMarkerGoogle(point, GMarkerGoogleType.green);
    markersOverlay.Markers.Add(marker);
    }
    }
    }

    5、编译、运行项目就可以看到地图。

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

    7、这里使用的是在线的Google中国的地图(网址换成百度地图就可以使用百度的)。

    8、地图控件的几个主要属性:

    MapProvider:地图服务的提供者。

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

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

    Zoom:当前缩放。

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

    DragButton:哪个键拖动地图。

    Position:地图中心点位置。

     转载自:http://www.cnblogs.com/luxiaoxun/p/3463250.html

    发现自己的不足,善于利用找到的方法去扬长避短。行动起来。
  • 相关阅读:
    VS2008编译的程序在某些机器上运行提示“由于应用程序配置不正确,应用程序未能启动”的问题
    C++中的预处理命令 .
    C++ sizeof用法 .
    详解C/C++预处理器 .
    C 风格字符串,C++string类,MFC,CString类的区别。
    VC: GDI绘图基本步骤总结 .
    关于字符数组 和 字符串比较 C++
    they're hiring
    HTTP Proxy Server
    Polipo
  • 原文地址:https://www.cnblogs.com/rechen/p/5056535.html
Copyright © 2020-2023  润新知