vue3.0引入百度地圖並標記點的實現代碼

首先新建一個js,主要用來引入百度地圖的js,代碼如下:

export function baiduMap() {
    return new Promise(function(resolve, reject) {
        window.baiduMap = function() {
            resolve()
        }

        var script2 = document.createElement('script')
        script2.type = 'text/javascript'
        script2.src = `http://api.map.baidu.com/library/SearchInfoWindow/1.5/src/SearchInfoWindow_min.js`

        var link = document.createElement('link')
        link.rel = 'stylesheet'
        link.href = `http://api.map.baidu.com/library/SearchInfoWindow/1.5/src/SearchInfoWindow_min.css`

        document.head.appendChild(link)
        document.head.appendChild(script2)

        var script = document.createElement('script')
        script.type = 'text/javascript'
        script.src = `http://api.map.baidu.com/api?v=3.0&ak=你的ak&callback=baiduMap`
        script.onerror = reject
        document.head.appendChild(script)
    })
}

獲取生成密匙: ak密匙(百度地圖)

其次,創建baidu-map.vue,代碼如下:

<template>
  <el-dialog title="門店地圖分佈" v-model="open" width="70%"  draggable>
    <div id="map" :style="{height:mapHeight}">
      <div id="allmap" ref="allmap" ></div>
    </div>
  </el-dialog>

</template>
<script setup name="BaiduMap">
import {baiduMap} from '@/api/post/baiduMap'
import {getLineShopMap} from '@/api/post/line'
import {nextTick} from "vue";

const mapHeight = ref('70vh')
const open = ref(false)
const allmap = ref(null)

function map(id) {
  open.value = true
  getLineShopMap(id).then(res => {
    var markerArr = []
    var data = res.data
    for (var i = 0; i < data.length; i++) {
      var point = data[i].lng + "," + data[i].lat;
      var shop = '{ title: "' + data[i].shopname + '",areaname: "' + data[i].name + '", point: "' + point + '", receiver: "' + data[i].receiver + '", mobile: "' + data[i].mobile + '" }';
      var shopobj = eval('(' + shop + ')');
      markerArr.push(shopobj);
    }
    nextTick(res => {
      baiduMap().then(thatMap => {
        // 創建地圖實例
        var map = new BMap.Map("map");
        var point = new BMap.Point(113.312213, 23.147267); //地圖中心點,廣州市
        map.centerAndZoom(point, 13); // 初始化地圖,設置中心點坐標和地圖級別。
        map.enableScrollWheelZoom(true); //啟用滾輪放大縮小
        //地圖、衛星、混合模式切換
        map.addControl(new BMap.MapTypeControl({mapTypes: [BMAP_NORMAL_MAP, BMAP_SATELLITE_MAP, BMAP_HYBRID_MAP]}));
        //向地圖中添加縮放控件
        var ctrlNav = new window.BMap.NavigationControl({
          anchor: BMAP_ANCHOR_TOP_LEFT,
          type: BMAP_NAVIGATION_CONTROL_LARGE
        });
        map.addControl(ctrlNav);
        //向地圖中添加縮略圖控件
        var ctrlOve = new window.BMap.OverviewMapControl({
          anchor: BMAP_ANCHOR_BOTTOM_RIGHT,
          isOpen: 1
        });
        map.addControl(ctrlOve);
        //向地圖中添加比例尺控件
        var ctrlSca = new window.BMap.ScaleControl({
          anchor: BMAP_ANCHOR_BOTTOM_LEFT
        });
        map.addControl(ctrlSca);

        var point = new Array(); //存放標註點經緯信息的數組
        var marker = new Array(); //存放標註點對象的數組
        var info = new Array(); //存放提示信息窗口對象的數組
        var searchInfoWindow = new Array();//存放檢索信息窗口對象的數組

        for (var i = 0; i < markerArr.length; i++) {
          var p0 = markerArr[i].point.split(",")[0]; //
          var p1 = markerArr[i].point.split(",")[1]; //按照原數組的point格式將地圖點坐標的經緯度分別提出來
          point[i] = new window.BMap.Point(p0, p1); //循環生成新的地圖點
          marker[i] = new window.BMap.Marker(point[i]); //按照地圖點坐標生成標記
          map.addOverlay(marker[i]);
          marker[i].setAnimation(BMAP_ANIMATION_BOUNCE); //跳動的動畫
          //顯示marker的title,marker多的話可以註釋掉
          var label = new window.BMap.Label(markerArr[i].title, {offset: new window.BMap.Size(20, -10)});
          marker[i].setLabel(label);
          // 創建信息窗口對象  receiver: "'+data[i].receiver+'", mobile:
          info[i] = "<p style='font-size:14px;line-height:20px'>" + "</br>門店名稱:" + markerArr[i].title + "</br>所在區域:" + markerArr[i].areaname + "</br>接收人:" + markerArr[i].receiver + "</br> 門店電話:" + markerArr[i].mobile + "</br></p>";
          //創建百度樣式檢索信息窗口對象
          searchInfoWindow[i] = new BMapLib.SearchInfoWindow(map, info[i], {
            title: markerArr[i].title,      //標題
            width: 290,             //寬度
            // height : 40,              //高度
            panel: "panel",         //檢索結果面板
            enableAutoPan: true,     //自動平移
            searchTypes: [
              BMAPLIB_TAB_SEARCH,   //周邊檢索
              //    BMAPLIB_TAB_TO_HERE,  //到這裡去
              //   BMAPLIB_TAB_FROM_HERE //從這裡出發
            ]
          });

          //添加點擊事件
          marker[i].addEventListener("click",
              (function (k) {
                // js 閉包
                return function () {
                  //將被點擊marker置為中心
                  // map.centerAndZoom(point[k], 13);
                  // console.log(searchInfoWindow[k])
                  //在marker上打開檢索信息窗口
                  searchInfoWindow[k].open(marker[k]);
                }
              })(i)
          );
        }
      });
    })
  })
}

defineExpose({
  map
})

</script>
<style>
#allmap {
  height: 100%;
  overflow: hidden;
}
</style>


最後,在你需要用到的界面引用

引用界面的script的代碼

import BaiduMap from "./baidu-map";
const mapRef = ref(null);

/**
 * 查看地圖
 * @param selection
 */
function openMap() {
  mapRef.value.map(form.value.id)
}

最終效果

參考:博客

以上就是vue3.0引入百度地圖並標記點的詳細內容,更多關於vue3.0百度地圖標記點的資料請關註WalkonNet其它相關文章!

推薦閱讀: