Simple tutorial on using GMap. Net map plug-in WinForm and WPF

Source: Internet
Author: User

How to Use GMap. Net in WinForm

Project home: https://greatmaps.codeplex.com/

Download GMap. Net. The downloaded version is greatmaps_81b71bf30091. Compile three core projects:

GMap. Net. Core: Core DLL

GMap. Net. WindowsForms: DLL used in WinForm

GMap. NET. WindowsPresentation: DLL used in WPF

Use GMap in the WinForm project:

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

2. Add a UserControl in the project, named MapControl. modify this UserControl to inherit from GMapControl. This is the control for displaying maps. Modify as follows:

Copy codeThe Code 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. In our Form design window, you can see this MapControl in the toolbox and add this MapControl to the Form.

4. Add the following code to the main Form:

Copy codeThe Code is 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>
/// Interaction logic of 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 Map
MapControl. MinZoom = 2; // min zoom
MapControl. MaxZoom = 17; // maximum zoom
MapControl. Zoom = 5; // current Scaling
MapControl. ShowCenter = false; // The center cross is not displayed.
MapControl. DragButton = MouseButton. Left; // drag the map with the Left button.
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. Compile and run the project to see the map. Here we use the online Google China map. The map controls have several main attributes:

MapProvider: the provider of the map service.

MinZoom: min zoom. Min Zoom can be 1.

MaxZoom: maximum zoom, up to 24.

Zoom: Current Zoom.

ShowCenter: Indicates whether to display the center point (preferably false, otherwise there will be a Red Cross in the middle of the map ).

DragButton: The key used to drag the map.

Position: the central point of the map.

The map is displayed as follows. You can drag it with the left button to zoom in and out. You can click the longitude and latitude with the left button.

How to Use GMap. Net in WPF

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

2. Because the UserControl of WPF cannot modify the inherited base class, add a new class called MapControl. cs. The Code is as follows:

Copy codeThe 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 only need to inherit GMapControl. The basic functions can be provided by GMapControl.

3. In our MainWindow. xaml, add the project namespace: xmlns: src = "clr-namespace: GMapWPFDemo" and add the usage of MapControl. cs in the XML code:

Copy codeThe Code is as follows:
<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 the following code to MainWindow:

Copy codeThe Code is 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>
/// Interaction logic of 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 Map
MapControl. MinZoom = 2; // min zoom
MapControl. MaxZoom = 17; // maximum zoom
MapControl. Zoom = 5; // current Scaling
MapControl. ShowCenter = false; // The center cross is not displayed.
MapControl. DragButton = MouseButton. Left; // drag the map with the Left button.
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 ()
{
}
}
}

As follows:

Similar to Winform Code, some response events are different. The GMap in WPF does not have the "layer" concept of GMapOverlay, so there is no way to add multiple GMapOverlay, add GMapMarker to GMapOverlay (which can be understood as an icon label). GMapMarker can only be directly added to mapControl.

The GMapMarker of WPF can be directly instantiated (not in WinForm), but it seems that there is no effect provided by default. To make some results, you need to design and implement it yourself. Some implementations are already available in the official Demo, GMapMarker in WinForm can be instantiated using GMarkerGoogle (optional effects can be provided, or bitmap can be imported as a custom icon ).

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.