在實現下面代碼之前要先到百度地圖開放平臺申請一個祕鑰,然後參照官方文檔進行開發
<!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=2.0&ak=你的祕鑰"></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}
.lng-lat{
margin: 0 0 30px 0px;
}
.lng-lat .item{
margin: 10px;
}
</style>
</head>
<body>
<div style="margin:10px">請輸入:<input type="text" id="suggestId" size="30" value="百度" style="width:300px;" /></div>
<div class="lng-lat">
<div class="item">
當前經度:
<input type="text" id="jing"/>
</div>
<div class="item">
當前緯度:
<input type="text" id="wei" />
</div>
</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 lng = document.getElementById('jing');
var lat = document.getElementById('wei');
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) { //鼠標點擊下拉列表後的事件
lng.value = '';
lat.value = '';
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));
}
}, "北京");
}
map.addEventListener('click', function (e) {
lng.value = e.point.lng;
lat.value = e.point.lat;
})
//定位到當前位置
var geolocation = new BMap.Geolocation();
geolocation.getCurrentPosition(function(r){
if(this.getStatus() == BMAP_STATUS_SUCCESS){
var mk = new BMap.Marker(r.point);
map.addOverlay(mk);
map.panTo(r.point);
alert('您的位置:'+r.point.lng+','+r.point.lat);
}
else {
alert('failed'+this.getStatus());
}
});
</script>
</body>
</html>
實現效果: