记录一次成功的vue调用高德地图

无需安装任何东西,无需调用任何模块,只需联网

<template>

  <div class="bbox">

    <div id='container'></div>

    <div class='info'>操作说明:圆和矩形通过拖拽来绘制,其他覆盖物通过点击来绘制</div>

    <div class="input-card" style='width: 24rem;'>

        <div class="input-item">

          <input type="radio" name='func' checked="" value='marker'><span class="input-text">画点</span>

          <input type="radio" name='func' value='polyline'><span class="input-text">画折线</span>

          <input type="radio" name='func' value='polygon'><span class="input-text" style='width:5rem;'>画多边形</span>

        </div>

        <div class="input-item">

          <input type="radio" name='func' value='rectangle'><span class="input-text">画矩形</span>

          <input type="radio" name='func' value='circle'><span class="input-text">画圆</span>

        </div>

        <div class="input-item">

            <input id="clear" type="button" class="btn" value="清除" />

          <input id="close" type="button" class="btn" value="关闭绘图" />

        </div>

    </div>

  </div>

</template>

<script>

export default {

  data() {

    return {

      map: null,

      mouseTool: null,

      overlays: []

    }

  },

  mounted() {

    const script = document.createElement('script')

    script.src = `https://webapi.amap.com/maps?v=2.0&key=放置你的key&plugin=AMap.MouseTool`

    document.body.appendChild(script)

    script.onload = () =>{

      console.log('第一个 JavaScript 文件加载成功');

      this.map = new window.AMap.Map('container', {

        zoom: 14

      });

      this.mouseTool = new window.AMap.MouseTool(this.map);

      this.mouseTool.on('draw', (e) => {

        this.overlays.push(e.obj);

      })

      var radios = document.getElementsByName('func');

      for (var i = 0; i < radios.length; i += 1) {

        radios[i].onchange = (e) => {

          this.draw(e.target.value)

        }

      }

      this.draw('marker')

      document.getElementById('clear').onclick = () => {

        this.map.remove(this.overlays)

        this.overlays = [];

      }

      document.getElementById('close').onclick = () => {

        this.mouseTool.close(true)//关闭,并清除覆盖物

        for (var i = 0; i < radios.length; i += 1) {

          radios[i].checked = false;

        }

      }

    }

  },

  methods: {

    draw(type) {

        switch (type) {

          case 'marker': {

            this.mouseTool.marker({

              //同Marker的Option设置

            });

            break;

          }

          case 'polyline': {

            this.mouseTool.polyline({

              strokeColor: '#80d8ff'

              //同Polyline的Option设置

            });

            break;

          }

          case 'polygon': {

            this.mouseTool.polygon({

              fillColor: '#00b0ff',

              strokeColor: '#80d8ff'

              //同Polygon的Option设置

            });

            break;

          }

          case 'rectangle': {

            this.mouseTool.rectangle({

              fillColor: '#00b0ff',

              strokeColor: '#80d8ff'

              //同Polygon的Option设置

            });

            break;

          }

          case 'circle': {

            this.mouseTool.circle({

              fillColor: '#00b0ff',

              strokeColor: '#80d8ff'

              //同Circle的Option设置

            });

            break;

          }

        }

      }

  }

}

</script>

<style>

@import url(@/utils/demo-center.css);

.bbox{

  width: 100%;

  height: 100%;

}

html,body,#container{

  height: 100%

}

.input-item{

  height: 2.2rem;

}

.btn{

  width: 6rem;

  margin: 0 1rem 0 2rem;

}

.input-text{

  width: 4rem;

  margin-right:1rem;

}

</style>

  • 8
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值