vue + 百度地图 实现大屏嵌入地图

效果图

 1.首先在 pubilc 中 index.html 文件引入

<script type="text/javascript" src="https://api.map.baidu.com/api?v=3.0&ak=你申请的ak"></script>

 2.html 部分代码

<template>
  <div class="map_section">
    <div class="map_box" id="mapbox"></div>
  </div>
</template>

3.script 代码

<script>
// 引入图片 方便后期使用 鼠标hover显示文字
import mapAddressIcon from "@/assets/img/dp_image/huang_lv.png";
export default {
  name: "map-page-index",
  data() {
    return {
        allDataList: [
                 {
                    longitude: "118.89945",
                    latitude: "31.57325",
                    dwmc: "明觉支行",
                    data1: "1124",
                    isShow: true,
                    isShowTwo: true,
                    typeDataVlaue: "1"
                  },
        ], // 所有数组
        mapAddressIcon: mapAddressIcon, // 引入的图片
        map: null
        }
        },
mounted() {
    this.getDataFn();
  },
methods: {
    getDataFn(){
        this.initCreatedBmap();
        this.initAddMarker();
    },
//long = "119.03065", lat = "31.585568", zoomIndex = 11 默认位置  大小
initCreatedBmap(long = "119.03065", lat = "31.585568", zoomIndex = 11) {
      let map = new BMap.Map("mapbox", {
        enableMapClick: false
      });
      let region = "溧水区";
      this.drawPolygon(region);
      let point = new BMap.Point(long, lat);
      map.centerAndZoom(point, zoomIndex);
      map.enableScrollWheelZoom(true);
      map.setMapStyleV2({
        styleId: "74e2530c8ee1d179e8ff860bdd6a5100" // 这个id 需要自己去百度地图中发布
      });
// 也可以用系统自带的
      // map.setMapStyle({ style: "midnight" });
      this.map = map;
    },
    // 绘制行政区划轮廓
    drawPolygon(regionName) {
      let that = this;
      let bdary = new BMap.Boundary();
      bdary.get(regionName, function(rs) {
        let count = rs.boundaries.length;
        if (!count) {
          console.log("未能获取到当前输入的行政区域");
          return;
        }
        for (let i = 0; i < count; i++) {
          //创建多边形覆盖物
          let ply = new BMap.Polygon(rs.boundaries[i], {
            strokeWeight: 4,
            strokeColor: "#01D6F8",
            fillOpacity: 0.6,
            strokeStyle: "solid"
          });
          ply.setFillColor("#285E89");
          //添加覆盖物
          that.map.addOverlay(ply);
        }
      });
    },
    initAddMarker(arr) {
      let that = this;
      let dataArr = that.allDataList;
      let bigSize = new BMap.Size(30, 30);
      let icon1 = new BMap.Icon(that.mapAddressIcon, bigSize);
      dataArr.map(item => {
        let mapIcon = icon1;
        mapIcon.setImageSize(bigSize); // 背景图 大小
        let point = new window.BMap.Point(item.longitude, item.latitude);
        let labelContent = item.dwmc;
        let num = `${item.data1}`;
        let optsion = {
          point: 0,
          offset: 0,
          enableMassClear: true
        };
        let marker = new window.BMap.Marker(point, { icon: icon1 });
        let label = new window.BMap.Label(item.dwmc[0], optsion);
        // marker.setIcon(mapIcon)
        marker.setLabel(label);
        // 自行加判断
        // if (!item.isShow && !item.isShowTwo) {
          // 休业 撤防
          label.setStyle({
            // 设置label的样式
            width: "30px",
            height: "30px",
            background: item.typeDataVlaue == 1 ? "#1BE000" : "#FFF600",
            borderRadius: "50%",
            lineHeight: "22px",
            textAlign: "center",
            fontFamily: "Microsoft YaHei",
            fontWeight: "bold",
            color: "rgba(139, 139, 139, 1)",
            fontSize: "15px",
            borderWidth: "4px",
            borderStyle: "solid",
            borderColor: "rgba(0, 170, 44, 1)"
          });
        this.map.addOverlay(marker);
        // 点击跳转页面
        marker.addEventListener("click", async item => {
          let index = this.allDataList.findIndex(
            item => item.dwmc == labelContent
          );
          this.allDataList.forEach(item => {
            if (item.dwmc == labelContent) {
              this.ItemData = item;
            }
          });
          if (index != -1) {
            this.$router.push({
              name: "cockpit-cockpitTwoPageIndex",
              params: { name: labelContent, data: this.ItemData }
            });
          }
        });
// 鼠标划入 
        marker.addEventListener("mouseover", async () => {
          // if (item.typeDataVlaue == "1") {
            var label = new BMap.Label(
              `<span style="font-size: 15px;font-family: Microsoft YaHei;font-weight: bold;color: #FFF; line-height: 4px;display: inline-block;transform: skewX(20deg);">${labelContent}(${num})</span>`,
              {
                offset: new BMap.Size(0, -35)
              }
            );
            label.setStyle({
              textAlign: "center",
              width: "132px",
              height: "26px",
              transform: "skewX(-20deg)",
              background: "rgba(25, 244, 2, .7)",
              borderRadius: "2px",
              border: "2px solid #19F402",
              borderRadius: "5px",
              lineHeight: "26px"
            });
          // } 

          marker.setLabel(label);
        });
// 鼠标划出
        marker.addEventListener("mouseout", function(e) {
          var label = this.getLabel();
          //设置标签内容为空
          label.setContent("");
          //设置标签边框宽度为0
          label.setStyle({ border: "none", width: "0px", padding: "0px" });
        });
      });
    },
  },
  destroyed() {
    this.map = null;
  }
}
</script>

3. css 代码

.map_section {
  height: 599px;
  width: 837px;
.map_box {
    width: inherit;
    height: inherit;
  }
}
/* 隐藏地图的baidulogo */
::v-deep .anchorBL {
  display: none;
}

4.完成

 

 

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
你可以使用百度地图 JavaScript API 来实现Vue 中进行定位。首先,你需要在你的 Vue 项目中引入百度地图 JavaScript API 的 SDK。 1. 在你的 HTML 文件中添加以下代码来引入百度地图的 SDK: ```html <script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=YOUR_API_KEY"></script> ``` 请将 `YOUR_API_KEY` 替换为你自己申请的百度地图的 API 密钥。 2. 在你的 Vue 组件中,你可以使用 `mounted` 钩子函数来初始化地图和定位: ```javascript mounted() { // 创建地图实例 const map = new BMap.Map("map-container"); // 创建定位控件实例 const geolocation = new BMap.Geolocation(); // 获取位置信息 geolocation.getCurrentPosition((result) => { if (geolocation.getStatus() === BMAP_STATUS_SUCCESS) { // 定位成功,获取经纬度信息 const { point } = result; // 在地图上显示定位结果 map.centerAndZoom(point, 15); map.addOverlay(new BMap.Marker(point)); } else { // 定位失败 console.log("定位失败:" + geolocation.getStatus()); } }); }, ``` 在上面的代码中,我们通过 `BMap.Map` 创建了一个地图实例,并通过 `BMap.Geolocation` 创建了一个定位控件实例。然后,使用 `geolocation.getCurrentPosition` 方法获取位置信息,并根据定位结果在地图上进行展示。 3. 在你的 Vue 模板中,添加包含地图的容器: ```html <template> <div id="map-container"></div> </template> ``` 通过以上步骤,你就可以在 Vue 中使用百度地图进行定位了。记得替换 `YOUR_API_KEY` 为你自己的百度地图 API 密钥,并根据需要调整地图的样式和定位的精度等参数。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

new Vue()

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

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

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

打赏作者

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

抵扣说明:

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

余额充值