百度地圖(41)-GL 地圖工具


 

1. 測量工具

 1 var myDis;
 2 
 3 function addDistanceTool(){
 4 
 5   map.centerAndZoom(new BMapGL.Point(116.404, 39.915), 11);      // 初始化地圖,設置中心點坐標和地圖級別
 6 
 7   myDis = new BMapGLLib.DistanceTool(map);
 8 
 9 // 監聽測距過程中的鼠標事件
10   myDis.addEventListener('drawend', function(e) {
11     console.log("drawend");
12     console.log(e.points);
13     console.log(e.overlays);
14     console.log(e.distance);
15   });
16   myDis.addEventListener('addPoint',function (e) {
17     console.log("addPoint");
18     console.log(e.point);
19     console.log(e.pixel);
20     console.log(e.index);
21     console.log(e.distance)
22   });
23 
24   myDis.addEventListener("removepolyline",function (e) {
25     console.log("removepolyline");
26     console.log(e);
27   });
28 }
29 
30 
31 function openMapDis(){
32   addDistanceTool();
33   myDis.open();
34 }
35 
36 function closeMapDis() {
37   if (myDis)
38     myDis.close();
39 }

 

2. 繪制工具

 1 function addDrawManager() {
 2   var styleOptions = {
 3     strokeColor: '#5E87DB',   // 邊線顏色
 4     fillColor: '#5E87DB',     // 填充顏色。當參數為空時,圓形沒有填充顏色
 5     strokeWeight: 2,          // 邊線寬度,以像素為單位
 6     strokeOpacity: 1,         // 邊線透明度,取值范圍0-1
 7     fillOpacity: 0.2          // 填充透明度,取值范圍0-1
 8   };
 9   var labelOptions = {
10     borderRadius: '2px',
11     background: '#FFFBCC',
12     border: '1px solid #E1E1E1',
13     color: '#703A04',
14     fontSize: '12px',
15     letterSpacing: '0',
16     padding: '5px'
17   };
18 
19   // 實例化鼠標繪制工具
20   var drawingManager = new BMapGLLib.DrawingManager(map, {
21     // isOpen: true,        // 是否開啟繪制模式
22     enableCalculate: false, // 繪制是否進行測距測面
23     enableSorption: true,   // 是否開啟邊界吸附功能
24     sorptiondistance: 20,   // 邊界吸附距離
25     circleOptions: styleOptions,     // 圓的樣式
26     polylineOptions: styleOptions,   // 線的樣式
27     polygonOptions: styleOptions,    // 多邊形的樣式
28     rectangleOptions: styleOptions,  // 矩形的樣式
29     labelOptions: labelOptions,      // label樣式
30   });
31 
32   function draw(e) {
33     var arr = document.getElementsByClassName('bmap-btn');
34     for(var i = 0; i<arr.length; i++) {
35       arr[i].style.backgroundPositionY = '0';
36     }
37     e.style.backgroundPositionY = '-52px';
38     switch(e.id) {
39       case 'marker': {
40         var drawingType = BMAP_DRAWING_MARKER;
41         break;
42       }
43       case 'polyline': {
44         var drawingType = BMAP_DRAWING_POLYLINE;
45         break;
46       }
47       case 'rectangle': {
48         var drawingType = BMAP_DRAWING_RECTANGLE;
49         break;
50       }
51       case 'polygon': {
52         var drawingType = BMAP_DRAWING_POLYGON;
53         break;
54       }
55       case 'circle': {
56         var drawingType = BMAP_DRAWING_CIRCLE;
57         break;
58       }
59     }
60     // 進行繪制
61     if (drawingManager._isOpen && drawingManager.getDrawingMode() === drawingType) {
62       drawingManager.close();
63     } else {
64       drawingManager.setDrawingMode(drawingType);
65       drawingManager.open();
66     }
67   };
68 }

 

3. 使用繪制工具制作的測量工具

 1 /**
 2  * 面積測量
 3  */
 4 function addAreaMeasure() {
 5   var styleOptions = {
 6     strokeColor: '#5E87DB',   // 邊線顏色
 7     fillColor: '#5E87DB',     // 填充顏色。當參數為空時,圓形沒有填充顏色
 8     strokeWeight: 2,          // 邊線寬度,以像素為單位
 9     strokeOpacity: 1,         // 邊線透明度,取值范圍0-1
10     fillOpacity: 0.2          // 填充透明度,取值范圍0-1
11   };
12   var labelOptions = {
13     borderRadius: '2px',
14     background: '#FFFBCC',
15     border: '1px solid #E1E1E1',
16     color: '#703A04',
17     fontSize: '12px',
18     letterSpacing: '0',
19     padding: '5px'
20   };
21 
22 // 實例化鼠標繪制工具
23   var drawingManager = new BMapGLLib.DrawingManager(map, {
24     enableCalculate: true,  // 繪制是否進行測距測面
25     enableSorption: true,   // 是否開啟邊界吸附功能
26     sorptiondistance: 20,   // 邊界吸附距離
27     enableGpc: true,        // 是否開啟延邊裁剪功能
28     enableLimit: true,      // 是否開啟超限提示
29     limitOptions: {
30       area: 50000000,     // 面積超限值
31       distance: 30000     // 距離超限值
32     },
33     circleOptions: styleOptions,     // 圓的樣式
34     polylineOptions: styleOptions,   // 線的樣式
35     polygonOptions: styleOptions,    // 多邊形的樣式
36     rectangleOptions: styleOptions,  // 矩形的樣式
37     labelOptions: labelOptions,      // label樣式
38   });
39 
40   function draw(e) {
41     var arr = document.getElementsByClassName('bmap-btn');
42     for(var i = 0; i<arr.length; i++) {
43       arr[i].style.backgroundPositionY = '0';
44     }
45     e.style.backgroundPositionY = '-52px';
46     switch(e.id) {
47       case 'marker': {
48         var drawingType = BMAP_DRAWING_MARKER;
49         break;
50       }
51       case 'polyline': {
52         var drawingType = BMAP_DRAWING_POLYLINE;
53         break;
54       }
55       case 'rectangle': {
56         var drawingType = BMAP_DRAWING_RECTANGLE;
57         break;
58       }
59       case 'polygon': {
60         var drawingType = BMAP_DRAWING_POLYGON;
61         break;
62       }
63       case 'circle': {
64         var drawingType = BMAP_DRAWING_CIRCLE;
65         break;
66       }
67     }
68     // 進行繪制
69     if (drawingManager._isOpen && drawingManager.getDrawingMode() === drawingType) {
70       drawingManager.close();
71     } else {
72       drawingManager.setDrawingMode(drawingType);
73       drawingManager.open();
74     }
75   };
76 
77 // 繪制完成后獲取相關的信息(面積等)
78   drawingManager.addEventListener('overlaycomplete', function(e) {
79     alert(e.calculate);
80   });
81 }

 

4. 其中 2 和 3 都是使用了  BMapGLLib.DrawingManager 的方式。更多請參考:

https://github.com/huiyan-fe/BMapGLLib

 

5.  頁面顯示:

 

 

6. 源碼地址

https://github.com/WhatGIS/bdMap


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM