The previous article introduced the basic concepts of GMAP. NET and some demos. This chapter mainly introduces how to use GMAP. NET in our code.
1. Download
Http://greatmaps.codeplex.com/releases/view/20235
2. Compile the GMAP. NET project
3. Reference in the project
My project uses WPF, so we need to reference GMAP. NET Core and GMap. NET. WindowsPresentation.
4.
GMapControl
1) create a UserControl in UserControl. xaml and reference it in UserControl.GMapControlI have set MaxZoom and MinZoom, which are also the maximum scaling scales supported by GMAP. NET, as shown below:
<UserControl x:Class="UICommon.View.Map.GMapTrack" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:gmap="clr-namespace:GMap.NET.WindowsPresentation;assembly=GMap.NET.WindowsPresentation" xmlns:Map="clr-namespace:UICommon.View.Map" x:Name="userControl" > <Grid> <GroupBox Name="mapgroup" Margin="0,0,50,0" VerticalContentAlignment="Stretch" HorizontalContentAlignment="Stretch"> <gmap:GMapControl x:Name="MainMap" MaxZoom="24" MinZoom="1"> </gmap:GMapControl> </GroupBox> </Grid></UserControl>
5.
GMapControl event
Set the Event code, including entering the event MouseEnter, right-clicking the event MouseRightButtonDown, and double-clicking the event MouseDoubleClick.
The OnTileLoadStart and OnTileLoadComplete events are as follows: All maps are downloaded and spliced as an image,The image here is called Tile.. So
OnTileLoadStart is the event triggered when each image starts loading. OnTileLoadComplete is the event triggered after each image is loaded. Then it can be displayed at OnTileLoadStart.
Loading or progress bars won't make the user feel dead. OnTileLoadComplete can close the progress bar.
public GMapTrack() { InitializeComponent(); this.MainMap.MouseEnter += MainMap_MouseEnter;
this.MainMap.MouseRightButtonDown += new MouseButtonEventHandler(MainMap_MouseRightButtonDown); this.MainMap.MouseDoubleClick += new MouseButtonEventHandler(MainMap_MouseDoubleClick);
this.MainMap.OnTileLoadStart += MainMap_OnTileLoadStart;
this.MainMap.OnTileLoadComplete += MainMap_OnTileLoadComplete;
this.MainMap.Loaded += new RoutedEventHandler(MainMap_Loaded);
}
6.
GMapControl Loaded Initialization
PositionIt is the center location where the map is started by default. I read it from the configuration file here.
AreaIndicates the region of the entire map. You can leave it empty.
ModeThere are three types: CacheOnly (obtained from the cache only), ServerAndCache (Network + cache), and ServerOnly (read from the network only)
MapProviderIt is the source of the map. The default value is OpenStreetMap. Of course, BingMap, GoogleMap, and support for domestic maps such as Baidu and SoSo will be introduced in the following sections.
DragButtonIndicates whether to use the left mouse or right mouse button to drag a map.
ZoomIs the level of the current Map Display (1 ~ 24)
MinZoomIs the minimum level supported by maps,MaxZoomIs the maximum level supported by a map.
this.MainMap.Position = new PointLatLng(double.Parse(ConfigurationManager.AppSettings["defaultLat"]), double.Parse(ConfigurationManager.AppSettings["defaultLng"])); this.MainMap.MapProvider.Area = new RectLatLng(30.981178, 105.351914, 2.765142, 4.120995); this.MainMap.BoundsOfMap = new RectLatLng(30.981178, 105.351914, 2.765142, 4.120995); this.MainMap.Manager.Mode = AccessMode.CacheOnly; this.MainMap.MapProvider = GMapProviders.OpenStreetMap; this.MainMap.DragButton = MouseButton.Left; this.MainMap.Zoom = 13; this.MainMap.MinZoom = 8; this.MainMap.MaxZoom = 24;