【百度地圖API】如何使用suggestion--下拉列表方式的搜尋建議
摘要:
百度地圖上有一個很強大的搜尋建議功能,以下拉列表的方式展示出來。比如,輸入“百度”,下拉列表中就會出現“北京市海淀區百度線上網路技術(北京)有限公司”。這個如何實現呢?讓我們一步一步來學習。
------------------------------------------------------------------------------------------------------------------------------------
一、suggestion 功能示意圖
二、suggestion的類參考
三、實現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>