官方demo连接:http://lbsyun.baidu.com/jsdemo.htm#i5_9
在C#中使用webbroswer进行地图显示,并利用百度地图API进行路径规划;先上效果图
1 HTML文件的编写
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <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=2.0&ak=需要自己申请百度API"></script> <!--加载百度API函数--> <script type="text/javascript" src="DrawingManager_min.js"></script> <!--加载绘制工具--> <script src="DistanceTool_min.js" type="text/javascript"></script> <!--//加载测距工具--> <title>百度地图的Hello, World</title> </head>
2 Javascript脚本代码
<script> function GENERATE_ROUTE(e) { //map.addEventListener("click", showPoly); map.addEventListener("click", generate_rout); } function generate_rout() { var p1 = new BMap.Point(121.443532,31.24603); var p2 = new BMap.Point(121.481477,31.240103); var p3 = new BMap.Point(121.493262,31.237015); var p4 = new BMap.Point(121.49639,31.22762); var p5 = new BMap.Point(121.489382,31.225034); var p6 = new BMap.Point(121.512953,31.219846); var p7 = new BMap.Point(121.510222,31.228122); var p8 = new BMap.Point(121.520715,31.232198); var p9 = new BMap.Point(121.515828,31.239485); var p10 = new BMap.Point(121.498724,31.238868); var pointList = [] ; pointList.push(p1);pointList.push(p2);pointList.push(p3);pointList.push(p4);pointList.push(p5); var driving = new BMap.DrivingRoute(map, {renderOptions:{map: map, autoViewport: true}}); for(c=0;c<pointList.length;c++){ var marker = new BMap.Marker(pointList[c]); map.addOverlay(marker); //将途经点按顺序添加到地图上 var label = new BMap.Label(c+1,{offset:new BMap.Size(20,-10)}); marker.setLabel(label); } //driving.search(p1, p5,{waypoints:["华盛小亭","上海科技京城", "豫园"]}); driving.search(p1, p10,{waypoints:[p2,p3,p4,p5,p6,p7,p8,p9]}); } </script>
3 c#添加交互代码
private void generateRout_Click(object sender, EventArgs e) { webBrowser1.Document.InvokeScript("generate_rout"); }
4注意事项
(1)引用的添加
//添加引用: using System.Security.Permissions; //许可及com可见 [PermissionSet(SecurityAction.Demand, Name = "FullTrust")] [System.Runtime.InteropServices.ComVisibleAttribute(true)]
(2)存在缺陷:
1)每次更新坐标需要重新生成exe,十分不便捷
2)百度API提供的起点、终点及经过点覆盖自己定义的标记信息,可作进一步改善
3)目前只是照葫芦画瓢,只是迈出了一小步,规划的路径存明显不是最优;还应深入研究其内部机理,多参考资料进行改善