Using Gmap.net map Plug-ins in WinForm and WPF simple tutorials-practical tips

Source: Internet
Author: User

How to use gmap.net in WinForm

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

Download gmap.net, I downloaded the version: greatmaps_81b71bf30091, compiled three core projects:

GMap.Net.Core: Core DLL

DLLs used in GMap.Net.WindowsForms:WinForm

DLLs used in GMap.NET.WindowsPresentation:WPF

Use Gmap in WinForm projects:

1, create a new Visual C # Windows window program. Add a reference to GMap.Net.Core.DLL and GMap.Net.WindowsForms.DLL.

2, add a UserControl in the project, here named Mapcontrol, modify this UserControl, so that it inherits from Gmapcontrol, this is the display map control. Modified as follows:

Copy Code code 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, in the Toolbox (tool box) can see this mapcontrol, add this mapcontrol to form.

4, add the relevant code in the main form as follows:

Copy Code code 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 Interactive logic
   ///</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 Map
            mapcontrol.minzoom = 2;  //min Zoom
            mapcontrol.maxzoom = 17;//MAX Zoom
            mapcontrol.zoom = 5;     //Current zoom
            mapcontrol.showcenter = false; Do not show center cross
            Mapcontrol.dragbutton = Mousebutton.left; Left-Drag 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, compile, run the project can see the map, here is using the online Google China map, map control of several key attributes:

Mapprovider: Provider of map services.

Minzoom: Minimum zoom, minimum 1.

Maxzoom: Maximum zoom, maximum is 24.

Zoom: Current zoom.

Showcenter: Whether to display the center point (preferably false, otherwise there will be a red cross in the middle of the map).

Dragbutton: The key to drag the map.

Position: Map Center point location.

The map shows the following, support left-side drag, zoom out, you can show the left click Latitude and longitude.

How to use gmap.net in WPF

1, create a new WPF program for Visual C #. Add a reference 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 for MapControl.cs, the following code:

Copy Code code as follows:

Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using GMap.NET.WindowsPresentation;

Namespace Gmapwpfdemo
{
Class Mapcontrol:gmapcontrol
{
}
}

Only need to inherit Gmapcontrol, the basic functions can be provided by Gmapcontrol.

3. In our MainWindow.xaml, add the namespace:xmlns:src= "Clr-namespace:gmapwpfdemo" of the project to add the use of MapControl.cs to the XML code:

Copy Code code 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= "maxzoom=" "minzoom=" "1"/>
</GroupBox>
</Grid>
</Window>

4, add the relevant code in the MainWindow as follows:

Copy Code code 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>
The interactive 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 Map
            mapcontrol.minzoom = 2;  //min Zoom
            mapcontrol.maxzoom = 17;//MAX Zoom
            mapcontrol.zoom = 5;     //Current zoom
            mapcontrol.showcenter = false; Do not show center cross
            Mapcontrol.dragbutton = Mousebutton.left; Left-Drag 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 effect chart is as follows:

Similar to the WinForm code, some response events are different, WPF Gmap does not gmapoverlay this "layer" concept, so can not add more than Gmapoverlay, Gmapoverlay (can be understood as icon callout) , Gmapmarker can only be directly added to the Mapcontrol.

WPF Gmapmarker can be directly instantiated (not in the WinForm), but there seems to be no default to provide the effect, but to make some effect, need to design their own implementation, the official demo has been some implementation, Gmapmarker in WinForm can be instantiated with gmarkergoogle (provided with an optional effect, or you can pass in bitmap as a custom icon).

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.