vue-amap区域显示,3d棱柱效果


vue-amap中文文档: https://www.wenjiangs.com/doc/mdxkhhtr
在这里插入图片描述

一、在vue项目中安装和基本使用

1. 安装

npm install vue-amap --save

2. 在vue项目中使用

1. 在main.js中引入

import VueAMap from 'vue-amap';

Vue.use(VueAMap);
VueAMap.initAMapApiLoader({
  key: "你的key",
  plugin: [
    "AMap.Scale",
    "AMap.OverView",
    "AMap.ToolBar",
    "AMap.MapType",
    "AMap.DistrictSearch",
  ],
  v: "1.4.4",
});
window._AMapSecurityConfig = {
  securityJsCode: "你的安全密钥",
};

2. demo显示高德地图

<template>
  <div>
    <div class="amap-page-container">
      <el-amap
        ref="map"
        vid="amapDemo"
        :center="center"
        :zoom="zoom"
        :plugin="plugin"
        :events="events"
        class="amap-demo"
      >
      </el-amap>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      zoom: 12,
      center: [121.59996, 31.197646],
      events: {
        init: (o) => {
          console.log(o.getCenter());
          console.log(this.$refs.map.$$getInstance());
          o.getCity((result) => {
            console.log(result);
          });
        },
        moveend: () => {},
        zoomchange: () => {},
        click: (e) => {
          alert("map clicked");
        },
      },
      plugin: [
        "ToolBar",
        {
          pName: "MapType",
          defaultType: 0,
          events: {
            init(o) {
              console.log(o);
            },
          },
        },
      ],
    };
  },
};
</script>

<style lang="less" scoped>
.amap-page-container {
  width: 100%;
  height: 900px;
  position: relative;

  .amap-demo {
    width: 100%;
    height: 100%;
  }
}
</style>

效果如图:
在这里插入图片描述

二、显示区域

效果如图:
在这里插入图片描述
代码如下:

<template>
  <div>
    <div class="amap-page-container">
      <el-amap
        ref="map"
        vid="amapDemo"
        :center="center"
        :zoom="zoom"
        :plugin="plugin"
        :events="events"
        class="amap-demo"
      >
      </el-amap>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      zoom: 12,
      center: [121.59996, 31.197646],
      events: {
        init: this.initMap,
        moveend: () => {},
        zoomchange: () => {},
        click: (e) => {
          alert("map clicked");
        },
      },
      plugin: [
        "ToolBar",
        {
          pName: "MapType",
          defaultType: 0,
          events: {
            init(o) {
              console.log(o);
            },
          },
        },
      ],
    };
  },
  methods: {
    initMap(map) {
        setTimeout(() => {
            this.drawBounds(map)
        }, 200)
    },
     drawBounds(map) {
            let opts = {
                subdistrict: 0,
                extensions: 'all',
                level: 'city'
            }
            let district = new AMap.DistrictSearch(opts)
            let polygons = []
            district.setLevel('上海市')
            district.search("上海市", (status, result) => {
                map.remove(polygons)
                polygons = [];
                let bounds = result.districtList[0].boundaries;
                if (bounds) {
                    for (let i = 0, l = bounds.length; i < l; i++) {
                        let polygon = new AMap.Polygon({
                            strokeWeight: 3,
                            path: bounds[i],
                            fillOpacity: 0.4,
                            fillColor: '#111e4b',
                            strokeColor: '#ffffff',
                            height: 100,
                            extrusionHeight: 100
                        });
                        polygons.push(polygon);
                    }
                }
                map.add(polygons)
                map.setFitView(polygons);
            })
        },
  }
};
</script>

<style lang="less" scoped>
.amap-page-container {
  width: 100%;
  height: 900px;
  position: relative;

  .amap-demo {
    width: 100%;
    height: 100%;
  }
}
</style>

三、只显示某个区域

效果如图:
在这里插入图片描述
代码如下:

<template>
  <div>
    <div class="amap-page-container">
      <el-amap
        ref="map"
        vid="amapDemo"
        :center="center"
        :zoom="zoom"
        :plugin="plugin"
        :events="events"
        class="amap-demo"
      >
      </el-amap>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      zoom: 12,
      center: [121.59996, 31.197646],
      events: {
        init: this.initMap,
        moveend: () => {},
        zoomchange: () => {},
        click: (e) => {
          alert("map clicked");
        },
      },
      plugin: [
        "ToolBar",
        {
          pName: "MapType",
          defaultType: 0,
          events: {
            init(o) {
              console.log(o);
            },
          },
        },
      ],
    };
  },
  methods: {
    initMap(map) {
        setTimeout(() => {
            this.drawBounds(map)
        }, 200)
    },
     drawBounds(map) {
            let opts = {
                subdistrict: 0,
                extensions: 'all',
                level: 'city'
            }
            let district = new AMap.DistrictSearch(opts)
            let polygons = []
            district.setLevel('上海市')
            district.search("上海市", (status, result) => {
                map.remove(polygons)
                polygons = [];
                let bounds = result.districtList[0].boundaries;

                if (bounds) {
                    for (let i = 0, l = bounds.length; i < l; i++) {
                        let polygon = new AMap.Polygon({
                            strokeWeight: 3,
                            path: bounds[i],
                            fillOpacity: 0.4,
                            fillColor: '#ffffff',
                            strokeColor: '#ffffff',
                        });
                        polygons.push(polygon);
                    }
                }
                map.add(polygons)
                map.setFitView(polygons);

                let outer = [
                    new AMap.LngLat(-360, 90, true),
                    new AMap.LngLat(-360, -90, true),
                    new AMap.LngLat(360, -90, true),
                    new AMap.LngLat(360, 90, true),
                ]
                let holes = result.districtList[0].boundaries
                console.log('holes', holes)
                let pathArray = [
                    outer
                ]
                pathArray.push.apply(pathArray, holes)
                let polygon = new AMap.Polygon({
                    pathL: pathArray,
                    strokeColor: "#ffffff",
                    strokeWeight: 3,
                    strokeOpacity: 1,
                    fillColor: '#031f52',
                    fillOpacity: 1,
                });
                polygon.setPath(pathArray)
                map.add(polygon)
            })
        },
  }
};
</script>

<style lang="less" scoped>
.amap-page-container {
  width: 100%;
  height: 900px;
  position: relative;

  .amap-demo {
    width: 100%;
    height: 100%;
  }
}
</style>

四、3D棱柱区域

效果如图:
在这里插入图片描述
代码如下:

<template>
  <div>
    <div class="amap-page-container">
      <el-amap
        ref="map"
        vid="amapDemo"
        :center="center"
        :zoom="zoom"
        :plugin="plugin"
        :events="events"
        :pitch="pitch"
        viewMode="3D"
        class="amap-demo"
      >
      </el-amap>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      pitch: 60,
      zoom: 12,
      center: [121.59996, 31.197646],
      events: {
        init: this.initMap,
        moveend: () => {},
        zoomchange: () => {},
        click: (e) => {
          alert("map clicked");
        },
      },
      plugin: [
        "ToolBar",
        {
          pName: "MapType",
          defaultType: 0,
          events: {
            init(o) {
              console.log(o);
            },
          },
        },
      ],
    };
  },
  methods: {
    initMap(map) {
      setTimeout(() => {
        this.drawBounds(map);
      }, 200);
    },
    drawBounds(map) {
      map.AmbientLight = new AMap.Lights.AmbientLight([1, 1, 1], 0.5);
      map.DirectionLight = new AMap.Lights.DirectionLight(
        [0, 0, 1],
        [1, 1, 1],
        1
      );
      let object3Dlayer = new AMap.Object3DLayer();
      map.add(object3Dlayer);

      let opts = {
        subdistrict: 0,
        extensions: "all",
        level: "city",
      };
      let district = new AMap.DistrictSearch(opts);
      let polygons = [];
      district.setLevel("上海市");
      district.search("上海市", (status, result) => {
        map.remove(polygons);
        polygons = [];
        let bounds = result.districtList[0].boundaries;

        let height = 50000;
        let color = "#0088ffcc"; // #0088ffcc; #111e4bcc
        let prism = new AMap.Object3D.Prism({
          path: bounds,
          height: height,
          color: color,
        });
        prism.transparent = true;
        object3Dlayer.add(prism);

        if (bounds) {
          for (let i = 0, l = bounds.length; i < l; i++) {
            let polygon = new AMap.Polygon({
              strokeWeight: 3,
              path: bounds[i],
              fillOpacity: 0.4,
              fillColor: "#ffffff",
              strokeColor: "#ffffff",
            });
            polygons.push(polygon);
          }
        }
        map.add(polygons);
        map.setFitView(polygons);

        
      });
    },
  },
};
</script>

<style lang="less" scoped>
.amap-page-container {
  width: 100%;
  height: 900px;
  position: relative;

  .amap-demo {
    width: 100%;
    height: 100%;
  }
}
</style>

五、添加卫星图层

效果如图:
在这里插入图片描述

代码如下:

<template>
  <div>
    <div class="amap-page-container">
      <el-amap
        ref="map"
        vid="amapDemo"
        :center="center"
        :zoom="zoom"
        :plugin="plugin"
        :events="events"
        :pitch="pitch"
        viewMode="3D"
        class="amap-demo"
      >
      </el-amap>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      pitch: 0,
      zoom: 6,
      center: [121.59996, 31.197646],
      events: {
        init: this.initMap,
        moveend: () => {},
        zoomchange: () => {},
        click: (e) => {
          alert("map clicked");
        },
      },
      plugin: [
        "ToolBar",
        {
          pName: "MapType",
          defaultType: 0,
          events: {
            init(o) {
              console.log(o);
            },
          },
        },
      ],
    };
  },
  methods: {
    initMap(map) {
      setTimeout(() => {
        this.setSatelliteLayer();
      }, 200);
    },
    setSatelliteLayer() {
      const tileLayer = new AMap.TileLayer.Satellite({
        map: this.$refs.map.$$getInstance(),
      });
      tileLayer.show();
    },
  },
};
</script>

<style lang="less" scoped>
.amap-page-container {
  width: 100%;
  height: 900px;
  position: relative;

  .amap-demo {
    width: 100%;
    height: 100%;
  }
}
</style>

  • 20
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
如果你使用的是 vue-amap SDK,那么可以通过以下步骤添加定位按钮: 1. 在 Vue 组件的 `mounted` 钩子函数中,调用 `this.$amap.service('AMap.Geolocation')` 创建定位服务对象。 2. 调用定位服务对象的 `getCurrentPosition` 方法获取当前位置,并将 `showButton` 参数设置为 `true` 打开定位按钮。 下是一个示例代码: ```javascript mounted() { // 创建定位服务对象 this.geolocation = this.$amap.service('AMap.Geolocation', {}); // 获取当前位置,并打开定位按钮 this.geolocation.getCurrentPosition((status, result) => { if (status === 'complete') { this.$amap.plugin('AMap.Geolocation', () => { const geolocation = new AMap.Geolocation({ enableHighAccuracy: true, // 是否使用高精度定位,默认为 true showButton: true, // 是否显示定位按钮,默认为 false buttonPosition: 'LB', // 定位按钮的位置,可以设置 'LT'、'LB'、'RT'、'RB',默认为 'LB' buttonOffset: new AMap.Pixel(10, 20), // 定位按钮距离地图边缘的偏移量,默认为 AMap.Pixel(10, 20) zoomToAccuracy: true, // 定位成功后是否自动调整地图视野到定位点,默认为 true }); this.$map.addControl(geolocation); }); } }); }, ``` 在上的代码中,我们首先创建了定位服务对象 `this.geolocation`,然后在 `getCurrentPosition` 方法中获取当前位置,并调用 `this.$amap.plugin` 方法添加定位控件,并将 `showButton` 参数设置为 `true` 打开定位按钮。 注意:为了使用 `this.$amap.plugin` 方法添加定位控件,需要先在 Vue 组件的 `mounted` 钩子函数中调用 `this.$amap.plugin('AMap.Geolocation')` 加载定位插件。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

jieyucx

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值