1 $(document).ready(function () { 2 /*用正则表达式获取url传递的地址参数,split后获得地址数组*/ 3 bmap = new BMap.Map('mapcontainer'); 4 var point = new BMap.Point(116.404, 39.915);//地图中心点 5 bmap.centerAndZoom(point, 15);//调整缩放以及设立中心点 6 bmap.enableScrollWheelZoom(); 7 var locations = ["天安门", "北京邮电大学", "地坛公园", "北京师范大学"]; 8 var i = 0; 9 var points=[];//保存目标点用于调整viewport 10 var driving = new BMap.DrivingRoute(bmap, { 11 policy : BMAP_DRIVING_POLICY_LEAST_TIME, 12 onSearchComplete : function (results) { 13 console.log(i); 14 var path = results.getPlan(0).getRoute(0).getPath(); 15 var pathpoly = new BMap.Polyline(path, { 16 strokeColor : "blue", 17 strokeWeight : 6, 18 strokeOpacity : 0.5 19 }); 20 if (i === 0) { 21 console.log('起点' + results.getStart()); 22 var start = new BMap.Marker(results.getStart().point); 23 start.setTitle('起点:' + results.getStart().title); 24 bmap.addOverlay(start); 25 points.push(results.getStart().point); 26 } 27 var marker = new BMap.Marker(results.getEnd().point); 28 points.push(results.getEnd().point); 29 marker.setTitle('经过点' + (i + 1) + ':' + results.getEnd().title); 30 bmap.addOverlay(pathpoly); 31 bmap.addOverlay(marker); 32 i++; 33 if (i < locations.length - 1) { 34 setTimeout(driving.search(locations[i], locations[i + 1]), 10000);//调用后面的路径
35 }
36 bmap.setViewport(points); 37 } 38 }); 39 driving.search(locations[0], locations[1]);//调用第一条路径
40 });