初识vue-baidu-map

地图的使用已是现在大数据时代的趋势所在,学会使用地图是必不可少的,今天就来谈谈如何使用百度的地图,在vue中我们通常使用百度地图的二次封装vue-baidu-map

<template>
  <div id="map">
    <baidu-map class="map" :center="center" :zoom="17" :scroll-wheel-zoom="true" @ready="handler">
      <!-- 点坐标 -->
      <bm-marker :position="center"> </bm-marker>
      <!-- 折线 -->
      <bm-polyline :path="path" stroke-color="#00FFFF" :stroke-opacity="0.5" :stroke-weight="12"></bm-polyline>
      <!-- 路书,行驶轨迹 -->
      <bml-lushu
        @stop="stop"
        :path="path"
        :icon="icon"
        :play="play"
        :speed="100"
        :autoView="true"
        :content="content"
        :rotation="true"
      >
      </bml-lushu>
      <!-- 信息窗体 -->
      <bm-info-window
        :position="position"
        :show="isShow"
        :title="`<p style=color:blue;cursor:pointer>${form.name}</p>`"
        :closeOnClick="false"
        @clickclose="isShow = false"
        class="infoWindow"
        @click.native="handleInfoClick"
      >
        <ul>
          <li>位置:{{ form.location }}</li>
          <li>所属地区:{{ form.area }}</li>
        </ul>
      </bm-info-window>
    </baidu-map>
  </div>
</template>
import BaiduMap from "vue-baidu-map/components/map/Map.vue";
import { BmlLushu } from "vue-baidu-map";
import { getAverage } from "@/utils/methods";
export default {
  data() {
    return {
      center:{ lng: 118.678105, lat: 31.954918},
      position: {},
      form: {},
      isShow: false,
      play: true,
      icon: {
        url: "http://api.map.baidu.com/library/LuShu/1.2/examples/car.png",
        size: { width: 52, height: 26 },
        opts: { anchor: { width: 27, height: 13 } },
      },
      path: [],
      content: "",
    };
  },
  components: { BaiduMap, levelTree ,BmlLushu },
  mounted() {
    this.isShow = true;
    this.path = [
      { lng: 118.671570, lat: 31.950587 },
      { lng: 118.684250, lat: 31.959605 },
    ];
    const lng = getAverage(this.path, "lng");
    const lat = getAverage(this.path, "lat");
    this.position = {
      ...lng,
      ...lat,
    };
    //点击信息窗体触发
    this.$nextTick(params => {
      document.getElementsByClassName("BMap_bubble_title")[0].onclick = this.handleInfoClick;
    });
  },
  methods: {
    //传递信息给父组件
    handleInfoClick() {
      this.$emit("handleInfoClick", this.form);
    },
    stop() {
      this.play = false;
      this.content = "到达了";
    },
    // 初始化
    handler({ BMap, map }) {
      this.map = map;
      // 创建标注
      this.map.setMapStyleV2({ styleId: "1c7acffbfbadbf927ec1b3453f059e90" });
      this.content = "出发了";
    }
  },
};
#map {
  height: calc(100vh - 152px);
  position: relative;
  /*去除水印*/
  ::v-deep .anchorBL {
    display: none;
  }
  .infoWindow {
    cursor: pointer;
    li {
      font-size: 14px;
      line-height: 1.5;
    }
  }
}
.map {
  width: 100%;
  height: 100%;
}

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值