【百度地圖API】如何使用suggestion--下拉列表方式的搜索建議


摘要:

  百度地圖上有一個很強大的搜索建議功能,以下拉列表的方式展示出來。比如,輸入“百度”,下拉列表中就會出現“北京市海淀區百度在線網絡技術(北京)有限公司”。這個如何實現呢?讓我們一步一步來學習。

------------------------------------------------------------------------------------------------------------------------------------

 

 

一、suggestion 功能示意圖

 

 

二、suggestion的類參考

更多類參考,請點擊這里:http://dev.baidu.com/wiki/map/index.php?title=Class:%E6%9C%8D%E5%8A%A1%E7%B1%BB/Autocomplete

 

 

三、實現HTML的結構

 

寫三個框,分別是suggestion的下拉列表、地圖容器,和最終信息顯示框。

<div style="margin:50px">請輸入:<input type="text" id="suggestId" size="30" value="百度" style="width:300px;" /></div>
<div id="searchResultPanel" style="border:1px solid #C0C0C0;width:300px;height:600px;position:absolute;left: 650px;top:20px;"></div>
<div id="container"></div>



四、suggestion的使用

首先,創建一個自動完成的對象。

其中,suggestId就是輸入框的id,通過它,能獲取到用戶輸入了什么。

onSearchComplete是搜索到結果后的回調函數,可以不用設置。

var ac = new BMap.Autocomplete(    //建立一個自動完成的對象
{"input" : "suggestId"
,"location" : map
});

 

 

根據類參考,suggestion有如下事件。分別可以控制,鼠標在下拉列表上的選擇(類似onfouce),和點擊確定下拉列表的選項。

 

我們設置當鼠標在下拉列表上,和點擊下拉列表后,都會在右邊的信息展示框,展示結果數據。

ac.addEventListener("onhighlight", function(e) {  //鼠標放在下拉列表上的事件
var str = "";
var _value = e.fromitem.value;
var value = "";
if (e.fromitem.index > -1) {
value = _value.province + _value.city + _value.district + _value.street + _value.business;
}
str = "FromItem<br />index = " + e.fromitem.index + "<br />value = " + value;

value = "";
if (e.toitem.index > -1) {
_value = e.toitem.value;
value = _value.province + _value.city + _value.district + _value.street + _value.business;
}
str += "<br />ToItem<br />index = " + e.toitem.index + "<br />value = " + value;
G("searchResultPanel").innerHTML = str;
});

ac.addEventListener("onconfirm", function(e) { //鼠標點擊下拉列表后的事件
var _value = e.item.value;
var myValue = _value.province + _value.city + _value.district + _value.street + _value.business
G("searchResultPanel").innerHTML = "onconfirm<br />index = " + e.item.index + "<br />myValue = " + myValue;

});



 

五、通過地址解析設置中心點

由於suggestion返回的是地址數據,並沒有point經緯度的信息。我們需要自己在回調函數,或者其他地方通過地址解析來打點。

function setPlace(){// 創建地址解析器實例
var myGeo = new BMap.Geocoder();// 將地址解析結果顯示在地圖上,並調整地圖視野
myGeo.getPoint(myValue, function(point){
if (point) {
map.centerAndZoom(point, 16);
map.addOverlay(new BMap.Marker(point));
}
}, "北京");
}

 

 

加上地址解析之后,能通過獲得的地址位置的描述,得到百度經緯度point。隨后,添加一個marker覆蓋物,即可完成打點工作。最后再把地圖中心點設置為point。

 

 

六、備注

這個教程是一個最簡單的示例,方便大家學習和上手。

由於沒有設置城市,該示例只適用於北京市內。詳細的城市設定,請看類參考:

 

 

七、全部源代碼

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<title>自動提示</title>
<script type="text/javascript" src="http://api.map.baidu.com/api?v=1.2"></script>
<style type="text/css">
body
{font-size:13px;margin:0px}
#container
{width:600px;height:400px}
.label
{margin-left:20px;font-weight:bold;font-size:14px}
</style>
</head>
<body>
<div style="margin:50px">請輸入:<input type="text" id="suggestId" size="30" value="百度" style="width:300px;" /></div>
<div id="searchResultPanel" style="border:1px solid #C0C0C0;width:300px;height:600px;position:absolute;left: 650px;top:20px;"></div>
<div id="container"></div>
<script type="text/javascript">
function G(id) {
return document.getElementById(id);
}

var map = new BMap.Map("container");
var point = new BMap.Point(116.3964,39.9093);
map.centerAndZoom(point,
13);
map.enableScrollWheelZoom();

var ac = new BMap.Autocomplete( //建立一個自動完成的對象
{"input" : "suggestId"
,
"location" : map
});

ac.addEventListener(
"onhighlight", function(e) { //鼠標放在下拉列表上的事件
var str = "";
var _value = e.fromitem.value;
var value = "";
if (e.fromitem.index > -1) {
value
= _value.province + _value.city + _value.district + _value.street + _value.business;
}
str
= "FromItem<br />index = " + e.fromitem.index + "<br />value = " + value;

value
= "";
if (e.toitem.index > -1) {
_value
= e.toitem.value;
value
= _value.province + _value.city + _value.district + _value.street + _value.business;
}
str
+= "<br />ToItem<br />index = " + e.toitem.index + "<br />value = " + value;
G(
"searchResultPanel").innerHTML = str;
});

var myValue;
ac.addEventListener(
"onconfirm", function(e) { //鼠標點擊下拉列表后的事件
var _value = e.item.value;
myValue
= _value.province + _value.city + _value.district + _value.street + _value.business;
G(
"searchResultPanel").innerHTML ="onconfirm<br />index = " + e.item.index + "<br />myValue = " + myValue;

setPlace();
});

function setPlace(){// 創建地址解析器實例
var myGeo = new BMap.Geocoder();// 將地址解析結果顯示在地圖上,並調整地圖視野
myGeo.getPoint(myValue, function(point){
if (point) {
map.centerAndZoom(point,
16);
map.addOverlay(
new BMap.Marker(point));
}
},
"北京");
}
</script>
</body>
</html>




免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM