“百度地圖JavaScript API”簡單一實例開發__Java

來源:互聯網
上載者:User

一直沒能堅持寫部落格的習慣,時隔n個月再一次寫篇部落格,也是第一篇技術部落格,執行個體項目是兩三個月前在學校寫的,編寫得也比較簡單,初次接觸API的開發,多半隻是照葫蘆畫瓢,自己的編程能力還需要提高。

簡單介紹這個執行個體,執行個體的主要功能是展示廣州旅遊景點的位置和相關資訊,可以實現地點搜尋、公交搜尋以及駕車搜尋,開發語言主要是HTML+JavaScript,當然網頁細節的效果中使用了jquery,地圖麻點的顯示資料是放在了百度地圖LBS開放雲裡的。api的調用需要申請密鑰,開放雲使用起來還算方便,但出現過資料無法顯示全的情況,聯絡了客服確定問題存在後 就沒有然後了...

有網頁編程基礎的接觸百度地圖API的開發是比較容易的,開發語言課堂是沒教的,但自己完全可以抽空學習編程,不管怎樣照葫蘆畫瓢、順藤摸瓜都是學習的過程,對編程能夠熱愛和堅持下來很重要。

HTML+JS代碼:

<!doctype html><html lang="en"><head><meta htttp-equiv="Content-Type" content="text/html; charset=UTF-8"/><meta name="viewport" content="initial-scale=1.0,user-scalable=no"/><link rel="stylesheet" type="text/css" href="GZMap.css"><script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=xxx密鑰xxx"></script><!-- 路況資訊 --><link href="http://api.map.baidu.com/library/TrafficControl/1.4/src/TrafficControl_min.css" rel="stylesheet" type="text/css" /><!-- <script type="text/javascript" src="http://api.map.baidu.com/api?v=1.4"></script> --><script type="text/javascript" src="http://api.map.baidu.com/library/TrafficControl/1.4/src/TrafficControl_min.js"></script><!-- 載入百度地圖樣式資訊視窗 --><script type="text/javascript" src="http://api.map.baidu.com/library/SearchInfoWindow/1.5/src/SearchInfoWindow_min.js"></script><link rel="stylesheet" href="http://api.map.baidu.com/library/SearchInfoWindow/1.5/src/SearchInfoWindow_min.css" /><!-- 測距 --><script type="text/javascript" src="http://api.map.baidu.com/library/DistanceTool/1.2/src/DistanceTool_min.js"></script><script type="text/javascript" src="js/jquery-1.10.2.min.js"></script><script type="text/javascript" src="js/organictabs.jquery.js"></script><script type="text/javascript">$(function() {$("#filter_d").organicTabs({"speed": 100,});});</script><title>廣州旅圖</title></head><body><div id="container"><div id="title_d"><div id="logo_d"><img src="images/logo.jpg"></div><div id="filter_d"><ul class="nav"><li class="nav-one"><a href="#featured2" class="current">地點搜尋</a></li><li class="nav-two"><a href="#core2">公交轉乘</a></li><li class="nav-three"><a href="#jquerytuts2">駕車路線</a></li></ul><div class="list-wrap"><ul id="featured2"><div style="margin-right:10px;margin-top:10px">地點搜尋:<input type="text" style="width:400px;height:30px;font-size: 18px" id="suggestId" autocomplete="off"></ul><ul id="core2" class="hide"><div style="margin-top:10px">公交轉乘:<input type="text" id="txtBusSta" autocomplete="off" style="width:400px;height:30px;font-size: 18px"> 到 <input type="text" id="txtBusEnd" autocomplete="off" style="width:400px;height:30px;font-size: 18px"></ul><ul id="jquerytuts2" class="hide"><div style="margin-top:10px">駕車路線:<input type="text" id="txtTraveSta" autocomplete="off" style="width:400px;height:30px;font-size: 18px"> 到 <input type="text" id="txtTraveEnd" autocomplete="off" style="width:400px;height:30px;font-size: 18px"></ul></div></div></div><div id="type_selection"><ul id="navlist"><li><a href="index.htm" target="_self" id="current">人文建築</a></li></ul></div><div id="map_d"></div><div id="result_d"></div></div> </body></html><script type="text/javascript">// 百度地圖API功能var map = new BMap.Map("map_d",{minZoom:10,maxZoom:18}); // 建立Map執行個體map.centerAndZoom(new BMap.Point(113.37319,23.165775), 11);// 初始化地圖,設定中心點座標和地圖層級map.addControl(new BMap.NavigationControl());// 添加平移縮放控制項map.addControl(new BMap.ScaleControl()); // 添加比例尺控制項map.addControl(new BMap.OverviewMapControl());//添加縮減地圖控制項map.enableScrollWheelZoom();//啟用滾輪放大縮小map.addControl(new BMap.MapTypeControl()); //添加地圖類型控制項map.setCurrentCity("廣州");// 設定地圖顯示的城市 此項是必須設定的var customLayer;function addCustomLayer(keyword) {    if (customLayer) {        map.removeTileLayer(customLayer);    }    customLayer=new BMap.CustomLayer({        geotableId: 62093,        q: '', //檢索關鍵字        tags: '', //空格分隔的多字串        filter: '' //過濾條件,參考http://developer.baidu.com/map/lbs-geosearch.htm#.search.nearby    });    map.addTileLayer(customLayer);    customLayer.addEventListener('hotspotclick',callback);}addCustomLayer();function callback(e)//單擊熱點圖層{        var customPoi = e.customPoi;//poi的預設欄位        var contentPoi=e.content;//poi的自訂欄位        var content = '<p style="width:280px;margin:0;line-height:20px;">地址:'+customPoi.address+'</p>';        var searchInfoWindow = new BMapLib.SearchInfoWindow(map, content, {            title: customPoi.title, //標題            width: 290, //寬度            height: 40, //高度            panel : "panel", //檢索結果面板            enableAutoPan : true, //自動平移            enableSendToPhone: true, //是否顯示發送到手機按鈕            searchTypes :[                BMAPLIB_TAB_SEARCH,   //周邊檢索                BMAPLIB_TAB_TO_HERE,  //到這裡去                BMAPLIB_TAB_FROM_HERE //從這裡出發            ]        });        var point = new BMap.Point(customPoi.point.lng, customPoi.point.lat);        searchInfoWindow.open(point);}// 路況資訊    var ctrl = new BMapLib.TrafficControl({           showPanel: false //是否顯示路況提示面板      });          map.addControl(ctrl);    ctrl.setAnchor(BMAP_ANCHOR_BOTTOM_RIGHT);       // 本地查詢var s_poi = new BMap.Autocomplete(    //建立一個自動完成的對象    {"input" : "suggestId",     "location" : map});var myValue;s_poi.addEventListener("onconfirm", function(e) {    //滑鼠點擊下拉式清單後的事件var _value = e.item.value;    myValue = _value.province +  _value.city +  _value.district +  _value.street +  _value.business;      setPlace();});function setPlace(){document.getElementById("txtBusSta").value ="";document.getElementById("txtBusEnd").value ="";document.getElementById("txtTraveSta").value ="";document.getElementById("txtTraveEnd").value ="";    map.clearOverlays();    //清除地圖上所有覆蓋物    function myFun(){        var pp = local.getResults().getPoi(0).point; //擷取第一個智能搜尋的結果        map.centerAndZoom(pp,15);        var marker = new BMap.Marker(pp);        map.addOverlay(marker);    //添加標註        marker.setAnimation(BMAP_ANIMATION_BOUNCE);//標註跳動    }    var local = new BMap.LocalSearch(map, { //智能搜尋      onSearchComplete: myFun    });    local.search(myValue);}// 公交查詢var t_BusSta = new BMap.Autocomplete({"input":"txtBusSta","location":map});var busStaValue;t_BusSta.addEventListener("onconfirm", function(e) {    //滑鼠點擊下拉式清單後的事件var _value = e.item.value;    busStaValue = _value.province +  _value.city +  _value.district +  _value.street +  _value.business;});var t_BusEnd = new BMap.Autocomplete({"input":"txtBusEnd","location":map});var busEndValue;t_BusEnd.addEventListener("onconfirm", function(e) {    //滑鼠點擊下拉式清單後的事件var _value = e.item.value;    busEndValue = _value.province +  _value.city +  _value.district +  _value.street +  _value.business;    transitRoute();});function transitRoute(){document.getElementById("suggestId").value = "";document.getElementById("txtTraveSta").value ="";document.getElementById("txtTraveEnd").value ="";map.clearOverlays();var transit = new BMap.TransitRoute(map,{renderOptions:{map:map,panel:"result_d"}});transit.search(busStaValue,busEndValue);}// 駕車查詢var t_TraveSta= new BMap.Autocomplete({"input":"txtTraveSta","location":map});var traveStaValue;t_TraveSta.addEventListener("onconfirm", function(e) {    //滑鼠點擊下拉式清單後的事件var _value = e.item.value;    traveStaValue = _value.province +  _value.city +  _value.district +  _value.street +  _value.business;});var t_TraveEnd = new BMap.Autocomplete({"input":"txtTraveEnd","location":map});var traveEndValue;t_TraveEnd.addEventListener("onconfirm", function(e) {    //滑鼠點擊下拉式清單後的事件var _value = e.item.value;    traveEndValue = _value.province +  _value.city +  _value.district +  _value.street +  _value.business;    drivingRoute();});function drivingRoute(){document.getElementById("suggestId").value = "";document.getElementById("txtBusSta").value ="";document.getElementById("txtBusEnd").value ="";map.clearOverlays();var driving = new BMap.DrivingRoute(map,{renderOptions:{map:map,panel:"result_d"}});driving.search(traveStaValue,traveEndValue);}// 右鍵菜單var contextMenu = new BMap.ContextMenu();var txtMenuItem = [  {  text:'測距',   callback:function(){   var myDis = new BMapLib.DistanceTool(map);   myDis.open();   }  },  {   text:'在此添加標註',   callback:function(p){    var marker = new BMap.Marker(p), px = map.pointToPixel(p);    map.addOverlay(marker);   }  },  {   text:'放置到最大級',   callback:function(){map.setZoom(14)}  }, ]; for(var i=0; i < txtMenuItem.length; i++){  contextMenu.addItem(new BMap.MenuItem(txtMenuItem[i].text,txtMenuItem[i].callback,100)); } map.addContextMenu(contextMenu);   </script>

頁面展示:


百度LBS開放平台:


本文著作權歸作者所有,歡迎轉載學習,但需在文章頁面明顯位置給出原文串連,否則保留追究法律責任的權利。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.