百度地圖(22)-GL 添加地圖控件


1. 百度地圖GLAPI提供了的Control,比二維的控件少的多,主要是

NavigationControl3D,ScaleControl,ZoomControl , CityListControl,LocationControl, CopyrightControl 。

 

2. 其主要用法和二維控件相似,初始化后直接添加。

 1 /**
 2  * 添加定位控件
 3  * @type {BMapGL.LocationControl}
 4  */
 5 function addLocationControl() {
 6 
 7   var locationControl = new BMapGL.LocationControl({
 8     anchor:BMAP_ANCHOR_TOP_RIGHT,
 9     offset: new BMapGL.Size(20,20)
10   });
11   map.addControl(locationControl);
12 
13   locationControl.addEventListener("locationSuccess",function (e) {
14     var address = "";
15     address += e.addressComponent.province;
16     address += e.addressComponent.city;
17     address += e.addressComponent.district;
18     address += e.addressComponent.street;
19     address += e.addressComponent.streetNumber;
20     console.log("當前定位地址:"+address);
21   });
22 
23   locationControl.addEventListener("locationError",function (e) {
24     console.log("定位錯誤:"+ e.message);
25   });
26 }
27 
28 /**
29  * 添加自定義版權控件
30  */
31 function addCopyRightControl() {
32   var cr = new BMapGL.CopyrightControl({
33     anchor: BMAP_ANCHOR_TOP_RIGHT,
34     offset: new BMapGL.Size(20, 20)
35   });   //設置版權控件位置
36   map.addControl(cr); //添加版權控件
37   var bs = map.getBounds();   //返回地圖可視區域
38   cr.addCopyright({
39     id: 1,
40     content: "<img src='../img/baidu.jpg' width='50px' height='50px'/><a href='#' style='font-size:16px;color:#000'>@安徽舜禹水務股份有限公司</a>",
41     bounds: bs
42   });
43 }
44 
45 /**
46  * 城市列表
47  * @type {BMapGL.CityListControl}
48  */
49 function addCityListControl() {
50   var cityControl = new BMapGL.CityListControl({
51     anchor: BMAP_ANCHOR_TOP_LEFT,
52     offset: new BMapGL.Size(10,5)
53   });
54   map.addControl(cityControl);
55 }
56 
57 
58 /**
59  *  添加地圖控件
60  **/
61 function addMapControls() {
62 
63   //添加地圖導航控件
64   map.addControl(new BMapGL.NavigationControl3D());
65 
66   //添加地圖比例尺控件
67   /**
68    * 比例尺工具與版本標識工具所用同一個class,anchorBL, 所以,當采用CSS方式 display:none 去掉左下角
69    * 的百度標識和版權標識時,縮放比例工具也會被隱藏。
70    **/
71   map.addControl(new BMapGL.ScaleControl());
72 
73   map.addControl(new BMapGL.ZoomControl());
74 
75   addCityListControl();
76 
77   addLocationControl();
78 
79 }

 

4. ScaleControl 的樣式與左下角百度的logo使用的同一個css,為了隱藏logo把ScaleControl也隱藏了。

 

5.. 頁面顯示

 

 

 6. 代碼參考

https://github.com/WhatGIS/bdMap

 


免責聲明!

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



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