Gmap.net Learning (i) adding marker and polygon

Source: Internet
Author: User
Tags visual studio 2010

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, Yandex , mapy.cz, maps.lt, ikarte.lv, Nearmap, Ovimap, Cloudmade, Wikimapia, MapQuest in Windows Forms & Presentation, Suppor TS caching and runs on Windows mobile!

As mentioned above, Gmap.net is a. NET control, open source, cross-platform, and free. The map is widely sourced. Can be used for WinForm, WPF, WM. Download: DLL files for gmap.net controls For WinFormCreate a C # Windows Forms project in Visual Studio 2010 to download the good GMap.NET.Core.dllAnd GMap.NET.WindowsForms.dllThe files are placed in the Project sub-folder. Add a reference (reference) to this two file.


In this way, your own code can access the classes in the gmap.net. Because this gmap.net is a user control, it can be added to the toolbox, which makes it easier to set the properties of the control in the Properties window. Of course, you can instantiate the control directly in your code without adding it to the toolbox, but you won't be able to set properties using the Properties window.

Add the control to the Toolbox, right-click on the Toolbox, select "Select items (Choose items)" To select " Browse ...option, and then select the file GMap.NET.WindowsForms DLLTo find the required toolset, which must include GmapcontrolControl. Tick this control and click OK. In this way, the control appears in the toolbox and can be dragged directly onto the form.

To use this control, you must understand the following points:

1. What is the map control (Gmapcontrol)? This is the control which renders the map.

2. What's an Overlay (gmapoverlay)? This was a layer on top of the map control. You can have several layers on top of a maps, each layer representing, say, a route with stops, a list of stores etc.

3. What is markers (gmapmarker)? These is the points on a layer, each representing a specific geo location (Lat,lon) e.g. each drop point on a route.

4. What is a route (gmaproute)? This is the path or direction between, or more points.

5. WTF is tiles? –well Here's something to read ... Bing Maps Tile System.

1. Add a Gmap control to Windows Form

Drag the control directly from the Toolbox onto the form, resize it, and change the default name GMapControl1 to Gmap

Select the control, select the property, open the Properties panel, in addition to the general properties of the control, there are gmap specific properties,

Candragmap-----The right mouse button to drag the map

Markersenabled---show markers

polygonsenabled---show polygon

Showtilegridlines---display grid of coordinates

Zoom level of Zoom, minzoom, Maxzoom---Google Maps from 0-18,0 is global, 18 is street level, national level, zoom is 5 more appropriate.

2. Initialize the map

Add an onload event to the form and write the following code:

Now you can see the map by running it. If you cannot right-drag the map and wheel zoom, check that the control property is set correctly.

There are four ways to set the default location of the map:

Gmap. Setcurrentpositionbykeywords ("Country")-USA

Gmap. Setcurrentpositionbykeywords ("State, Country") –berlin, Germany

Gmap. Setcurrentpositionbykeywords ("Province, Country") –alberta, Canada

Gmap. Position = new POINTLATLNG ( -25.971684,32.589759);
3. Map provider Maps Providers gmap.net Library the magic of it is not just to use Google maps, as well as other maps available, Gmap can invoke a large number of map sources, and the interfaces are encapsulated inside. Here is the map provider:

Cloudmademapprovider

Googlemapprovider–map provider for Google maps; There are many kinds of maps

Opencyclemapprovider

Openstreetmapprovider

Wikimapiamapprovider

Yahoomapprovider

Any marker and polygon code-write for gmap.net would work with any of the providers. awesome! 4. Add Icon Adding markers markers added to the layer above the map, called overlays, can be in a Overlay Add any number of markers, and then overlay Add to add to the map. overlay can be hidden or displayed as needed. The following code adds a markers named overlay on the map, this there is only one marker in overlay. Gmapoverlay markersoverlay = new Gmapoverlay ("markers"); New One Overlays ObjectGmarkergoogle marker = new Gmarkergoogle (new Pointlatlng ( -25.966688, 32.580528), Gmarkergoogletype.green); MARKERSOVERLAY.MARKERS.ADD (marker); Gmap. Overlays.add (Markersoverlay);
First, create a the overlay instance, when new is named, is an optional parameter. You can refer to this in other places by name . overlay, you can also refer directly to this overlay the instance itself. then, create a Gmarkergoogle instance, new this instance, requires 2 parameters, the first is the position (also POINTLATLNG Example ), one is the marker type. The marker type is a set of icon images that can be found in the Google Maps API. You can also provide yourself with a Bitmap instance as a marker picture.
Gmarkergoogle marker = new Gmarkergoogle (new Pointlatlng ( -25.966688, 32.580528), Newbitmap ("C:\\images\\mymarker.png" );
Finally, add the marker to the overlay and add the overlay to the map. These marker can be compatible with any map provider. 
5. Add Zone Adding polygons
The following code creates a overlay and adds a quad
Gmapoverlay polyoverlay = new Gmapoverlay ("polygons");
List<pointlatlng> points = new list<pointlatlng> ();
Points. ADD (New POINTLATLNG ( -25.969562,32.585789));
Points. ADD (New POINTLATLNG ( -25.966205,32.588171));
Points. ADD (New POINTLATLNG ( -25.968134,32.591647));
Points. ADD (New POINTLATLNG ( -25.971684,32.589759));
Gmappolygon polygon = new Gmappolygon (points, "MyPolygon");
Polygon. Fill = new SolidBrush (Color.FromArgb (color.red));
Polygon. Stroke = new Pen (color.red, 1);
POLYOVERLAY.POLYGONS.ADD (Polygon);
Gmap. Overlays.add (Polyoverlay);
first create a new overlay, and then create a list of polygon vertices, using this list to create a Gmappolygon instance, and then use the SolidBrush tells this polygon how to draw himself, the color of the pen selected for the stroke, finally, add polygon to overlay, and then add overlay to the map. 
Conclusion:
Tip
If marker is not displayed, first look in the properties panel below markersenabled is set to True
If you are not connected, you can also use offline maps to add the following code:
GMap.NET.GMaps.Instance.Mode = GMap.NET.AccessMode.CacheOnly;
To add a reference namespace to the code:
Using Gmap.net;
Using GMap.NET.WindowsForms;
Using GMap.NET.WindowsForms.Markers;
Using GMap.NET.WindowsForms.ToolTips;


Transfer from http://blog.sina.com.cn/s/blog_819100560101dgng.html

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.