本文實(shí)例講述了WinForm調(diào)用百度地圖接口用法。分享給大家供大家參考,具體如下:
1、首先用一個(gè)html文件調(diào)用百度地圖接口(主要注冊(cè)一個(gè)序列號(hào)):
<!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"> 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; } </style> <script type="text/javascript" src="http://api.map.baidu.com/api?v=1.5&ak=6c497f51c06477544e5fa6e9bd68f7c3"></script></head><body> <div id="allmap"> </div></body></html><script type="text/javascript"> //alert("Hello World"); var map = new BMap.Map("allmap"); // 創(chuàng)建Map實(shí)例 var point = new BMap.Point(121.504, 31.212); // 創(chuàng)建點(diǎn)坐標(biāo)(經(jīng)度,緯度) map.centerAndZoom(point, 11); // 初始化地圖,設(shè)置中心點(diǎn)坐標(biāo)和地圖大小級(jí)別 map.addOverlay(new BMap.Marker(point)); // 給該坐標(biāo)加一個(gè)紅點(diǎn)標(biāo)記 //var traffic = new BMap.TrafficLayer(); // 創(chuàng)建交通流量圖層實(shí)例 //map.addTileLayer(traffic); // 將圖層添加到地圖上 map.addControl(new BMap.NavigationControl()); // 添加平移縮放控件 map.addControl(new BMap.ScaleControl()); // 添加比例尺控件 map.addControl(new BMap.OverviewMapControl()); //添加縮略地圖控件 map.addControl(new BMap.MapTypeControl()); //添加地圖類型控件 map.setCurrentCity("上海"); //設(shè)置地圖顯示的城市 map.enableScrollWheelZoom(); //啟用滾輪放大縮小 function setLocation(x,y){//參數(shù):經(jīng)緯度 var point = new BMap.Point(x, y); map.centerAndZoom(point, 11); map.addOverlay(new BMap.Marker(point)); }</script>
2、建立一個(gè)Winform項(xiàng)目,用一個(gè)WebBrower控件查看html文件、調(diào)用JavaScript代碼
using System;using System.IO;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;namespace BaiDuMap{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { try { //webBrowser1.Url = new Uri("https://www.baidu.com"); //這個(gè)文件于可執(zhí)行文件放在同一目錄 webBrowser1.Url = new Uri(Path.Combine(Application.StartupPath, "GoogleMap.htm")); } catch (Exception ex) { MessageBox.Show(ex.Message, "異常", MessageBoxButtons.OK, MessageBoxIcon.Error); } } private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) { //這里傳入x、y的值,調(diào)用JavaScript腳本 webBrowser1.Document.InvokeScript("setLocation", new object[] { 121.504, 39.212 }); } }}
希望本文所述對(duì)大家C#程序設(shè)計(jì)有所幫助。
|
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注