<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=这里是获取到的ak"></script>
</head>
<body>
<input name="address" value=""id="where" class="case_text" type="text">
</div>
<div style="display: inline" onClick="sear(document.getElementById('where').value);" >
搜索
</div>
</div>
<input name="latitude" value="116.403928|39.91512|19"id="map" class="case_text" type="text">
<div style="width:100%;height:340px;border:1px solid gray" id="container"></div>
<script>
var map_id = document.getElementById("map").value;
var zoom = "";
if (map_id){
map_array = map_id.split("|" );
lng = map_array[0];
lat = map_array[1];
zoom = map_array[2];
}else{
lng = 116.403928;
lat = 39.91512;
zoom = 15;
}
var is_empty =0
var map = new BMap.Map("container");//在指定的容器内创建地图实例
map.setDefaultCursor("crosshair");//设置地图默认的鼠标指针样式
map.enableScrollWheelZoom();//启用滚轮放大缩小,默认禁用。
var point =new BMap.Point(lng,lat)
map.centerAndZoom(point, zoom);
map.addControl(new BMap.NavigationControl());
var marker = new BMap.Marker(point); // 创建标注
this.map.addOverlay(marker);
console.log(map.getZoom());
map.addEventListener("click", function(e){//地图单击事件
var geocoder = new BMap.Geocoder();
var point = new BMap.Point(e.point.lng,e.point.lat);
geocoder.getLocation(point,function(geocoderResult,LocationOptions){
map.clearOverlays()
map.addControl(new BMap.NavigationControl());
var marker = new BMap.Marker(point); // 创建标注
this.map.addOverlay(marker);
//定位成功
var address = geocoderResult.address;
document.getElementById("where").value =address
zoom = map.getZoom();
});
document.getElementById("map").value = e.point.lng+"|"+e.point.lat+"|"+zoom;
});
function sear(result){//地图搜索
if(result.length ==0){
dingwei();
return false
}
var local = new BMap.LocalSearch(map, {
renderOptions:{map: map}
});
local.search(result);
}
</script>
</body>
</html>