C#winform使用WebBrowser控件調用百度地圖

一、申請百度地圖密鑰

百度官方文檔:http://developer.baidu.com/map/jsmobile.htm
百度申請密鑰:http://lbsyun.baidu.com/apiconsole/key
如圖所示創建應用,申請密鑰如下圖·AK所示

二、新建winform項目

  1. 新建一個winform項目 ,打開VS2015,文件–>新建–>項目–>windows窗體應用程序;

2.設計界面
在Form1中添加label,textBox,button,webBrowser控件。WebBrowser 類使用戶可以在窗體中導航網頁。

3.winform下添加HTML頁面
右鍵 添加–>新建項–>HTML頁


現在項目下有一個窗體Form1和一個HTML頁,在HTMLPage1中添加代碼,將您的密鑰修改爲一開始在百度地圖申請的密鑰AK。
HTMLPage1.html頁代碼

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
    <style type="text/css">
        body, html, #allmap {
            width: 100%;
            height: 100%;
            overflow: hidden;
            margin: 0;
            font-family: "微軟雅黑";
        }
    </style>
    <script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=您的密鑰"></script>
    //<script type="text/javascript" src="//api.map.baidu.com/api?v=2.0&ak=您的密鑰"></script>
    <title>地圖展示</title>
</head>
<body>
    <div id="allmap"></div>
</body>
</html>
<script type="text/javascript">
	// 百度地圖API功能
	var map = new BMap.Map("allmap");    // 創建Map實例
	map.centerAndZoom(new BMap.Point(116.404, 39.915), 11);  // 初始化地圖,設置中心點座標和地圖級別
	//添加地圖類型控件
	map.addControl(new BMap.MapTypeControl({
		mapTypes:[
            BMAP_NORMAL_MAP,
            BMAP_HYBRID_MAP
        ]}));
	map.setCurrentCity("北京");          // 設置地圖顯示的城市 此項是必須設置的
	map.enableScrollWheelZoom(true);     //開啓鼠標滾輪縮放
</script>

http://lbsyun.baidu.com/jsdemo.htm#a1_2
百度官方文檔給了很多Demo,可根據先不要進行選擇

4.Form1.cs文件代碼

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;
using System.Security.Permissions;
using System.IO;

namespace WindowsFormsApplication1
{
    [PermissionSet(SecurityAction.Demand, Name = "FullTrust")]//調用JS代碼必要
    [System.Runtime.InteropServices.ComVisibleAttribute(true)]
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                string str_url = Application.StartupPath + "../HTMLPage1.html";// 添加自己添加的html文件名,注意使用相對路徑的方法 HTMLPage1.html要複製到debug目錄下
                Uri url = new Uri(str_url);
                webBrowser1.Url = url;
                webBrowser1.ObjectForScripting = this;

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "異常", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //本地文件 MapWinForms\bin\Debug  
            string url = Application.StartupPath + "\\HTMLPage1.html";
            //textBox1.Text = url;
            url = textBox1.Text.ToString();
            //string file = "file:///E:\\WinFormBaiduMap\\a1_1.html";

            //屏蔽js相關錯誤  
            webBrowser1.ScriptErrorsSuppressed = true;

            //導航顯示本地HTML文件  
            webBrowser1.Navigate(url);
        }
    }
}

三、運行結果圖如下:

效果圖

發佈了11 篇原創文章 · 獲贊 1 · 訪問量 1693
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章