百度地圖API之如何制作駕車導(dǎo)航
讓用戶自己選擇起點(diǎn)和終點(diǎn)呢?答案是,使用數(shù)據(jù)接口。數(shù)據(jù)接口,可以讓百度地圖API的數(shù)據(jù),按照自定義的形式展示。
這個(gè)功能非常實(shí)用,學(xué)會(huì)這個(gè)方法,可以讓您的地圖更加接近百度地圖的功能??!
一、創(chuàng)建地圖與網(wǎng)頁樣式
創(chuàng)建一張簡單的地圖,只需要3句話。
- varmap =newBMap.Map("container"); //創(chuàng)建Map實(shí)例
- varpoint =newBMap.Point(116.404, 39.915); //創(chuàng)建點(diǎn)坐標(biāo)
- map.centerAndZoom(point,15); //初始化地圖,設(shè)置中心點(diǎn)坐標(biāo)和地圖級(jí)別。
然后,我們制作出兩個(gè)輸入框,分別是起點(diǎn)輸入框,和終點(diǎn)輸入框。
從<input class="txt"type="text"value="機(jī)場(chǎng)"id="startInput"/>到<input class="txt"type="text"value="西站"id="endInput"/> <input type="button"value="駕車"onclick="mDriving()"/>
使用“駕車”按鈕,獲取輸入框中的數(shù)據(jù)。
- functionmDriving(){varstartPlace =document.getElementById("startInput").value;varendPlace =document.getElementById("endInput").value;}
二、創(chuàng)建搜索實(shí)例
對(duì)于起點(diǎn)和終點(diǎn),我們需要?jiǎng)?chuàng)建2個(gè)不同的搜索實(shí)例:
- //創(chuàng)建2個(gè)搜索實(shí)例
- varstartSearch =newBMap.LocalSearch(map,startOption);varendSearch =newBMap.LocalSearch(map,endOption);
在點(diǎn)擊“駕車”按鈕后,開始搜索起點(diǎn)和終點(diǎn)都有哪些符合關(guān)鍵詞的地方(POI點(diǎn))。
- functionmDriving(){varstartPlace =document.getElementById("startInput").value;varendPlace =document.getElementById("endInput").value;
- startSearch.search(startPlace);
- endSearch.search(endPlace);
- document.getElementById("box").style.display="block";
- }
三、搜索的數(shù)據(jù)接口
由于AJAX是異步加載的,我們使用百度地圖API提供的回調(diào)函數(shù)onSearchComplete,來完成對(duì)搜索成功后的操作。
以起點(diǎn)的搜索為例:
當(dāng)搜索成功后,把每一個(gè)搜索結(jié)果(POI),按照我們自定義的方式,列在面板中。其實(shí),這里我們只使用了數(shù)據(jù)接口,沒用百度默認(rèn)的結(jié)果面板。
- varstartOption ={
- onSearchComplete: function(results){//判斷狀態(tài)是否正確if(startSearch.getStatus() ==BMAP_STATUS_SUCCESS){
- startResults =results;vars =[];for(vari =0;i <results.getCurrentNumPois(); i ++){
- s.push("<div><p><a onmouseover='map.openInfoWindow(startInfowin,startResults.getPoi("+i +").point);' href='#'>");
- s.push(results.getPoi(i).title);
- s.push("</a></p><p>");
- s.push(results.getPoi(i).address);
- s.push("</p></div>");
- }
- document.getElementById("startPanel").innerHTML =s.join("");
- }else{startResults =null;}
- }
- };
當(dāng)用戶鼠標(biāo)移到起點(diǎn)面板的標(biāo)題處,我們?cè)诘貓D上打開一個(gè)信息窗口。里面放“選為起點(diǎn)”的按鈕。
- varstartInfowin =newBMap.InfoWindow("<p class='t-c'><input value='選為起點(diǎn)' type='button' onclick='startDeter();' /></p>");
用戶點(diǎn)擊“選為起點(diǎn)”的按鈕后,選定該點(diǎn)為起點(diǎn),并隱藏起點(diǎn)面板,讓用戶選擇終點(diǎn)。
為了方便看清起點(diǎn)的位置,我們需要在地圖上打個(gè)紅色的標(biāo)注。并且,再次選擇起點(diǎn)時(shí),要清楚上一次的標(biāo)注。
- functionstartDeter(){
- map.clearOverlays();
- startPoint =startInfowin.getPosition();varmarker =newBMap.Marker(startPoint);
- map.addOverlay(marker);
- document.getElementById("startPanel").style.display="none";
- }
同理,制作終點(diǎn)的面板。這里需要注意的是,終點(diǎn)和起點(diǎn)不同,選擇終點(diǎn)之后,需要?jiǎng)?chuàng)建一個(gè)駕車實(shí)例,并且繪制出駕車路線。
所以要做一個(gè)判斷,用戶是否已經(jīng)選擇了起點(diǎn)。如果沒有,提示用戶要先選擇起點(diǎn)。
- functionendDeter(){ if(startPoint==null){alert("請(qǐng)先選擇起點(diǎn)!");}else{
- endPoint =endInfowin.getPosition();
- driving.search(startPoint,endPoint);
- document.getElementById("endPanel").style.display="none";
- }
- }
四、創(chuàng)建駕車實(shí)例和結(jié)果面板
在選擇完畢確定的終點(diǎn)和起點(diǎn)后,駕車的結(jié)果就明了了。
一句話,輕松搞定。
vardriving =newBMap.DrivingRoute(map, {renderOptions:{map: map, autoViewport: true,panel:drivingPanel}});
五、頁面樣式完善
為了讓頁面干凈好看,我們可以把不必要的結(jié)果展示暫時(shí)隱藏起來,當(dāng)需要它們的時(shí)候,再展開。
1、比如,先把地圖和搜索框以外的結(jié)果面板隱藏起來。
我使用了hidden樣式,來隱藏右邊的面板boxpanel。
- .hidden{display:none;}
- <div class="boxpanel hidden"id="box">中間省略</div>
對(duì)起點(diǎn)選擇和終點(diǎn)選擇面板,采取使用時(shí)“展開”,選取完畢即刻隱藏的辦法。例如,
- <h5>起點(diǎn)選擇 <a href="#"onclick="document.getElementById('startPanel').style.display='block';">(展開)</a></h5>
2、清除上次駕車查詢結(jié)果
如果你要再次使用駕車查詢,一定要先清除上次駕車查詢的結(jié)果:driving.clearResults();
也可以使用clearOverlays,一次性清除地圖上所有的覆蓋物。map.clearOverlays();
另外,補(bǔ)充一個(gè)清除覆蓋物的知識(shí):
清除地圖上所有的標(biāo)記,用map.clearOverlays();
清除單個(gè)標(biāo)注,用map.removeOverlay(marker);
顯示和隱藏自定義覆蓋物,可以繼承overlay的hide();或者show()方法。
附,全部源代碼:
- <!DOCTYPE html>
- <html>
- <head>
- <meta http-equiv="Content-Type"content="text/html; charset=gb2312"/>
- <title>指定起點(diǎn)與終點(diǎn)的駕車導(dǎo)航</title>
- <script type="text/javascript"src="http://api.map.baidu.com/api?v=1.2"></script>
- <style>body{font-size:14px;}h5{line-height:3em;padding:0;margin:0;}a{color:#EE3399;}a:hover{color:#99AA66;}.txt{border:1px solid #ccc;background:none;padding:1px;}.f-l{float:left;}.t-c{text-align:center;}.clear{clear:both;}.hidden{display:none;}.searchbox{border:4px solid #e5ecf9;height:40px;float:left;line-height:40px;padding:0 20px;margin:0 0 0 50px;}.mainbox{margin:20px 0 0;}.boxmap{width:700px;height:500px;border:1px solid gray;float:left;}.boxpanel{width:250px;float:left;margin:0 0 0 10px;border:1px solid gray;padding:0 10px 10px;}#startPanel,#endPanel{border:1px solid #FA8722;font-size:12px;}#startPanel div,#endPanel div{padding:5px;}#startPanel p,#endPanel p{margin:0;paddin:0;line-height:1.2em;}#drivingPanel{border:1px solid #6688EE;}
- </style>
- </head>
- <body>
- <div>
- <img class="f-l"src="http://map.baidu.com/img/logo-map.gif"/>
- <div class="searchbox">從<input class="txt"type="text"value="機(jī)場(chǎng)"id="startInput"/>到<input class="txt"type="text"value="西站"id="endInput"/> <input type="button"value="駕車"onclick="mDriving()"/>
- </div>
- </div>
- <div class="clear"></div>
- <div class="mainbox">
- <div class="boxmap"id="container"></div>
- <div class="boxpanel hidden"id="box">
- <h5>起點(diǎn)選擇 <a href="#"onclick="document.getElementById('startPanel').style.display='block';">(展開)</a></h5>
- <div id="startPanel"></div>
- <h5>終點(diǎn)選擇 <a href="#"onclick="document.getElementById('endPanel').style.display='block';">(展開)</a></h5>
- <div id="endPanel"></div>
- <h5>駕車導(dǎo)航</h5>
- <div id="drivingPanel"></div>
- </div>
- </div>
- </body>
- </html>
- <script type="text/javascript">
- varmap =newBMap.Map("container"); //創(chuàng)建Map實(shí)例
- varpoint =newBMap.Point(116.404, 39.915); //創(chuàng)建點(diǎn)坐標(biāo)
- map.centerAndZoom(point,15); //初始化地圖,設(shè)置中心點(diǎn)坐標(biāo)和地圖級(jí)別。
- varstartInfowin =newBMap.InfoWindow("<p class='t-c'><input value='選為起點(diǎn)' type='button' onclick='startDeter();' /></p>");varendInfowin =newBMap.InfoWindow("<p class='t-c'><input value='選為終點(diǎn)' type='button' onclick='endDeter();' /></p>");varstartResults =null;varendResults =null;varstartPoint;varendPoint;vardriving =newBMap.DrivingRoute(map, {renderOptions:{map: map, autoViewport: true,panel:drivingPanel}});varstartOption ={
- onSearchComplete: function(results){//判斷狀態(tài)是否正確if(startSearch.getStatus() ==BMAP_STATUS_SUCCESS){
- startResults =results;vars =[];for(vari =0;i <results.getCurrentNumPois(); i ++){
- s.push("<div><p><a onmouseover='map.openInfoWindow(startInfowin,startResults.getPoi("+i +").point);' href='#'>");
- s.push(results.getPoi(i).title);
- s.push("</a></p><p>");
- s.push(results.getPoi(i).address);
- s.push("</p></div>");
- }
- document.getElementById("startPanel").innerHTML =s.join("");
- }else{startResults =null;}
- }
- };varendOption ={
- onSearchComplete: function(results){//判斷狀態(tài)是否正確if(endSearch.getStatus() ==BMAP_STATUS_SUCCESS){
- endResults =results;vars =[];for(vari =0;i <results.getCurrentNumPois(); i ++){
- s.push("<div><p><a href='#' onmouseover='map.openInfoWindow(endInfowin,endResults.getPoi("+i +").point);'>");
- s.push(results.getPoi(i).title);
- s.push("</a></p><p>");
- s.push(results.getPoi(i).address);
- s.push("</p></div>");
- }
- document.getElementById("endPanel").innerHTML =s.join("");
- }else{endResults =null;}
- }
- };//創(chuàng)建2個(gè)搜索實(shí)例
- varstartSearch =newBMap.LocalSearch(map,startOption);varendSearch =newBMap.LocalSearch(map,endOption);functionmDriving(){varstartPlace =document.getElementById("startInput").value;varendPlace =document.getElementById("endInput").value;
- startSearch.search(startPlace);
- endSearch.search(endPlace);
- document.getElementById("box").style.display="block";
- }functionstartDeter(){
- map.clearOverlays();
- startPoint =startInfowin.getPosition();varmarker =newBMap.Marker(startPoint);
- map.addOverlay(marker);
- document.getElementById("startPanel").style.display="none";
- }functionendDeter(){ if(startPoint==null){alert("請(qǐng)先選擇起點(diǎn)!");}else{
- endPoint =endInfowin.getPosition();
- driving.search(startPoint,endPoint);
- document.getElementById("endPanel").style.display="none";
- }
- }</script>
【編輯推薦】