精品伊人久久大香线蕉,开心久久婷婷综合中文字幕,杏田冲梨,人妻无码aⅴ不卡中文字幕

打開APP
userphoto
未登錄

開通VIP,暢享免費(fèi)電子書等14項(xiàng)超值服

開通VIP
c#調(diào)用百度地圖API實(shí)現(xiàn)標(biāo)點(diǎn)和連接數(shù)據(jù)庫實(shí)現(xiàn)彈窗

c#調(diào)用百度地圖API實(shí)現(xiàn)標(biāo)點(diǎn)和連接數(shù)據(jù)庫實(shí)現(xiàn)彈窗

  • 最終效果
  • 你需要準(zhǔn)備
  • 代碼部分
    • c#代碼
    • JavaScript部分(HTML)
  • 注意事項(xiàng)(參考以上代碼段食用)
    • 1
    • 2
    • 3

最終效果

你需要準(zhǔn)備

  1. 數(shù)據(jù)庫
  2. 秘鑰(網(wǎng)上教程較多,也可以使用別人的秘鑰)
    http://lbsyun.baidu.com/index.php?title=jspopular

代碼部分

c#代碼

using System;
using System.Windows.Forms;
using System.Data.Odbc;
using System.Configuration;
using System.Security.Permissions;              //為了能與JS交互
using System.IO;

namespace Map
{
    [PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
    [System.Runtime.InteropServices.ComVisibleAttribute(true)]

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //try
            //{
            //    //這個(gè)文件與可執(zhí)行文件放在同一目錄  
            webBrowser1.Url = new Uri(Path.Combine(Application.StartupPath, "marker.html"));
            //}
            //catch (Exception ex)
            //{
            //    MessageBox.Show(ex.Message, "異常", MessageBoxButtons.OK, MessageBoxIcon.Error);
            //}
            string conStr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;//連接數(shù)據(jù)庫的語句,次方法需要在App.config里面配置
            OdbcConnection conn = new OdbcConnection(conStr);
            conn.Open();
            string mysql = "SELECT * FROM Condition_Monitoring where SIM in(select SIM from Basic_Info)ORDER BY Upload_Time DESC LIMIT 3"; //查詢出符合條件的最近的一條數(shù)據(jù)
            OdbcDataAdapter adapter = new OdbcDataAdapter(mysql, conStr);
            System.Data.DataTable dt = new System.Data.DataTable("Condition_Monitoring");
            adapter.Fill(dt);
            conn.Close();
            if (dt.Rows.Count == 0) return;

            //獲取數(shù)據(jù)庫數(shù)據(jù)
            string[] sim = new string[dt.Rows.Count];
            float[] dos = new float[dt.Rows.Count];
            float[] lon = new float[dt.Rows.Count];
            float[] lat = new float[dt.Rows.Count];
            float[] tem = new float[dt.Rows.Count];         //溫度
            float[] hum = new float[dt.Rows.Count];         //濕度
            string[] dum = new string[dt.Rows.Count];       //剩余電量
            string[] pow = new string[dt.Rows.Count];       //供電類型         

            for (int i = 0; i < dt.Rows.Count; i++)                                     //針對(duì)DataTable循環(huán)對(duì)數(shù)組賦值
            {
                sim[i] = dt.Rows[i][0].ToString();
                dos[i] = float.Parse(dt.Rows[i][1].ToString());
                lon[i] = float.Parse(dt.Rows[i][2].ToString());
                lat[i] = float.Parse(dt.Rows[i][3].ToString());
                tem[i] = float.Parse(dt.Rows[i][4].ToString());
                hum[i] = float.Parse(dt.Rows[i][5].ToString());
                dum[i] = dt.Rows[i][6].ToString();
                pow[i] = dt.Rows[i][7].ToString();

                object[] obj = new object[] { sim[i], dos[i], lon[i], lat[i], tem[i], hum[i], dum[i], pow[i] };
                webBrowser1.Document.InvokeScript("setLocation", obj);
            }

        }               
        /// <summary>
        /// 觸發(fā)標(biāo)點(diǎn)以及顯示窗口
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            string conStr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
            OdbcConnection conn = new OdbcConnection(conStr);
            conn.Open();
            string mysql = "SELECT * FROM Condition_Monitoring where SIM in(select SIM from Basic_Info)ORDER BY Upload_Time DESC LIMIT 3"; //查詢出符合條件的最近的一條數(shù)據(jù)
            OdbcDataAdapter adapter = new OdbcDataAdapter(mysql, conStr);
            System.Data.DataTable dt = new System.Data.DataTable("Condition_Monitoring");
            adapter.Fill(dt);
            conn.Close();
            if (dt.Rows.Count == 0) return;

            //獲取數(shù)據(jù)庫數(shù)據(jù)
            string[] sim = new string[dt.Rows.Count];
            float[] dos = new float[dt.Rows.Count];
            float[] lon = new float[dt.Rows.Count];
            float[] lat = new float[dt.Rows.Count];
            float[] tem = new float[dt.Rows.Count];         //溫度
            float[] hum = new float[dt.Rows.Count];         //濕度
            string[] dum = new string[dt.Rows.Count];       //剩余電量
            string[] pow = new string[dt.Rows.Count];       //供電類型         

            for (int i = 0; i < dt.Rows.Count; i++)                                     //針對(duì)DataTable循環(huán)對(duì)數(shù)組賦值
            {
                sim[i] = dt.Rows[i][0].ToString();
                dos[i] = float.Parse(dt.Rows[i][1].ToString());
                lon[i] = float.Parse(dt.Rows[i][2].ToString());
                lat[i] = float.Parse(dt.Rows[i][3].ToString());
                tem[i] = float.Parse(dt.Rows[i][4].ToString());
                hum[i] = float.Parse(dt.Rows[i][5].ToString());
                dum[i] = dt.Rows[i][6].ToString();
                pow[i] = dt.Rows[i][7].ToString();

                object[] obj = new object[] { sim[i], dos[i], lon[i], lat[i], tem[i], hum[i], dum[i], pow[i] };
                webBrowser1.Document.InvokeScript("setLocation", obj);
            }
        }
    }
}

JavaScript部分(HTML)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<title>Hello World</title>
	<style type="text/css">
	//對(duì)網(wǎng)頁以及信息窗口寬度高度等設(shè)置
		body, html, #allmap {
			width: 100%;
			height: 100%;
			overflow: hidden;
			margin: 0;
		}

		#l-map {
			height: 100%;
			width: 78%;
			float: left;
			border-right: 2px solid #bcbcbc;
		}

		#r-result {
			height: 100%;
			width: 20%;
			float: left;
		}
        .info_ul {
            margin: 0 0 5px 0;
            padding: 0.2em 0;
        }

        .info_li {
            line-height: 26px;
            font-size: 15px;
        }

        .info_span {
            width: 80px;
            display: inline-block;
        }

	</style>
	<script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=ttv8Mf0FOSR9iife0kfxS0iIvBwuHzzA"></script>
ak后面這一串是需要自己申請(qǐng)的秘鑰
</head>
<body>
	<div id="allmap">
	</div>
</body>
</html>
	

<script type="text/javascript">
	var map = new BMap.Map("allmap");                    // 創(chuàng)建Map實(shí)例,打開地圖時(shí)默認(rèn)加載的
	var point = new BMap.Point(103.388611,35.563611);    // 創(chuàng)建點(diǎn)坐標(biāo),此為蘭州交通大學(xué)初始坐標(biāo)
	map.centerAndZoom(point, 6);                        // 初始化地圖,設(shè)置中心點(diǎn)坐標(biāo)和地圖大小級(jí)別               
	map.addControl(new BMap.NavigationControl());        // 添加平移縮放控件
	map.addControl(new BMap.ScaleControl());             // 添加比例尺控件
	map.addControl(new BMap.OverviewMapControl());       //添加縮略地圖控件
	map.addControl(new BMap.MapTypeControl());           //添加地圖類型控件
	map.setCurrentCity("湖北");                           //設(shè)置地圖顯示的城市,這個(gè)不知道有啥用
	map.enableScrollWheelZoom(true);                     //開啟鼠標(biāo)滾輪縮放


//這個(gè)setlocation是自己定義的函數(shù),需要顯示哪些數(shù),就在括號(hào)里添加幾個(gè),然后里面當(dāng)做變量使用,和c#里的object數(shù)組一一對(duì)應(yīng)
	function setLocation(sim,dos,lng,lat, tem, hum, dum, pow){	
		map.removeOverlay(marker);
		var point = new BMap.Point(lng,lat);	
		map.centerAndZoom(point, 6);                      //設(shè)置查詢位置結(jié)束顯示時(shí)地圖大小級(jí)別,數(shù)值越大,放大倍數(shù)越大(越詳細(xì))
		var marker=new BMap.Marker(point);
		map.addOverlay(marker);                            //將標(biāo)注添加到地圖中 

		//給標(biāo)注點(diǎn)添加點(diǎn)擊事件。使用立即執(zhí)行函數(shù)和閉包
		(function() {
			var thePoint = point;
			marker.addEventListener("click",function(){
				showInfo(this,thePoint);
			});
		})();
							
		function showInfo(thisMaker,point){			//顯示信息窗口,顯示標(biāo)注點(diǎn)的信息。
			var sContent =
			'<ul class="info_ul">'
			+'<li class="info_li">'
			+'<span class="info_span">SIM卡號(hào):</span>  '+sim+'  </li>'
			+'<li class="info_li">'
			+'<span class="info_span">劑量:</span> '+dos+' </li>'	
			+'<li class="info_li">'
			+'<span class="info_span">溫度:</span>  '+tem+'  </li>'
			+'<li class="info_li">'
			+'<span class="info_span">濕度:</span>  '+hum+'  </li>'
			+'<li class="info_li">'
			+'<span class="info_span">剩余電量:</span>  '+dum+'  </li>'
			+'<li class="info_li">'
			+'<span class="info_span">供電類型:</span>  '+pow+'  </li>'
			+'</ul>';
			var infoWindow = new BMap.InfoWindow(sContent);		// 創(chuàng)建信息窗口對(duì)象
			thisMaker.openInfoWindow(infoWindow);				//圖片加載完畢重繪infowindow
		}
	}

</script>	

注意事項(xiàng)(參考以上代碼段食用)

1

mark.html為JavaScript的代碼文件,此文件放在 bin\Debug文件夾下


關(guān)于這個(gè)文件,可以新建文本文檔,把代碼復(fù)制到里面,另存為,名稱填寫“mark.html”,保存類型為“所有文件”。

2

為了與js交互,需引入
using System.Security.Permissions;

[PermissionSet(SecurityAction.Demand, Name = “FullTrust”)]
[System.Runtime.InteropServices.ComVisibleAttribute(true)]

 webBrowser1.Url = new Uri(Path.Combine(Application.StartupPath, "marker.html"));

中含有path接口,需引入using System.IO;

3

我使用的是用odbc連接數(shù)據(jù)庫,所以引用了using System.Data.Odbc;,也可以直接連接mysql,需要using引用,解決方案管理器里引用, OdbcConnection變?yōu)閙ysqlconnection, OdbcDataAdapter同理

本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
如何用HTML5+CSS3實(shí)現(xiàn)無插件拖拽上傳圖片(支持預(yù)覽與批量)
WordPress+Markdown+為知筆記,實(shí)現(xiàn)高質(zhì)量筆記和博客
jQuery個(gè)人名片樣式代碼
輕輕松松學(xué)CSS:position
表單驗(yàn)證(登錄/注冊(cè))
HTML5樣式控制示例代碼
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服

主站蜘蛛池模板: 金昌市| 福安市| 通化市| 樟树市| 湘阴县| 黄陵县| 兴业县| 察哈| 芜湖县| 宁明县| 东乌珠穆沁旗| 山阳县| 内黄县| 永仁县| 通渭县| 大同县| 莱阳市| 涪陵区| 甘谷县| 报价| 郯城县| 贵港市| 枣强县| 彰武县| 漳浦县| 新野县| 溧水县| 沛县| 闽侯县| 信丰县| 上饶市| 渝北区| 宜宾市| 洛隆县| 瑞丽市| 罗平县| 名山县| 德安县| 霞浦县| 故城县| 泸水县|