draw.html
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no"/>
<style type="text/css">
body, html{width: 100%;height: 100%;margin:0;font-family:"微軟雅黑";}
#allmap {width: 100%; height:500px; overflow: hidden;}
#result {width:100%;font-size:12px;}
dl,dt,dd,ul,li{
margin:0;
padding:0;
list-style:none;
}
p{font-size:12px;}
dt{
font-size:14px;
font-family:"微軟雅黑";
font-weight:bold;
border-bottom:1px dotted #000;
padding:5px 0 5px 5px;
margin:5px 0;
}
dd{
padding:5px 0 0 5px;
}
li{
line-height:28px;
}
</style>
<script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=O*************k"></script>
<script type="text/javascript" src="http://api.map.baidu.com/library/DrawingManager/1.4/src/DrawingManager_min.js"></script>
<link rel="stylesheet" href="http://api.map.baidu.com/library/DrawingManager/1.4/src/DrawingManager_min.css"/>
<script type="text/javascript" src="http://api.map.baidu.com/library/SearchInfoWindow/1.4/src/SearchInfoWindow_min.js"></script>
<link rel="stylesheet" href="http://api.map.baidu.com/library/SearchInfoWindow/1.4/src/SearchInfoWindow_min.css"/>
<title>百度地圖API功能演示</title>
</head>
<body>
<div id="allmap" style="overflow:hidden;zoom:1;position:relative;">
<div id="map" style="height:100%;-webkit-transition: all 0.5s ease-in-out;transition: all 0.5s ease-in-out;">
</div>
</div>
<div id="result">
<input type="button" value="獲取繪製的覆蓋物個數" onclick="alert(overlays.length)"/>
<input type="button" value="清除所有覆蓋物" onclick="clearAll()"/>
<input type="button" value="獲取多邊形的頂點" onclick="getPoint()"/><br/>
<input type="button" value="開啟線、面編輯功能" onclick="Editing('enable')"/>
<input type="button" value="關閉線、面編輯功能" onclick="Editing('disable')"/>
<input type="button" value="顯示原有多邊形" onclick="showPolygon(this)"/><br/>
<input type="button" value="畫多邊形" onclick="draw(BMAP_DRAWING_POLYGON)"/>
<input type="button" value="畫矩形" onclick="draw(BMAP_DRAWING_RECTANGLE)"/>
<input type="button" value="畫線" onclick="draw(BMAP_DRAWING_POLYLINE)"/>
<span>右鍵獲取任意點的經緯度</span>
</div>
<div id="resultShape">
</div>
<div id="shape">
座標為
</div>
<script type="text/javascript">
function $(id){
return document.getElementById(id);
}
var map = new BMap.Map('map');
var poi = new BMap.Point(113.948913,22.530844);
map.centerAndZoom(poi, 16);
map.enableScrollWheelZoom();
var overlays = [];
var overlaycomplete = function(e){
overlays.push(e.overlay);
};
var styleOptions = {
strokeColor:"red",
fillColor:"red",
strokeWeight: 3,
strokeOpacity: 0.8,
fillOpacity: 0.6,
strokeStyle: 'solid'
}
var drawingManager = new BMapLib.DrawingManager(map, {
isOpen: false,
drawingToolOptions: {
anchor: BMAP_ANCHOR_TOP_RIGHT,
offset: new BMap.Size(5, 5),
},
circleOptions: styleOptions,
polylineOptions: styleOptions,
polygonOptions: styleOptions,
rectangleOptions: styleOptions
});
drawingManager.addEventListener('overlaycomplete', overlaycomplete);
map.addEventListener("rightclick",function(e){
if(confirm(e.point.lng + "," + e.point.lat)){
$("shape").innerHTML=$("shape").innerHTML+" <br/>("+e.point.lng+","+e.point.lat+")";
}
});
function draw(type){
drawingManager.open();
drawingManager.setDrawingMode(type);
}
function clearAll() {
for(var i = 0; i < overlays.length; i++){
map.removeOverlay(overlays[i]);
}
overlays.length = 0
}
function getPoint(){
$("resultShape").innerHTML='';
for(var i = 0; i < overlays.length; i++){
var overlay=overlays[i].getPath();
$("resultShape").innerHTML=$("resultShape").innerHTML+overlay.length+'邊形:<br/>';
for(var j = 0; j < overlay.length; j++){
var grid =overlay[j];
$("resultShape").innerHTML=$("resultShape").innerHTML+(j+1)+"個點:("+grid.lng+","+grid.lat+");<br/>";
}
}
}
function Editing(state){
for(var i = 0; i < overlays.length; i++){
state=='enable'?overlays[i].enableEditing():overlays[i].disableEditing();
}
}
function showPolygon(btn){
var polygon = new BMap.Polygon([
new BMap.Point(113.941853,22.530777),
new BMap.Point(113.940487,22.527789),
new BMap.Point(113.94788,22.527597),
new BMap.Point(113.947925,22.530618)
], styleOptions);
map.addOverlay(polygon);
btn.setAttribute('disabled','false');
showText();
}
function showText(){
var opts = {
position : new BMap.Point(113.941853,22.530777),
offset : new BMap.Size(30, 30)
}
var label = new BMap.Label("不可編輯", opts);
label.setStyle({
color : "black",
fontSize : "16px",
fillColor:"red",
});
map.addOverlay(label);
}
</script>
</body>
</html>
效果如圖: