Vue 高德地图的使用


近期,在进行项目开发的时候,系统中一大功能就是物流系统。所以以高德地图原生API结合Vue,开发相关功能...

一、如何在Vue中引入高德地图?

①在index.html中
your key 在高德地图官网申请的key码

<script type="text/javascript" src="http://webapi.amap.com/maps?v=1.3&key='your key'"></script>

②在main.js中引入

import AMap from "AMap";
Vue.use(AMap);

③在vue.config.js中引入

  configureWebpack: {
    externals: {
      AMap: "AMap",
    },
  },

二、需求一:在首页制作轮播图类型的地图组件

在这里插入图片描述
作为子组件,他需要获取从上层父级元素遍历来的数组进行渲染,主要点还是在于要把包括id在内的所有数据动态绑定在data中

<!-- 高德地图组组件 -->
<template>
  <div>
    <div :id="container" style="width: 630px; height: 221px"></div>
  </div>
</template>

<script>
import AMap from "AMap";
export default {
  data() {
    return {
      container: "",
      lineArr: [],
      map: {},
      maker: {},
      polyline: {},
    };
  },
  created() {
    this.printData();
  },
  mounted() {
    this.init();
  },
  props: ["recordData"],
  methods: {
    printData() {
    //处理从上层传值来的数据
      this.lineArr = this.recordData.recordMap.mapAO;
      this.container = this.recordData.id;
    },
    init() {
      //制作地图
      this.map = new AMap.Map(this.recordData.id, {
        resizeEnable: true,
        center: this.recordData.recordMap.mapData.center,
      });
      //制作点
      this.marker = new AMap.Marker({
        map: this.map,
        position: this.recordData.recordMap.mapData.position,
      });
      //制作路径
      this.polyline = new AMap.Polyline({
        map: this.map,
        path: this.lineArr,
        showDir: true,
        strokeColor: "#28F",
        strokeWeight: 6,
      });
      //打印点,路径,以及动态设置zoom大小
      this.map.add(this.marker);
      this.map.setFitView();
      this.map.setZoom(16);
    },
  },
};
</script>


<style  scoped>
</style>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值