GMAP. Net searches by address and double-click the address to obtain the latest version of the latitude and longitude (winform)

Source: Internet
Author: User

First download the required plug-in: http://greatmaps.codeplex.com /. And reference the DLL plug-in.

In our coding, we must understand how map is made up, there are three layers of composition, the picture is as follows (cut Address: http://www.flickr.com/photos/33793929@N07/6008096410 ):

Top layer: gmapmarker. Middle Layer: gmapoverlay. Bottom layer: gmapcontrol. Gmapmarker is the water drop mark of the selected address. When we want to add a water drop mark, the mark is first added to gmapmarker, then gmapmarker is added to gmapoverlay, and finally gmapoverlay is added to gmapmarker, which will hit the water drop effect in the image below. If the drop mark is removed, it is removed in reverse order. The following is the code.

The function code is as follows:

Using system; using system. collections. generic; using system. componentmodel; using system. data; using system. drawing; using system. LINQ; using system. text; using system. windows. forms; using GMAP. net; using GMAP. net. windowsforms. markers; using GMAP. net. windowsforms; using GMAP. net. mapproviders; using GMAP. net. projections; using windowsformsapplication1.properties; using system. windows; namespace windowsformsapplic Ation1 {public partial class frmmap: FORM {// The map consists of three layers. Top layer: gmapmarker. Middle Layer: gmapoverlay. Bottom layer: gmapcontrol. Gmapoverlay objects;
Gmapcontrol mainmap; Public frmmap () {initializecomponent (); // obtain map information. The default location is Beijing. Suspendlayout (); mainmap = new gmapcontrol (); {// Google map of China. Mainmap. mapprovider = gmapproviders. googlechinamap; mainmap. position = new pointlatlng (39.904214, 116.407113); mainmap. minzoom = 1; mainmap. maxzoom = 19; mainmap. zoom = 10; mainmap. dock = dockstyle. fill; mainmap. markersenabled = true; this. label2.text = This. label1.text = ""; this. label1.text = "longitude:" + mainmap. position. LNG. tostring (); this. label2.text = "latitude:" + mainmap. position. lat. tostring ();}/ /This. mainmap. reloadmap (); // reload the map. Gmapprovider. Language = languagetype. chinesesimplified; // display the upper-level icon. Cursor. current = cursors. waitcursor; var current = new pointlatlng (mainmap. position. lat, mainmap. position. LNG); var currentmark = new gmapmarkergooglegreen (current); var overlay = new gmapoverlay (mainmap, "current"); overlay. markers. add (currentmark); mainmap. overlays. add (overlay); cursor. current = cursors. default; // double-click the user to mark the selected vertex. And obtain the current longitude and latitude. {Mainmap. mousedoubleclick + = new mouseeventhandler (mainmap_mousedoubleclick);} gmapcontrol1.controls. add (mainmap); resumelayout (true); objects = new gmapoverlay (this. gmapcontrol1, "objects"); this. gmapcontrol1.overlays. add (objects); gmapmarkerrect rect = new gmapmarkerrect (mainmap. position); rect. size = new system. drawing. size (0, 0); rect. tooltiptext = "Beijing"; rect. tooltipmode = markertooltipmode. Always; overlay. markers. Add (rect); mainmap. dragbutton = mousebuttons. Left; txtaddress. Focus () ;}// select the map location to obtain the longitude and latitude. Void mainmap_mousedoubleclick (Object sender, mouseeventargs e) {If (E. Button = mousebuttons. Left) {// the icon is displayed. Cursor. current = cursors. waitcursor; pointlatlng latlng = mainmap. fromlocaltolatlng (E. x, E. y); var current = new pointlatlng (math. ABS (latlng. LAT), latlng. LNG); var currentmark = new gmapmarkergooglegreen (current); var overlay = new gmapoverlay (mainmap, "current"); mainmap. markersenabled = false; overlay. markers. clear (); mainmap. overlays. clear (); overlay. markers. add (currentmark); mainmap. overlays. A Dd (overlay); mainmap. markersenabled = true; cursor. current = cursors. hand; this. label2.text = This. label1.text = ""; this. label1.text = "longitude:" + latlng. LNG. tostring (); this. label2.text = "latitude:" + latlng. lat. tostring () ;}// query a map by city. Private void btnsearch_click (Object sender, eventargs e) {If (this.txt address. text. length = 0) {this.txt address. focus (); MessageBox. show ("Enter the address to query"); return;} This. mainmap. reloadmap (); gmapprovider. language = languagetype. chinesesimplified; string search = string. format ("{0}", this.txt address. text); // determines whether the address key exists. Geocoderstatuscode code = mainmap. setcurrentpositionbykeywords (Search); If (code! = Geocoderstatuscode. g_geo_success) {MessageBox. show ("address not found: '" + this.txt address. text + "', reason:" + code. tostring (), "GMAP. net ", messageboxbuttons. OK, messageboxicon. exclamation); return;} objects = new gmapoverlay (mainmap, "objects"); objects. markers. clear (); this. mainmap. overlays. clear (); addlocation (txtaddress. text);} // obtain the latitude and longitude from the address, and then display the map position. Private void addlocation (string place) {geocoderstatuscode unknow = geocoderstatuscode. unknow; pointlatlng? Latlngfromgeocoder = GMAP. net. mapproviders. gmapproviders. googlechinamap. getpoint (place, out unknow); If (latlngfromgeocoder. hasvalue & (unknow = geocoderstatuscode. g_geo_success) {gmapmarker item = new gmapmarkergooglegreen (latlngfromgeocoder. value); gmapmarkerrect rect = new gmapmarkerrect (latlngfromgeocoder. value); rect. size = new system. drawing. size (100,100); rect. tooltiptext = place; rect. tooltipmode = markertooltipmode. always; objects = new gmapoverlay (mainmap, "objects"); objects. markers. add (item); objects. markers. add (rect); mainmap. overlays. add (objects); this. label2.text = This. label1.text = ""; this. label1.text = "longitude:" + latlngfromgeocoder. value. LNG. tostring (); this. label2.text = "latitude:" + latlngfromgeocoder. value. lat. tostring (); If (txtaddress. text. length <4) {This. mainmap. zoom = 10;} else {This. mainmap. zoom + = 3 ;}}}}}

 

The interface is as follows:

 

 

After loading and running, the effect is as follows:

 

 

The addlocation Method for searching has a class gmapmarkerrect which needs to be re-created. The code in the class is as follows:

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace WindowsFormsApplication1{    using System.Drawing;    using GMap.NET.WindowsForms;    using GMap.NET.WindowsForms.Markers;    using GMap.NET;    using System;    using System.Runtime.Serialization;    using System.Drawing.Drawing2D;    [Serializable]    public class GMapMarkerRect : GMapMarker, ISerializable    {        [NonSerialized]        public Pen Pen;        //[NonSerialized]        //public GMarkerGoogle InnerMarker;        public GMapMarkerRect(PointLatLng p): base(p)        {            Pen = new Pen(Brushes.Blue, 5);            // do not forget set Size of the marker            // if so, you shall have no event on it ;}            Size = new System.Drawing.Size(111, 111);            Offset = new System.Drawing.Point(-Size.Width / 2, -Size.Height / 2);        }        public override void OnRender(Graphics g)        {            g.DrawRectangle(Pen, new System.Drawing.Rectangle(LocalPosition.X, LocalPosition.Y, Size.Width, Size.Height));        }    }}

The search results by address are as follows:

 

Reference: http://www.cnblogs.com/wu?cong/archive/2010/03/10/1681790.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.