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);
}
}
}
}
<!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>
mark.html為JavaScript的代碼文件,此文件放在 bin\Debug文件夾下
為了與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;
我使用的是用odbc連接數(shù)據(jù)庫,所以引用了using System.Data.Odbc;,也可以直接連接mysql,需要using引用,解決方案管理器里引用, OdbcConnection變?yōu)閙ysqlconnection, OdbcDataAdapter同理
聯(lián)系客服