C # call Baidu Map API Getting Started & solving bmap undefined issues

Source: Internet
Author: User
Tags polyline

This article is mainly a recent Baidu map API to help friends study JavaScript, and display it to the C # WinForm interface. Also encountered BMap undefined error (BMAP is not defined) and workaround. Previously written based on the Android Baidu map, in fact, the principle is similar, I hope the article is helpful to you!

A. C # display Baidu map

The use of C # WinForm display Baidu Map mainly includes two steps:
1. Call the Baidu Map API and access the JavaScript version map via the application key AK;
2. In C # webbrowser the browser Control to access the JS file, display the map to the window.

Baidu Official Document: Http://developer.baidu.com/map/jsmobile.htm
Baidu Application key: Http://lbsyun.baidu.com/apiconsole/key

First, the application key is as follows:



The key request succeeds, as shown by AK, which displays the JavaScript Baidu map. It includes applications such as Android, browser-side, server, and so on.


Baidu Map Hello World:http://developer.baidu.com/map/jsdemo-mobile.htm#a1_1
The detailed code is as follows, you need to fill in your key, a new local a1_1.html can be created.

<! DOCTYPE html> open a1_1.html via Chrome browser as shown in: 


then open VS2012 new WinForm project, the main interface is through WebBrowser. As shown in the following:


WebBrowser is a. NET control class that is new in the. NET Framework version 2.0. The WebBrowser class enables users to navigate a Web page in a form.
One of the most common webbrowser1.navigate (TextBox1.Text.Trim ()), you can access the URL types of websites.
This is also the control to display the local JavaScript Web page. The core 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.threading.tasks;using system.windows.forms;//newly added namespaces using  System.net;using system.io;using System.Security.Permissions; Interactive Jsnamespace mapwinforms{[PermissionSet (SecurityAction.Demand, Name = "FullTrust")] [System.Runtime.InteropServi Ces. ComVisibleAttribute (true)] public partial class Form1:form {public Form1 () {Initializec        Omponent ();            private void Button1_Click (object sender, EventArgs e) {//local file Mapwinforms\bin\debug            String url = application.startuppath + "\\a1_1.html";            TextBox1.Text = URL;            string file = "file:///e:\\winformbaidumap\\a1_1.html";             Block JS related error webbrowser1.scripterrorssuppressed = true;        Navigation displays the local HTML file webbrowser1.navigate (URL); }    }}
the results of the operation are as follows:


The next article will detail JavaScript some of the common functions of Baidu map application.
Here are some good blogs to recommend:
C # call Baidu Map API experience Sharing (IV) Look forward to autumn leaves
WinForm map based on Baidu Map API Qin Yuanpei
C # Baidu Map control bmap.net Qin Yuanpei
C # form calls Baidu Map API tips and FAQs
Baidu Map JS Summary java_xiaobin
Http://www.cnblogs.com/ljmin/archive/2012/07/27/2612413.html


two. Resolve Bmap undefined issues

When you use the C # WebBrowser control to display Baidu maps, you usually get an error bmap undefined.
Error: Bmap.map ("container") BMap undefined BMap is not defined



Unresolved method:
See a lot of information on the Internet, but have not solved. Including:
1. The key to verify the JS must be placed in the head tag;
2.c# Ignore JS error: webbrowser1.scripterrorssuppressed = true;
3. Add the following code to the head of the HTML file:
<script type= "Text/javascript" src= "http://api.map.baidu.com/library/DistanceTool/1.2/src/DistanceTool_min.js "></script><script type=" Text/javascript "src=" http://api.map.baidu.com/library/DrawingManager/1.4/ Src/drawingmanager_min.js "></script><script type=" Text/javascript "src=" http://api.map.baidu.com/ Library/searchinfowindow/1.4/src/searchinfowindow_min.js "></script>
Official solution (no effect):
http://bbs.lbsyun.baidu.com/forum.php?mod=viewthread&tid=4530
http://bbs.lbsyun.baidu.com/forum.php?mod=viewthread&tid=3062
http://bbs.lbsyun.baidu.com/forum.php?mod=viewthread&tid=88488
http://bbs.lbsyun.baidu.com/forum.php?mod=viewthread&page=1&tid=88543
http://blog.csdn.net/zhujianli1314/article/details/45696259
https://segmentfault.com/q/1010000004531211
HTTPS call Baidu Map API tip: BMAP not defined

later, because I use the WebBrowser control to show that http://www.baidu.com is not accessible because it is a software developed by Microsoft, I suspect that the control is called by IE browser display.
Fix the following: Tool = "internet option =" Advanced = "reset =" Restart the computer. You can also set the Enable load script, mix, and so on in security.


Before repairing it really my own IE browser can not be used, after the repair of IE browser,C #The program does have access to the Web page through WebBrowser. I think this is the reason, and then in the loading JS still error: Bmap undefined.

Final Solution:
This method is only guaranteed to be correctly invoked, but I am not sure why the key is set incorrectly.
Modify the code that invokes the key in the head to not require a key:
<script type= "Text/javascript"
src= "Http://api.map.baidu.com/api?key=&amp;v=1.1&services=true" >
</script>

Code to replace:
<script type= "Text/javascript"
Src= "http://api.map.baidu.com/api?type=quick&ak= your key &v=1.0" >
</script>
or use the following code to display normally in C #: (AK is my app)
<script type= "Text/javascript"
src= "Http://api.map.baidu.com/api?v=1.0&ak=ui7X2CSu3ITjojARL4tILi5W1i9gkDVS" >
</script>



three. Back up a section of JS Baidu Map codeHere is a code backup:
<! DOCTYPE html>the results of the operation are as follows:


Reference:
http://blog.csdn.net/smartsmile2012/article/details/47754163
Http://www.sufeinet.com/thread-7897-1-1.html

PS: Finally hope that the article is helpful to you! This article is mainly about some online notes and introductory, very basic content, detailed knowledge reference Baidu Map official documents. (By:eastmount late 2016-05-15 2:30 http://blog.csdn.net/eastmount/)

C # call Baidu Map API Getting Started & resolving bmap undefined issues

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.