在页面中加入:
<script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=Xbq3g4meudxD5Q0MB9osTLpg"></script>
JS:
var myAddress = $('input[name=address]').val();
var destPoint = new BMap.Point($('input[name=primarylng]').val(), $('input[name=primarylat]').val());
var map = new BMap.Map('map');
map.centerAndZoom(new BMap.Point(destPoint.lng, destPoint.lat), 20);
map.enableScrollWheelZoom();
map.addControl(new BMap.NavigationControl());
var marker = new BMap.Marker(destPoint);
map.addOverlay(marker);
map.addEventListener('click', function(e){
destPoint=e.point;
set_primary_input();
map.clearOverlays();
map.addOverlay(new BMap.Marker(destPoint));
});
var ac = new BMap.Autocomplete({'input':'address','location':map}); //建立一个自动完成的对象
ac.addEventListener('onhighlight', function(e) { //鼠标放在下拉列表上的事件
ac.setInputValue(e.toitem.value.business);
});
ac.setInputValue(myAddress);
var myValue;
ac.addEventListener('onconfirm', function(e) {//鼠标点击下拉列表后的事件
var _value = e.item.value;
myValue = _value.province + _value.city + _value.district + _value.street + _value.business;
myAddress=_value.business;
ac.setInputValue(myAddress);
setPlace();
});
function setPlace(){
map.clearOverlays(); //清除地图上所有覆盖物
function myFun(){
var pp = local.getResults().getPoi(0).point; //获取第一个智能搜索的结果
map.centerAndZoom(pp, 18);
map.addOverlay(new BMap.Marker(pp)); //添加标注
}
var local = new BMap.LocalSearch(map, { //智能搜索
onSearchComplete: myFun
});
local.search(myValue);
}