C # embedded Baidu map in the program,
This example briefly introduces how to use Baidu map in WinForm. Baidu map currently supports Android development, IOS development, Web development, and service interfaces. For details, refer to 'Baidu map open platform '.
Knowledge points involved in [dynamic loading of Baidu map:
- WebBrowser control, which is a control provided by VS, allows you to navigate webpages in WinForm. The Navigate function is mainly used to load the documents at the specified unified resource locator (URL) to a new browser window or the System. Windows. Forms. WebBrowser control. For details about this control, refer to the MSDN.
- Baidu map JavaScript API, call API to display Baidu map on the webpage.
As follows:
The Html code for calling Baidu map is as follows:
1 <! DOCTYPE html> 2 View Code
The code for WinForm to call Html is as follows:
1 private void BaiduMap01_Load (object sender, EventArgs e) 2 {3 // Copy the htm file to the program root directory 4 this. wbBaidu. navigate (AppDomain. currentDomain. baseDirectory + "Baidu01.htm", false); 5}View Code
Certificate --------------------------------------------------------------------------------------------------------------------------------------------------
Loading static graphs involves knowledge points
- Call Baidu's static graph interface
- PictureBox VS comes with an image container, indicating the Windows Image box control used to display images.
- HttpWebRequest and HttpWebResponse send/receive http requests in WinForm.
- Thread is called in the background process to prevent the interface from being killed.
- Converts the returned byte stream to an Image object.
As follows:
The code for calling the static Graph API in the WinForm program is as follows:
1 using System; 2 using System. collections. generic; 3 using System. componentModel; 4 using System. data; 5 using System. drawing; 6 using System. linq; 7 using System. text; 8 using System. windows. forms; 9 using System. net; 10 using System. IO; 11 using System. threading; 12 13 namespace DemoSharp14 {15 public partial class BaiduMap02: Form16 {17 public BaiduMap02 () 18 {19 InitializeComponent (); 20} 21 22 private void btnLoad_Click (object sender, eventArgs e) 23 {24 // Thread execution 25 Thread t = new Thread (new ThreadStart (InitMap); 26 t. start (); 27} 28 29 private void InitMap () {30 string url =" http://api.map.baidu.com/staticimage/v2?ak=AKCode Request & mcode = 666666 & center = 116.403874, 39.914888 & width = 910 & height = 400 & zoom = 11 "; 31 HttpWebRequest request = (HttpWebRequest) HttpWebRequest. create (url); 32 request. method = "GET"; 33 HttpWebResponse response = request. getResponse () as HttpWebResponse; 34 while (true) 35 {36 if (response. statusCode = HttpStatusCode. OK) 37 {38 Image img = Image. fromStream (response. getResponseStream (); 39 this. pictureBox1.Image = img; 40 break; 41} 42 Thread. sleep (1000); 43} 44} 45} 46}View Code
Postscript:
When calling Baidu map functions, you must firstApply for an accesskey ),Personal development and learning can be registered using a mobile phone.