a.html 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. <!doctype html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="initial-scale=1.0, user-scalable=no, width=device-width">
  7. <link rel="stylesheet" href="https://a.amap.com/jsapi_demos/static/demo-center/css/demo-center.css"/>
  8. <title>Object3D 的添加/移除</title>
  9. <meta name="description" content="对 Object3D 对象进行添加和移除的基本操作。">
  10. <style>
  11. html,
  12. body,
  13. #container {
  14. width: 100%;
  15. height: 100%;
  16. }
  17. </style>
  18. </head>
  19. <body>
  20. <div id="container"></div>
  21. <!--<div class="input-card" style="width: auto;">-->
  22. <!--<div class="input-item">-->
  23. <!--<button id="add-obj3d-btn" class="btn">添加 Object3D</button>-->
  24. <!--</div>-->
  25. <!--<div class="input-item">-->
  26. <!--<button id="rm-obj3d-btn" class="btn">移除 Object3D</button>-->
  27. <!--</div>-->
  28. <!--</div>-->
  29. <!-- 加载地图JSAPI脚本 -->
  30. <script src="https://webapi.amap.com/maps?v=1.4.14&key=a9575a60476c3c07963ea2e3c8a9b03d&plugin=Map3D,AMap.DistrictSearch"></script>
  31. <script>
  32. //
  33. // var map = new AMap.Map('container', {
  34. // center: [109.945017,27.402736],
  35. // viewMode: '3D',
  36. // labelzIndex: 130,
  37. // pitch: 40,
  38. // zoom: 9
  39. // });
  40. //创建 3D 底图
  41. var map = new AMap.Map('container', {
  42. viewMode: '3D', // 开启 3D 模式
  43. pitch: 75,
  44. rotation: 35,
  45. // center: [116.39756, 39.904215],
  46. center: [109.93386,27.395993],
  47. features: ['bg', 'road'],
  48. zoom: 15
  49. });
  50. //**************
  51. map.plugin(["AMap.ControlBar"],function(){
  52. var controlBar = new AMap.ControlBar(109.945017,27.402736)
  53. map.addControl(controlBar)
  54. });
  55. var t= new AMap.Text({
  56. text:"中方县第一中学",
  57. position:[109.946506,27.40],
  58. height:2000,
  59. verticalAlign:'bottom',
  60. map:map,
  61. style:{
  62. 'background-color':'#40E0D0',
  63. 'color':'white',
  64. 'font-size':'12px'
  65. }
  66. });
  67. var t1= new AMap.Text({
  68. text:"湖南骏泰科技消防队",
  69. position:[109.923688,27.387485],
  70. height:500,
  71. verticalAlign:'bottom',
  72. map:map,
  73. style:{
  74. 'background-color':'#CD5C5C',
  75. 'color':'white',
  76. 'font-size':'12px'
  77. }
  78. });
  79. var t2= new AMap.Text({
  80. text:"红宝科技汽柴油储罐",
  81. position:[109.931149,27.378376],
  82. height:1000,
  83. verticalAlign:'bottom',
  84. map:map,
  85. style:{
  86. 'background-color':'#778899',
  87. 'color':'white',
  88. 'font-size':'12px'
  89. }
  90. });
  91. //**************
  92. // // 添加 Object3DLayer 图层,用于添加 3DObject 对象
  93. // var object3Dlayer = new AMap.Object3DLayer();
  94. // map.add(object3Dlayer);
  95. //
  96. //
  97. //
  98. var object3Dlayer = new AMap.Object3DLayer({ zIndex: 1 });
  99. map.add(object3Dlayer);
  100. // 以不规则棱柱体 Prism 为例,添加至 3DObjectLayer 图层中
  101. var paths = [
  102. [109.945017,27.402736],
  103. [109.947506,27.402526],
  104. [109.947785,27.398868],
  105. // [109.946777,27.397154],
  106. [109.945339,27.398487]
  107. ];
  108. var bounds = paths.map(function(path) {
  109. return new AMap.LngLat(path[0], path[1]);
  110. });
  111. // 创建 Object3D 对象
  112. var prism = new AMap.Object3D.Prism({
  113. path: bounds,
  114. height: 5000,
  115. color: 'rgba(100, 150, 230, 0.7)' // 支持 #RRGGBB、rgb()、rgba() 格式数据
  116. });
  117. // 开启透明度支持
  118. prism.transparent = true;
  119. // 添加至 3D 图层
  120. object3Dlayer.add(prism);
  121. //消防队
  122. var paths1 = [
  123. [109.919783,27.392553],
  124. [109.929568,27.388971],
  125. [109.928023,27.381769],
  126. [109.918367,27.384284],
  127. [109.918023,27.388933]
  128. ];
  129. var bounds1 = paths1.map(function(path) {
  130. return new AMap.LngLat(path[0], path[1]);
  131. });
  132. var prism1 = new AMap.Object3D.Prism({
  133. path: bounds1,
  134. height: 1000,
  135. color: 'rgba(100, 150, 230, 0.7)' // 支持 #RRGGBB、rgb()、rgba() 格式数据
  136. });
  137. object3Dlayer.add(prism1);
  138. //油罐
  139. var paths2 = [
  140. [109.929776,27.380243],
  141. [109.932909,27.379214],
  142. [109.931836,27.377232],
  143. [109.929733,27.377766]
  144. ];
  145. var bounds2 = paths2.map(function(path) {
  146. return new AMap.LngLat(path[0], path[1]);
  147. });
  148. var prism2 = new AMap.Object3D.Prism({
  149. path: bounds2,
  150. height: 3000,
  151. color: 'rgba(131, 112, 205, 0.7)' // 支持 #RRGGBB、rgb()、rgba() 格式数据
  152. });
  153. object3Dlayer.add(prism2);
  154. // //绑定按钮事件
  155. // document.querySelector("#add-obj3d-btn").onclick = function() {
  156. // // 添加至 3D 图层
  157. // object3Dlayer.add(prism);
  158. // };
  159. //
  160. // document.querySelector("#rm-obj3d-btn").onclick = function() {
  161. // // 从 3D 图层中移除
  162. // object3Dlayer.remove(prism);
  163. // };
  164. var opts = {
  165. subdistrict: 0,
  166. extensions: 'all',
  167. level: 'district'
  168. };
  169. //利用行政区查询获取边界
  170. var district = new AMap.DistrictSearch(opts);
  171. district.search('中方县', function (status, result) {
  172. var bounds = result.districtList[0].boundaries;
  173. var height = 3000;
  174. var color = '#0088ffcc';//rgba
  175. var wall = new AMap.Object3D.Wall({
  176. path: bounds,
  177. height: height,
  178. color: color
  179. });
  180. wall.backOrFront = 'both';
  181. wall.transparent = true;
  182. object3Dlayer.add(wall);
  183. });
  184. var addBox = function (center, length, width, height) {
  185. var box = new AMap.Object3D.MeshAcceptLights();
  186. var geometry = box.geometry;
  187. box.textures.push('https://a.amap.com/jsapi_demos/static/texture3d/shuilifang.png');
  188. var halfLength = length / 2;
  189. var halfWidth = width / 2;
  190. var path = [
  191. [-halfLength, halfWidth],
  192. [halfLength, halfWidth],
  193. [halfLength, -halfWidth],
  194. [-halfLength, -halfWidth]
  195. ];
  196. //构造4个侧面
  197. for (var i = 0; i < 4; i += 1) {
  198. var x1 = path[i][0];
  199. var y1 = path[i][1];
  200. var x2 = path[(i + 1) % 4][0];
  201. var y2 = path[(i + 1) % 4][1];
  202. // 每个侧面四个顶点
  203. geometry.vertices.push(x1, y1, 0);
  204. geometry.vertices.push(x1, y1, -height);
  205. geometry.vertices.push(x2, y2, 0);
  206. geometry.vertices.push(x2, y2, -height);
  207. var v1 = new AMap.Vector3(0, 0, -height);
  208. var v2 = new AMap.Vector3(x2 - x1, y2 - y1, 0);
  209. // 各面的法向量
  210. var normal = new AMap.Vector3().crossVectors(v2, v1).normalize().elements;
  211. geometry.vertexNormals.push(normal[0], normal[1], normal[2]);
  212. geometry.vertexNormals.push(normal[0], normal[1], normal[2]);
  213. geometry.vertexNormals.push(normal[0], normal[1], normal[2]);
  214. geometry.vertexNormals.push(normal[0], normal[1], normal[2]);
  215. // 四个顶点的纹理坐标
  216. geometry.vertexUVs.push(0, 1);
  217. geometry.vertexUVs.push(0, 0);
  218. geometry.vertexUVs.push(1, 1);
  219. geometry.vertexUVs.push(1, 0);
  220. var bottomIndex = i * 4;
  221. var topIndex = bottomIndex + 1;
  222. var nextBottomIndex = (bottomIndex + 2);
  223. var nextTopIndex = (bottomIndex + 3);
  224. // 侧面三角形1
  225. geometry.faces.push(topIndex, bottomIndex, nextBottomIndex);
  226. // 侧面三角形2
  227. geometry.faces.push(topIndex, nextBottomIndex, nextTopIndex);
  228. }
  229. //构造顶面
  230. for (var i = 0; i < 4; i += 1) {
  231. geometry.vertices.push(path[i][0], path[i][1], -height);
  232. geometry.vertexNormals.push(0, 0, -1);
  233. }
  234. // 四个顶点的纹理坐标
  235. geometry.vertexUVs.push(0, 2);
  236. geometry.vertexUVs.push(1, 2);
  237. geometry.vertexUVs.push(1, 0);
  238. geometry.vertexUVs.push(0, 0);
  239. // 侧面三角形1
  240. geometry.faces.push(16 + 0, 16 + 1, 16 + 2);
  241. // 侧面三角形2
  242. geometry.faces.push(16 + 0, 16 + 2, 16 + 3);
  243. box.position(center);
  244. // 如果使用了透明颜色,请设置true
  245. box.transparent = true;
  246. object3Dlayer.add(box);
  247. };
  248. addBox([109.929776,27.380243], 1600, 1600, 800);
  249. // alert(map.getCenter())
  250. </script>
  251. </body>
  252. </html>