<script type="text/javascript" src="//api.map.baidu.com/api?v=3.0&ak=你的ak"></script>
<script type="text/javascript">
// 百度地图API功能
var map = new BMap.Map("allmap");
var point = new BMap.Point(116.331398,39.897445);
map.centerAndZoom(point,11);
city = "{$city}" //初始化城市 传入城市名字
if(city != ""){
map.centerAndZoom(city,11); // 用城市名设置地图中心点
}
var ac = new BMap.Autocomplete( //建立一个自动完成的对象
{"input" : "suggestId"
,"location" : map
});
map.addEventListener("click", function(e){
addMarker(e.point.lng, e.point.lat) //添加标记
map_click(e.point.lng, e.point.lat) //input 添加地址,经纬度转换地址
});
map.enableScrollWheelZoom(true);
// 用经纬度设置地图中心点
function addMarker(lng, lat){
var point = new BMap.Point(lng, lat);
map.centerAndZoom(point, 15);
var marker = new BMap.Marker(point); // 创建标注
map.clearOverlays(); //清除所有标记
map.addOverlay(marker);
}
function map_click(lng,lat){
var point = new BMap.Point(lng,lat);
map.centerAndZoom(point,12);
var geoc = new BMap.Geocoder();
geoc.getLocation(point,function(rs){
var addComp = rs.addressComponents;
if(addComp.province == addComp.city){
var address = addComp.city + addComp.district + addComp.street + addComp.streetNumber
}else{
var address = addComp.province + addComp.city + addComp.district + addComp.street + addComp.streetNumber
}
$("#suggestId").val(address)
});
}
</script>
// 根据地址获取经度 并标记
<script type="text/javascript">
// 百度地图API功能
var map = new BMap.Map("allmap");
keyword = "杭州" //传入的地址
var localSearch = new BMap.LocalSearch(map);
function searchByStationName() {
map.clearOverlays();//清空原来的标注
localSearch.setSearchCompleteCallback(function (searchResult) {
var poi = searchResult.getPoi(0);
map.centerAndZoom(poi.point, 13);
var marker = new BMap.Marker(new BMap.Point(poi.point.lng, poi.point.lat)); // 创建标注,为要查询的地址对应的经纬度
map.addOverlay(marker);
});
localSearch.search(keyword);
}
searchByStationName();
window.onload = function(){
searchByStationName()
}
</script>