使用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ǎ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)擊開始實(shí)時(shí)顯示按鈕,鼠標(biāo)在地圖上移動(dòng),可以獲得實(shí)時(shí)經(jīng)緯度
最終圖

利用webBrowser控件展示地圖
VS創(chuàng)建工程,添加控件webBrowser,新建.html文件,.html文件參考百度API,將其寫入文件
為了能與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)頁路徑
webBrowser1.ObjectForScripting = this; // 將當(dāng)前類設(shè)置為可由腳本訪問
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)聽事件
//添加地圖類型控件
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); //開啟鼠標(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ù)需求來寫
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)頁路徑
webBrowser1.ObjectForScripting = this; // 將當(dāng)前類設(shè)置為可由腳本訪問
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 == "開啟實(shí)時(shí)坐標(biāo)")
{
timer1.Enabled = true;
btnGetLocation.Text = "關(guān)閉實(shí)時(shí)坐標(biāo)";
}
else
{
btnGetLocation.Text = "開啟實(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#使用NPOI設(shè)置Excel下拉選項(xiàng)
這篇文章主要為大家詳細(xì)介紹了C#使用NPOI設(shè)置Excel下拉選項(xiàng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02
Unity?UGUI的RawImage原始圖片組件使用示例詳解
這篇文章主要為大家介紹了Unity?UGUI的RawImage原始圖片組件使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07
DevExpress之餅狀圖突出(Explode)設(shè)置實(shí)例
這篇文章主要介紹了DevExpress之餅狀圖突出(Explode)設(shè)置方法,以實(shí)例形式展示了餅狀圖突出設(shè)置的具體實(shí)現(xiàn)過程,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2014-10-10
C#執(zhí)行表達(dá)式樹(Expression Tree)的具體使用
本文將深入探討表達(dá)式樹的基本概念、創(chuàng)建方法、修改和刪除節(jié)點(diǎn)、查詢和遍歷技巧以及在C#中的應(yīng)用示例,具有一定的參考價(jià)值,感興趣的可以了解一下2024-03-03
Unity3D獲取當(dāng)前鍵盤按鍵及Unity3D鼠標(biāo)、鍵盤的基本操作
這篇文章主要介紹了Unity3D獲取當(dāng)前鍵盤按鍵及Unity3D鼠標(biāo)、鍵盤的基本操作的相關(guān)資料,需要的朋友可以參考下2015-11-11
c#實(shí)現(xiàn)一個(gè)超實(shí)用的證件照換底色小工具(附源碼)
這篇文章主要給大家介紹了關(guān)于利用c#實(shí)現(xiàn)一個(gè)超實(shí)用的證件照換底色小工具的相關(guān)資料,通過這個(gè)小工具大家可以很方便的進(jìn)行底色的切換,不用再因?yàn)榈咨脑蝾^疼了,需要的朋友可以參考借鑒,下面來一起學(xué)習(xí)學(xué)習(xí)吧。2018-01-01

