使用C#調(diào)用百度地圖并實(shí)現(xiàn)坐標(biāo)點(diǎn)的設(shè)置以及讀取示例
申請(qǐng)百度地圖密鑰以及查看百度API
網(wǎng)址:http://lbsyun.baidu.com/apiconsole/key#/home
網(wǎng)址:http://lbsyun.baidu.com/jsdemo.htm#c1_3
程序?qū)崿F(xiàn)功能:
1、輸入網(wǎng)址那可以調(diào)用本地的html文件,也可以訪問(wèn)其他網(wǎng)站
2、輸入坐標(biāo)、添加坐標(biāo)按鈕,可以將坐標(biāo)值傳入html文件中,顯示在經(jīng)緯度的文本框中
3、定位按鈕可以將地圖重新定位,定位中心是文本框內(nèi)的經(jīng)緯度
4、添加標(biāo)注點(diǎn)是將文本框內(nèi)的經(jīng)緯度添加坐標(biāo)到地圖
5、刪除標(biāo)注按鈕可以刪除全部標(biāo)注點(diǎn)
6、鼠標(biāo)點(diǎn)擊地圖,可以在文本框內(nèi)顯示點(diǎn)擊的坐標(biāo)經(jīng)緯度
7、點(diǎn)擊開(kāi)始實(shí)時(shí)顯示按鈕,鼠標(biāo)在地圖上移動(dòng),可以獲得實(shí)時(shí)經(jīng)緯度
最終圖
利用webBrowser控件展示地圖
VS創(chuàng)建工程,添加控件webBrowser,新建.html文件,.html文件參考百度API,將其寫(xiě)入文件
為了能與JS交互,首先引入using System.Security.Permissions;,然后在namespace下必須加入兩行:
[PermissionSet(SecurityAction.Demand, Name = "FullTrust")] [System.Runtime.InteropServices.ComVisibleAttribute(true)]
給窗體一個(gè)Load事件、、、這個(gè)是功能的主要點(diǎn)
然后窗體運(yùn)行的代碼:
private void Form1_Load(object sender, EventArgs e) { try { //string str_url = Application.StartupPath + "../HTMLPage1.html";// 添加自己添加的html文件名,注意使用相對(duì)路徑的方法 HTMLPage1.html要復(fù)制到debug目錄下 string str_url = "C:/Users/12606/Desktop/C#/map/map/HTMLPage1.html";// 添加自己添加的html文件名,注意使用相對(duì)路徑的方法 HTMLPage1.html要復(fù)制到debug目錄下 Uri url = new Uri(str_url); webBrowser1.Url = url; // WebBrowser控件顯示的網(wǎng)頁(yè)路徑 webBrowser1.ObjectForScripting = this; // 將當(dāng)前類設(shè)置為可由腳本訪問(wèn) textBox1.Text = str_url; } catch (Exception ex) { MessageBox.Show(ex.Message, "異常", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
.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{ width: 100%; height: 100%; overflow: hidden; margin: 0; font-family: "微軟雅黑"; } #allmap { height: 97%; width: 100%; } #r-result { width: 100%; font-size: 14px; } </style> <script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=你的密鑰"></script> <title>地圖展示</title> </head> <body> <div id="r-result"> <!--文字和文本框---> 經(jīng)度: <input id="longitude" type="text" style="width:100px; margin-right:10px;" /> 緯度: <input id="latitude" type="text" style="width:100px; margin-right:10px;" /> <!--按鈕---> <input type="button" value="定位" onclick="theLocation()" /> <input type="button" value="添加標(biāo)注" onclick="addPoint()" /> <input type="button" value="刪除標(biāo)注" onclick="deletePoint()" /> </div> <div id="allmap"></div> <b id="mouselng">0</b> <b id="mouselat">0</b> </body> </html> <script type="text/javascript"> // 百度地圖API功能 var map = new BMap.Map("allmap"); // 創(chuàng)建Map實(shí)例 var point = new BMap.Point(120.371, 30.327); // 創(chuàng)建點(diǎn)坐標(biāo) map.centerAndZoom(point, 17); // 初始化地圖,設(shè)置中心點(diǎn)坐標(biāo)和地圖級(jí)別 //向地圖添加標(biāo)注 var marker = new BMap.Marker(point); // 創(chuàng)建標(biāo)注 map.addOverlay(marker); // 將標(biāo)注添加到地圖中 // 添加帶有定位的導(dǎo)航控件 var navigationControl = new BMap.NavigationControl({ // 靠左上角位置 anchor: BMAP_ANCHOR_TOP_LEFT, // LARGE類型 type: BMAP_NAVIGATION_CONTROL_LARGE, // 啟用顯示定位 enableGeolocation: true }); map.addControl(navigationControl); //添加地圖單擊顯示GPS事件 function showInfo(e) { //alert(e.point.lng + ", " + e.point.lat);//窗口顯示點(diǎn)擊位置的GPS document.getElementById("longitude").innerText = e.point.lng; document.getElementById("latitude").innerText = e.point.lat; document.getElementById("mouselng").innerHTML = e.point.lng; document.getElementById("mouselat").innerHTML = e.point.lat; } map.addEventListener("click", showInfo); //監(jiān)聽(tīng)事件 //添加地圖類型控件 map.addControl(new BMap.MapTypeControl({ mapTypes:[ BMAP_NORMAL_MAP, BMAP_HYBRID_MAP ] })); var opts = { offset: new BMap.Size(100, 20) } map.addControl(new BMap.ScaleControl(opts));//比例尺控件 //map.addControl(new BMap.ScaleControl()); //比例尺控件 map.setCurrentCity("杭州"); // 僅當(dāng)設(shè)置城市信息時(shí),MapTypeControl的切換功能才能可用 map.enableScrollWheelZoom(true); //開(kāi)啟鼠標(biāo)滾輪縮放 // 用經(jīng)緯度設(shè)置地圖中心點(diǎn) function theLocation() { if (document.getElementById("longitude").value != "" && document.getElementById("latitude").value != "") { map.clearOverlays(); var new_point = new BMap.Point(document.getElementById("longitude").value, document.getElementById("latitude").value); var marker = new BMap.Marker(new_point); // 創(chuàng)建標(biāo)注 map.addOverlay(marker); // 將標(biāo)注添加到地圖中 map.panTo(new_point); //用經(jīng)緯度設(shè)置地圖中心點(diǎn) } } // 添加標(biāo)注 function addPoint() { if (document.getElementById("longitude").value != "" && document.getElementById("latitude").value != "") { var new_point = new BMap.Point(document.getElementById("longitude").value, document.getElementById("latitude").value); var marker = new BMap.Marker(new_point); // 創(chuàng)建標(biāo)注 map.addOverlay(marker); // 將標(biāo)注添加到地圖中 } } // 刪除所有標(biāo)注 function deletePoint() { //獲取地圖上所有的覆蓋物,并刪除 //map.clearOverlays(); //獲取地圖上所有的覆蓋物,并刪除 var allOverlay = map.getOverlays(); for (var i = 0; i < allOverlay.length; i++) { if (allOverlay[i].toString() == "[object Marker]") { map.removeOverlay(allOverlay[i]); } } ////刪除指定經(jīng)緯度的標(biāo)注 //if (document.getElementById("longitude").value != "" && document.getElementById("latitude").value != "") { // var new_point = new BMap.Point(document.getElementById("longitude").value, document.getElementById("latitude").value); // var marker = new BMap.Marker(new_point); // 創(chuàng)建標(biāo)注 // map.removeOverlay(marker); //} } map.addEventListener("mousemove", GetlngAndlat); function GetlngAndlat(e) { if (e.point.lng != null) { document.getElementById("mouselng").innerHTML = e.point.lng; document.getElementById("mouselat").innerHTML = e.point.lat; } } </script>
http://lbsyun.baidu.com/jsdemo.htm#c1_3
百度官方文檔給了很多Demo,可根據(jù)需求來(lái)寫(xiě)
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; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Security.Permissions; using System.IO; namespace map { // 而為了能與JS交互,首先引入using System.Security.Permissions;,然后在namespace下必須加入兩行: [PermissionSet(SecurityAction.Demand, Name = "FullTrust")]//調(diào)用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文件名,注意使用相對(duì)路徑的方法 HTMLPage1.html要復(fù)制到debug目錄下 string str_url = "C:/Users/12606/Desktop/C#/map/map/HTMLPage1.html";// 添加自己添加的html文件名,注意使用相對(duì)路徑的方法 HTMLPage1.html要復(fù)制到debug目錄下 Uri url = new Uri(str_url); webBrowser1.Url = url; // WebBrowser控件顯示的網(wǎng)頁(yè)路徑 webBrowser1.ObjectForScripting = this; // 將當(dāng)前類設(shè)置為可由腳本訪問(wèn) textBox1.Text = str_url; } 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"; //string url = "C:/Users/12606/Desktop/C#/map/map/HTMLPage1.html"; //textBox1.Text = url; string url = textBox1.Text.ToString(); //屏蔽js相關(guān)錯(cuò)誤 webBrowser1.ScriptErrorsSuppressed = true; //導(dǎo)航顯示本地HTML文件 webBrowser1.Navigate(url); } private void timer1_Tick(object sender, EventArgs e) { try { string tag_lng = webBrowser1.Document.GetElementById("mouselng").InnerText; string tag_lat = webBrowser1.Document.GetElementById("mouselat").InnerText; double dou_lng, dou_lat; if (double.TryParse(tag_lng, out dou_lng) && double.TryParse(tag_lat, out dou_lat)) { label2.Text = "當(dāng)前坐標(biāo):" + dou_lng.ToString("F6") + "," + dou_lat.ToString("F6");//保留小數(shù)點(diǎn)后6位 } } catch (Exception ee) { MessageBox.Show(ee.Message); } } private void btnGetLocation_Click(object sender, EventArgs e) { if (btnGetLocation.Text == "開(kāi)啟實(shí)時(shí)坐標(biāo)") { timer1.Enabled = true; btnGetLocation.Text = "關(guān)閉實(shí)時(shí)坐標(biāo)"; } else { btnGetLocation.Text = "開(kāi)啟實(shí)時(shí)坐標(biāo)"; timer1.Enabled = false; } } private void btnGPS_Click(object sender, EventArgs e) { webBrowser1.Document.GetElementById("longitude").InnerText = textBox_longitude.Text; webBrowser1.Document.GetElementById("latitude").InnerText = textBox_latitude.Text; } } }
到此這篇關(guān)于使用C#調(diào)用百度地圖并實(shí)現(xiàn)坐標(biāo)點(diǎn)的設(shè)置以及讀取示例的文章就介紹到這了,更多相關(guān)C#百度地圖坐標(biāo)點(diǎn)讀取內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用C#?11的靜態(tài)接口方法改進(jìn)?面向約定?的設(shè)計(jì)方法
我們知道接口是針對(duì)契約的定義,但是一直以來(lái)它只能定義一組“實(shí)例”的契約,而不能定義類型的契約,因?yàn)槎x在接口中的方法只能是實(shí)例方,這篇文章主要介紹了使用C#?11的靜態(tài)接口方法改進(jìn)面向約定?的設(shè)計(jì),需要的朋友可以參考下2022-12-12C# 打印網(wǎng)頁(yè)不顯示頁(yè)眉頁(yè)腳的實(shí)現(xiàn)方法
這篇文章主要介紹了C# 打印網(wǎng)頁(yè)不顯示頁(yè)眉頁(yè)腳的實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-01-01C#實(shí)現(xiàn)簡(jiǎn)易的計(jì)算器
這篇文章主要為大家詳細(xì)介紹了C#實(shí)現(xiàn)簡(jiǎn)易的計(jì)算器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-04-04DataGridView凍結(jié)列或行、列順序調(diào)整、操作行頭列頭標(biāo)題的方法
這篇文章介紹了DataGridView凍結(jié)列或行、列順序調(diào)整、操作行頭列頭標(biāo)題的方法,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-02-02