Simple tutorial for using the GMap. Net map plug in in WinForm and WPF

  • 2020-09-16 07:25:53
  • OfStack

How do I use ES2en.Net in WinForm

Home page of the project: https: / / greatmaps codeplex. com /

Download ES12en. Net, the version I downloaded: greatmaps_81b71bf30091, compile 3 core projects:

GMap.Net.Core: Core DLL

GMap.Net.WindowsForms: DLL used in WinForm

GMap.NET.WindowsPresentation: DLL used in WPF

Using GMap in the WinForm project:

1. Create a new Windows window program for Visual C# Add to GMap. Net. Core. DLL and GMap Net. WindowsForms. DLL references.

2. Add 1 UserControl to the project, named MapControl here, and modify this UserControl to inherit from GMapControl, which is the control to display the map. The modification is as follows:


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. Compile the project. Under the design window of Form, you can see this MapControl in the toolbox (tool box), and add this MapControl to Form.

4. Add relevant codes to main Form as follows:


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  Interaction logic of 
    /// </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  The map 
            mapControl.MinZoom = 2;  // The minimum zoom 
            mapControl.MaxZoom = 17; // The largest scale 
            mapControl.Zoom = 5;     // The current zoom 
            mapControl.ShowCenter = false; // Do not show center 10 Word point 
            mapControl.DragButton = MouseButton.Left; // Left-click to drag the map 
            mapControl.Position = new PointLatLng(32.064, 118.704); // Map center location: Nanjing 
            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. After compiling and running the project, you can see the map. Here is the online Google Map of China.

MapProvider: Provider of mapping services.

MinZoom: Minimum scaling, up to 1.

MaxZoom: Maximum zoom, maximum 24.

Zoom: Currently zooming.

ShowCenter: Whether the center point is displayed (preferably false, otherwise there will be a red 10 word in the middle of the map).

DragButton: That key drag the map.

Position: Location of the center of the map.

The map is shown as follows. Left-click drag is supported to zoom in and out, and left-click longitude and latitude can be displayed.

How to use ES107en.Net in WPF

1. Create a new Visual C# WPF program. Add to GMap. Net. Core. DLL and GMap NET. WindowsPresentation. DLL references.

2. Since WPF's UserControl cannot modify the inherited base class, add a new class, MapControl.cs, the code is as follows:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GMap.NET.WindowsPresentation;
namespace GMapWPFDemo
{
    class MapControl : GMapControl
    {
    }
}

You just need to inherit from GMapControl, and the basic functionality can be provided by GMapControl.

3. In our ES137en.xaml, add namespace: xmlns:src=" clr-ES143en :GMapWPFDemo", add the use of ES146en.cs in the XML code:


<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. Add relevant codes to MainWindow as follows:


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  Interaction logic of 
    /// </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  The map 
            mapControl.MinZoom = 2;  // The minimum zoom 
            mapControl.MaxZoom = 17; // The largest scale 
            mapControl.Zoom = 5;     // The current zoom 
            mapControl.ShowCenter = false; // Do not show center 10 Word point 
            mapControl.DragButton = MouseButton.Left; // Left-click to drag the map 
            mapControl.Position = new PointLatLng(32.064, 118.704); // Map center location: Nanjing 
            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()
        {
        }
    }
}

The renderings are as follows:

Similar to the Winform code, some of the response events are different. WPF's GMap does not have the "layer" concept of GMapOverlay, so it is impossible to add more GMapOverlay. GMapMarker can only be added to mapControl.

WPF GMapMarker can be instantiated directly (not in WinForm), but there is no default seems to provide effect, and to make some effect, the need to design implementation, official Demo have 1 some implementation, the WinForm GMapMarker GMarkerGoogle can be used to instantiate (provided has optional effect, also can be introduced to bitmap as custom ICONS).


Related articles: