1. 程式人生 > >在百度地圖自由畫線---利用拆線-自定義可編輯路線

在百度地圖自由畫線---利用拆線-自定義可編輯路線

<!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 name="keywords" content="百度地圖,百度地圖API,百度地圖自定義工具,百度地圖所見即所得工具" />
<meta name="description" content="百度地圖API自定義地圖,幫助使用者在視覺化操作下生成百度地圖" />
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>自定義可編輯路線</title>
<script type="text/javascript" src="
http://api.map.baidu.com/api?key=46ce9d0614bf7aefe0ba562f8cf87194&v=1.1&services=true
">
</script>
</head>
<body>
<div style="width:520px;height:340px;border:1px solid gray" id="container">
</div>
</body>
<script type="text/javascript">
    var map = new BMap.Map("container");
    map.centerAndZoom(new BMap.Point(116.404, 39.915), 18);
    var e1, e2;
    var p = []; //用來儲存折線的點
    var polyline;
    var doneDraw = 0; //判斷是否繪製折線結束
    map.addEventListener("click", function (e1) { //當滑鼠單擊時
        if (doneDraw == 0) { //判斷是否繪製曲線完畢
            p.push(new BMap.Point(e1.point.lng, e1.point.lat)) //儲存曲線上每個點的經緯度
            if (polyline) { polyline.setPoints(p); } //如果曲線存在,則獲取折線上的點
            else { polyline = new BMap.Polyline(p); } //如果折線不存在,就增加此點
            if (p.length < 2) { return; } //當折線上的點只有一個時,不繪製
            map.addOverlay(polyline); //繪製曲線
        }
    });
    map.addEventListener("dblclick", function (e2) { //當滑鼠雙擊時:結束繪製,並可以編輯曲線
        alert("繪製完成");
        doneDraw = 10;
        polyline.enableEditing();
    });
</script>
</html>