高德地图 绘制 柱状图形

要注意清空柱状图形

<template>
  <div class="contentBox">
    <div id="map"></div>
  </div>
</template>
<script>
import AMap from 'AMap'

export default {
  data() {
    return {
      object3Dlayer: null,//柱状图
    }
  },
  methods: {
    setHomeMap(mapData, code) {
      //柱状图颜色设置
      const prismData = {
        height: 500,
        radius: 100,
        topColor: [1, 0.8, 0.4, 0.8],
        topFaceColor: [1, 0.8, 0.5, 0.8],//顶部
        bottomColor: [1, 0.72, 0, 0.8]
      };
      this.object3Dlayer = new AMap.Object3DLayer();
      this.map.add(this.object3Dlayer);
      this.offset = new AMap.Pixel(0, 0);

      //柱状图
      let center_3d = this.map.lngLatToGeodeticCoord('中心点座标');
      this._createCylinder(center_3d, prismData, this.object3Dlayer);
      
    },    
    initMap() {
      if (!this.map) {
        this.map = new AMap.Map('map', {
          viewMode: '3D',
          pitch: 50,
          resizeEnable: true, 
          zoom: 15.85, 
          center: [121.536233, 31.172555], 
        });
        this.map.setMapStyle("amap://styles/blue")
      }

    },
    _createCylinder(center, prismData, object3Dlayer) {
      //圆柱设置
      let segment = 32;//棱柱的形状,圆
      let topColor = prismData.topColor;
      let topFaceColor = prismData.topFaceColor;
      let bottomColor = prismData.bottomColor;
      // let height = prismData.height;
      let setRadius = prismData.radius;
      this.cylinder = new AMap.Object3D.Mesh();
      let geometry = this.cylinder.geometry;
      let verticesLength = segment * 2;
      let path = [];
      for (let i = 0; i < segment; i += 1) {
        let angle = 2 * Math.PI * i / segment;
        let x = center.x + Math.cos(angle) * setRadius;
        let y = center.y + Math.sin(angle) * setRadius;
        path.push([x, y]);
        geometry.vertices.push(x, y, 0); //底部顶点
        geometry.vertices.push(x, y, -prismData.height); //顶部顶点

        geometry.vertexColors.push.apply(geometry.vertexColors, bottomColor); //底部颜色
        geometry.vertexColors.push.apply(geometry.vertexColors, topColor); //顶部颜色

        let bottomIndex = i * 2;
        let topIndex = bottomIndex + 1;
        let nextBottomIndex = (bottomIndex + 2) % verticesLength;
        let nextTopIndex = (bottomIndex + 3) % verticesLength;

        geometry.faces.push(bottomIndex, topIndex, nextTopIndex); //侧面三角形1
        geometry.faces.push(bottomIndex, nextTopIndex, nextBottomIndex); //侧面三角形2
      }
      // 构建顶面三角形,为了区分顶面点和侧面点使用不一样的颜色,所以需要独立的顶点
      for (let i = 0; i < segment; i += 1) {
        geometry.vertices.push.apply(geometry.vertices, geometry.vertices.slice(i * 6 + 3, i * 6 + 6)); //底部顶点
        geometry.vertexColors.push.apply(geometry.vertexColors, topFaceColor);
      }
      let triangles = AMap.GeometryUtil.triangulateShape(path);
      let offset = segment * 2;
      for (let v = 0; v < triangles.length; v += 3) {
        geometry.faces.push(triangles[v] + offset, triangles[v + 2] + offset, triangles[v + 1] + offset);
      }
      this.cylinder.transparent = true; // 如果使用了透明颜色,请设置true
      object3Dlayer.add(this.cylinder);
    },
  },
  },
  mounted() {
    this.initMap();
  }
}
</script>


 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章