您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

C# 程序中嵌入百度地图

2025/8/8 6:52:35发布30次查看
本例是对winform中使用百度地图的简要介绍。百度地图目前支持android开发,ios开发,web开发,服务接口,具体可以参照'百度地图开放平台'。
【动态加载百度地图】涉及到的知识点:
webbrowser控件,此控件是vs自带的控件,使用户可以在winform窗体中导航网页。主要用到navigate函数,此函数将指定的统一资源定位符 (url) 处的文档加载到浏览器新窗口或 system.windows.forms.webbrowser 控件中。有关此控件的详细信息,请参照msdn上详细说明。
百度地图javascript api,调用api在网页中显示百度地图。
效果图如下:
关于调用百度地图的html代码如下:
1 <!doctype html> 2 <html> 3 <head> 4 <meta http-equiv="content-type" content="text/html; charset=utf-8" /> 5 <meta name="viewport" content="initial-scale=1.0, user-scalable=no" /> 6 <style type="text/css"> 7 body, html,#allmap {width: 100%;height: 100%;overflow: hidden;margin:0;font-family:"微软雅黑";} 8 </style> 9 <script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=akcode需要申请"></script> 10 <title>地图展示</title> 11 <script type="text/javascript"> 12 window.onload = function () { 13 // 百度地图api功能 14 var map = new bmap.map("allmap"); 15 var point = new bmap.point(116.404, 39.915); 16 map.centerandzoom(point, 15); 17 // 编写自定义函数,创建标注 18 function addmarker(point) { 19 var marker = new bmap.marker(point); 20 map.addoverlay(marker); 21 } 22 // 随机向地图添加25个标注 23 var bounds = map.getbounds(); 24 var sw = bounds.getsouthwest(); 25 var ne = bounds.getnortheast(); 26 var lngspan = math.abs(sw.lng - ne.lng); 27 var latspan = math.abs(ne.lat - sw.lat); 28 for (var i = 0; i < 25; i++) { 29 var point = new bmap.point(sw.lng + lngspan * (math.random() * 0.7), ne.lat - latspan * (math.random() * 0.7)); 30 addmarker(point); 31 } 32 // 33 var top_left_control = new bmap.scalecontrol({ anchor: bmap_anchor_top_left }); // 左上角,添加比例尺 34 var top_left_navigation = new bmap.navigationcontrol(); //左上角,添加默认缩放平移控件 35 var top_right_navigation = new bmap.navigationcontrol({ anchor: bmap_anchor_top_right, type: bmap_navigation_control_small }); //右上角,仅包含平移和缩放按钮 36 map.addcontrol(top_left_control); 37 map.addcontrol(top_left_navigation); 38 map.addcontrol(top_right_navigation); 39 } 40 </script> 41 </head> 42 <body> 43 <div id="allmap"></div> 44 </body> 45 </html>
关于winform调用html的代码如下:
private void baidumap01_load(object sender, eventargs e) 2 { 3 //htm文件copy到程序根目录 4 this.wbbaidu.navigate(appdomain.currentdomain.basedirectory + "baidu01.htm",false); 5 }
【加载静态图】涉及到知识点
调用百度的静态图接口
picturebox vs自带的图片容器,表示用于显示图像的 windows 图片框控件。
httpwebrequest,httpwebresponse 在winform中发送/接收 http请求。
thread 为了不让界面卡死,采用在后台进程中调用。
将返回的字节流,转换成image对象
效果图如下:
关于在winform程序中调用静态图api的代码如下:
1 using system; 2 using system.collections.generic; 3 using system.componentmodel; 4 using system.data; 5 using system.drawing; 6 using system.linq; 7 using system.text; 8 using system.windows.forms; 9 using system.net; 10 using system.io; 11 using system.threading; 12 13 namespace demosharp 14 { 15 public partial class baidumap02 : form 16 { 17 public baidumap02() 18 { 19 initializecomponent(); 20 } 21 22 private void btnload_click(object sender, eventargs e) 23 { 24 //在线程中执行 25 thread t = new thread(new threadstart(initmap)); 26 t.start(); 27 } 28 29 private void initmap() { 30 string url = "http://api.map.baidu.com/staticimage/v2?ak=akcode需要申请&mcode=666666&center=116.403874,39.914888&width=910&height=400&zoom=11"; 31 httpwebrequest request = (httpwebrequest)httpwebrequest.create(url); 32 request.method = "get"; 33 httpwebresponse response = request.getresponse() as httpwebresponse; 34 while (true) 35 { 36 if (response.statuscode == httpstatuscode.ok) 37 { 38 image img = image.fromstream(response.getresponsestream()); 39 this.picturebox1.image = img; 40 break; 41 } 42 thread.sleep(1000); 43 } 44 } 45 } 46 }
后记:
调用百度地图相关功能时,需要先申请密钥(ak),个人开发学习使用手机进行注册即可。
以上就是c# 程序中嵌入百度地图的内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product