123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287 |
- <!doctype html>
- <html>
- <head>
- <meta charset="utf-8">
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
- <meta name="viewport" content="initial-scale=1.0, user-scalable=no, width=device-width">
- <link rel="stylesheet" href="https://a.amap.com/jsapi_demos/static/demo-center/css/demo-center.css"/>
- <title>Object3D 的添加/移除</title>
- <meta name="description" content="对 Object3D 对象进行添加和移除的基本操作。">
- <style>
- html,
- body,
- #container {
- width: 100%;
- height: 100%;
- }
- </style>
- </head>
- <body>
- <div id="container"></div>
- <!--<div class="input-card" style="width: auto;">-->
- <!--<div class="input-item">-->
- <!--<button id="add-obj3d-btn" class="btn">添加 Object3D</button>-->
- <!--</div>-->
- <!--<div class="input-item">-->
- <!--<button id="rm-obj3d-btn" class="btn">移除 Object3D</button>-->
- <!--</div>-->
- <!--</div>-->
- <!-- 加载地图JSAPI脚本 -->
- <script src="https://webapi.amap.com/maps?v=1.4.14&key=a9575a60476c3c07963ea2e3c8a9b03d&plugin=Map3D,AMap.DistrictSearch"></script>
- <script>
- //
- // var map = new AMap.Map('container', {
- // center: [109.945017,27.402736],
- // viewMode: '3D',
- // labelzIndex: 130,
- // pitch: 40,
- // zoom: 9
- // });
- //创建 3D 底图
- var map = new AMap.Map('container', {
- viewMode: '3D', // 开启 3D 模式
- pitch: 75,
- rotation: 35,
- // center: [116.39756, 39.904215],
- center: [109.93386,27.395993],
- features: ['bg', 'road'],
- zoom: 15
- });
- //**************
- map.plugin(["AMap.ControlBar"],function(){
- var controlBar = new AMap.ControlBar(109.945017,27.402736)
- map.addControl(controlBar)
- });
- var t= new AMap.Text({
- text:"中方县第一中学",
- position:[109.946506,27.40],
- height:2000,
- verticalAlign:'bottom',
- map:map,
- style:{
- 'background-color':'#40E0D0',
- 'color':'white',
- 'font-size':'12px'
- }
- });
- var t1= new AMap.Text({
- text:"湖南骏泰科技消防队",
- position:[109.923688,27.387485],
- height:500,
- verticalAlign:'bottom',
- map:map,
- style:{
- 'background-color':'#CD5C5C',
- 'color':'white',
- 'font-size':'12px'
- }
- });
- var t2= new AMap.Text({
- text:"红宝科技汽柴油储罐",
- position:[109.931149,27.378376],
- height:1000,
- verticalAlign:'bottom',
- map:map,
- style:{
- 'background-color':'#778899',
- 'color':'white',
- 'font-size':'12px'
- }
- });
- //**************
- // // 添加 Object3DLayer 图层,用于添加 3DObject 对象
- // var object3Dlayer = new AMap.Object3DLayer();
- // map.add(object3Dlayer);
- //
- //
- //
- var object3Dlayer = new AMap.Object3DLayer({ zIndex: 1 });
- map.add(object3Dlayer);
- // 以不规则棱柱体 Prism 为例,添加至 3DObjectLayer 图层中
- var paths = [
- [109.945017,27.402736],
- [109.947506,27.402526],
- [109.947785,27.398868],
- // [109.946777,27.397154],
- [109.945339,27.398487]
- ];
- var bounds = paths.map(function(path) {
- return new AMap.LngLat(path[0], path[1]);
- });
- // 创建 Object3D 对象
- var prism = new AMap.Object3D.Prism({
- path: bounds,
- height: 5000,
- color: 'rgba(100, 150, 230, 0.7)' // 支持 #RRGGBB、rgb()、rgba() 格式数据
- });
- // 开启透明度支持
- prism.transparent = true;
- // 添加至 3D 图层
- object3Dlayer.add(prism);
- //消防队
- var paths1 = [
- [109.919783,27.392553],
- [109.929568,27.388971],
- [109.928023,27.381769],
- [109.918367,27.384284],
- [109.918023,27.388933]
- ];
- var bounds1 = paths1.map(function(path) {
- return new AMap.LngLat(path[0], path[1]);
- });
- var prism1 = new AMap.Object3D.Prism({
- path: bounds1,
- height: 1000,
- color: 'rgba(100, 150, 230, 0.7)' // 支持 #RRGGBB、rgb()、rgba() 格式数据
- });
- object3Dlayer.add(prism1);
- //油罐
- var paths2 = [
- [109.929776,27.380243],
- [109.932909,27.379214],
- [109.931836,27.377232],
- [109.929733,27.377766]
- ];
- var bounds2 = paths2.map(function(path) {
- return new AMap.LngLat(path[0], path[1]);
- });
- var prism2 = new AMap.Object3D.Prism({
- path: bounds2,
- height: 3000,
- color: 'rgba(131, 112, 205, 0.7)' // 支持 #RRGGBB、rgb()、rgba() 格式数据
- });
- object3Dlayer.add(prism2);
- // //绑定按钮事件
- // document.querySelector("#add-obj3d-btn").onclick = function() {
- // // 添加至 3D 图层
- // object3Dlayer.add(prism);
- // };
- //
- // document.querySelector("#rm-obj3d-btn").onclick = function() {
- // // 从 3D 图层中移除
- // object3Dlayer.remove(prism);
- // };
- var opts = {
- subdistrict: 0,
- extensions: 'all',
- level: 'district'
- };
- //利用行政区查询获取边界
- var district = new AMap.DistrictSearch(opts);
- district.search('中方县', function (status, result) {
- var bounds = result.districtList[0].boundaries;
- var height = 3000;
- var color = '#0088ffcc';//rgba
- var wall = new AMap.Object3D.Wall({
- path: bounds,
- height: height,
- color: color
- });
- wall.backOrFront = 'both';
- wall.transparent = true;
- object3Dlayer.add(wall);
- });
- var addBox = function (center, length, width, height) {
- var box = new AMap.Object3D.MeshAcceptLights();
- var geometry = box.geometry;
- box.textures.push('https://a.amap.com/jsapi_demos/static/texture3d/shuilifang.png');
- var halfLength = length / 2;
- var halfWidth = width / 2;
- var path = [
- [-halfLength, halfWidth],
- [halfLength, halfWidth],
- [halfLength, -halfWidth],
- [-halfLength, -halfWidth]
- ];
- //构造4个侧面
- for (var i = 0; i < 4; i += 1) {
- var x1 = path[i][0];
- var y1 = path[i][1];
- var x2 = path[(i + 1) % 4][0];
- var y2 = path[(i + 1) % 4][1];
- // 每个侧面四个顶点
- geometry.vertices.push(x1, y1, 0);
- geometry.vertices.push(x1, y1, -height);
- geometry.vertices.push(x2, y2, 0);
- geometry.vertices.push(x2, y2, -height);
- var v1 = new AMap.Vector3(0, 0, -height);
- var v2 = new AMap.Vector3(x2 - x1, y2 - y1, 0);
- // 各面的法向量
- var normal = new AMap.Vector3().crossVectors(v2, v1).normalize().elements;
- geometry.vertexNormals.push(normal[0], normal[1], normal[2]);
- geometry.vertexNormals.push(normal[0], normal[1], normal[2]);
- geometry.vertexNormals.push(normal[0], normal[1], normal[2]);
- geometry.vertexNormals.push(normal[0], normal[1], normal[2]);
- // 四个顶点的纹理坐标
- geometry.vertexUVs.push(0, 1);
- geometry.vertexUVs.push(0, 0);
- geometry.vertexUVs.push(1, 1);
- geometry.vertexUVs.push(1, 0);
- var bottomIndex = i * 4;
- var topIndex = bottomIndex + 1;
- var nextBottomIndex = (bottomIndex + 2);
- var nextTopIndex = (bottomIndex + 3);
- // 侧面三角形1
- geometry.faces.push(topIndex, bottomIndex, nextBottomIndex);
- // 侧面三角形2
- geometry.faces.push(topIndex, nextBottomIndex, nextTopIndex);
- }
- //构造顶面
- for (var i = 0; i < 4; i += 1) {
- geometry.vertices.push(path[i][0], path[i][1], -height);
- geometry.vertexNormals.push(0, 0, -1);
- }
- // 四个顶点的纹理坐标
- geometry.vertexUVs.push(0, 2);
- geometry.vertexUVs.push(1, 2);
- geometry.vertexUVs.push(1, 0);
- geometry.vertexUVs.push(0, 0);
- // 侧面三角形1
- geometry.faces.push(16 + 0, 16 + 1, 16 + 2);
- // 侧面三角形2
- geometry.faces.push(16 + 0, 16 + 2, 16 + 3);
- box.position(center);
- // 如果使用了透明颜色,请设置true
- box.transparent = true;
- object3Dlayer.add(box);
- };
- addBox([109.929776,27.380243], 1600, 1600, 800);
- // alert(map.getCenter())
- </script>
- </body>
- </html>
|