vue高德地图绘制矢量图形

vue高德地图实现绘制矢量图形功能,需要注册并登录高德地图开发平台申请密钥,然后安装高德地图加载器,这里就不在介绍了,直接看代码。
1、效果图如下:
点击底部按钮实现绘制
在这里插入图片描述

<template>
  <div class="container">
    <div ref="mapContainer" class="map-container"></div>
    <div class="btnList">
      <button class="btn" ="drawPolyline()">绘制线段</button>
      <button class="btn" ="drawPolygon()">绘制多边形</button>
      <button class="btn" ="drawRectangle()">绘制矩形</button>
      <button class="btn" ="drawCircle()">绘制圆形</button>
    </div>
  </div>
</template>

<script>
import AMapLoader from '@amap/amap-jsapi-loader'
window._AMapSecurityConfig = {
  securityJsCode: '你的安全密钥'
}
export default {
  data() {
    return {}
  },
  mounted() {
    this.initMap()
  },
  methods: {
    initMap() {
      AMapLoader.load({
        key: '你的key值',
        version: '2.0', // 指定要加载的 JSAPI 的版本,缺省时默认为 1.4.15
        plugins: ['AMap.AutoComplete', 'AMap.PlaceSearch', 'AMap.MouseTool'] // 需要使用的的插件列表,如比例尺'AMap.Scale'等
      })
        .then((AMap) => {
          // 初始化地图
          var map = new AMap.Map(this.$refs.mapContainer, {
            viewMode: '2D', //  是否为3D地图模式
            zoom: 13, // 初始化地图级别
            center: [103.834228, 36.060798], // 中心点坐标
            resizeEnable: true
          })
          // 绘制矢量图形
          this.mouseTool = new AMap.MouseTool(map)
        })
        .catch((e) => {
          console.log(e)
        })
    },
    // 绘制线段
    drawPolyline() {
      this.mouseTool.polyline({
        strokeColor: '#3366FF',
        strokeOpacity: 1,
        strokeWeight: 6,
        strokeStyle: 'solid'
      })
    },
    // 绘制多边形
    drawPolygon() {
      this.mouseTool.polygon({
        strokeColor: '#FF33FF',
        strokeWeight: 6,
        strokeOpacity: 0.2,
        fillColor: '#1791fc',
        fillOpacity: 0.4,
        strokeStyle: 'solid'
      })
    },
    // drawRectangle
    drawRectangle() {
      this.mouseTool.rectangle({
        strokeColor: 'red',
        strokeOpacity: 0.5,
        strokeWeight: 6,
        fillColor: 'blue',
        fillOpacity: 0.5,
        strokeStyle: 'solid'
      })
    },
    // 绘制圆形
    drawCircle() {
      this.mouseTool.circle({
        strokeColor: '#FF33FF',
        strokeWeight: 6,
        strokeOpacity: 0.2,
        fillColor: '#1791fc',
        fillOpacity: 0.4,
        strokeStyle: 'solid'
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.container {
  width: 100%;
  height: 100%;
  position: relative;
  .map-container {
    width: 100%;
    height: 100%;
  }
  .btnList {
    position: absolute;
    bottom: 5%;
    right: 2%;
    .btn {
      margin: 5px;
    }
  }
}
</style>

Vue 高德地图 API 提供了 `AMap.MouseTool` 类,可以用来绘制矢量图形。下面是一个简单的示例: ``` <template> <div id="map-container" style="height: 500px;"></div> </template> <script> export default { data() { return { map: null, mouseTool: null }; }, mounted() { // 加载地图 this.map = new AMap.Map(&#39;map-container&#39;, { zoom: 13, // 缩放级别 center: [116.397428, 39.90923] // 地图中心点 }); // 创建 MouseTool 实例 this.mouseTool = new AMap.MouseTool(this.map); // 监听绘制事件 AMap.event.addListener(this.mouseTool, &#39;draw&#39;, e => { // 获取绘制矢量图形对象 const shape = e.obj; // 在地图上显示矢量图形 this.map.add(shape); }); }, methods: { // 开始绘制矢量图形 startDraw(type) { this.mouseTool[type](); } } }; </script> ``` 在上面的示例中,我们首先创建了一个地图实例并将其渲染到页面上。然后,我们创建了一个 MouseTool 实例,并在绘制事件中获取绘制矢量图形对象并将其添加到地图上。最后,我们提供了一个 `startDraw` 方法,用于开始绘制不同类型的矢量图形。 可以通过调用 `startDraw` 方法来开始绘制矢量图形。例如,要绘制一个圆形,可以在模板中添加一个按钮并将其与 `startDraw` 方法关联: ``` <template> <div> <div id="map-container" style="height: 500px;"></div> <button @click="startDraw(&#39;circle&#39;)">绘制圆形</button> </div> </template> ``` 这将在地图上启动圆形绘制工具。其他可用的绘制工具包括 `rectangle`(矩形)、`polygon`(多边形)和 `polyline`(折线)。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值