前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >百度地图使用记录

百度地图使用记录

作者头像
tianyawhl
发布2022-12-21 16:41:28
5370
发布2022-12-21 16:41:28
举报
文章被收录于专栏:前端之攻略前端之攻略

loadBaiduMap.js

代码语言:javascript
复制
export default function loadBaiduMap(ak) {
    return new Promise(function (resolve, reject) {
        // window.onload = function () {
        //     resolve(BMapGL)
        // }
        window.init = function(){
            resolve(BMapGL)
        }
        var script = document.createElement("script");
        script.type = "text/javascript";
        script.src = `http://api.map.baidu.com/api?type=webgl&v=1.0&ak=${ak}&callback=init`
        // script.src = "http://api.map.baidu.com/api?v=2.0&ak=" + ak + "&callback=init";
        script.onerror = reject;
        document.head.appendChild(script);
    })
}
代码语言:javascript
复制
<template>
  <div>
    baidu
    <el-button @click="clearMarkers">清除点</el-button>
    <div id="BDMap" style="height: calc(100vh - 200px)"></div>
     <InfoWindow ref="infoWindow" v-show="showinfoWindow"/>
  </div>
 
</template>

<script>
import loadMap from "@/assets/js/loadBaiduMap.js";
import InfoWindow from "@/components/BaiduInforWindow.vue"
export default {
  components:{InfoWindow},
  data() {
    return {
      showinfoWindow:false,
      BDMap:null,
      markers:[],
      markerList:[{name:"标记点1",lng:119.984933,lat:31.77332,id:"001"},{name:"标记点2标记点2",lng:119.978035,lat:31.790264,id:"002"}],
      
      baiduKey: "m4ryezjGKNVeNuMiviQx1cLGNrinVItK",
    };
  },
  methods: {
    clearMarkers(){
      console.log(this.markers)
      this.markers.forEach(item=>{
        this.BDMap.removeOverlay(item)
      })
      this.markers = []
    },
    drawMarkers(){
      this.markerList.forEach((item,index)=>{
        let myIcon = new BMapGL.Icon(require("@/assets/imgs/MapOnline.png"),new BMapGL.Size(20,27),{anchor:new BMapGL.Size(10,27)})
        let pointer = new BMapGL.Point(item.lng,item.lat)
        let marker = new BMapGL.Marker(pointer,{icon:myIcon})
        let label = new BMapGL.Label(item.name,{position:pointer,offset:new BMapGL.Size(0,0)})
        label.setStyle({
          color:"red",
          // color: "#fff",
          // backgroundColor: "#333333",
          // border: "0",
          // fontSize : "14px",
          // width:"200px",
          // opacity:"0.8",
          // textAlign:"center",
          // verticalAlign:"center",
          // borderRadius: "2px",
          // whiteSpace:"normal",
          // wordWrap:"break-word",
          // padding:"7px",
          transform:'translateX(-50%)'
        })
        marker.setLabel(label)
        marker.extraData = {id:item.id,name:item.name}
        this.BDMap.addOverlay(marker)
        var sContent= `<div>信息窗口测试</div>`
       
        marker.addEventListener("click",(e)=>{
          console.log(e.target)
          this.markerClick(e)
        })
        this.markers.push(marker)
      })
    },
    markerClick(e){
       console.log(e.target.extraData.id)
       var infoWindow = new BMapGL.InfoWindow(this.$refs.infoWindow.$el,{offset: new BMapGL.Size(100, 100)})
        this.showinfoWindow = true  
        infoWindow.setTitle(e.target.extraData.name)     
        e.target.openInfoWindow(infoWindow)
        this.$nextTick(()=>{
          infoWindow.redraw()
        })
        this.$refs.infoWindow.initialize({
          id:e.target.extraData.id
        })
        // 监听信息窗口关闭时执行的代码
        infoWindow.addEventListener("close",()=>{
          console.log("关闭")
        })
        // 信息窗口关闭
        // setTimeout(() => {
        //   infoWindow.close()
        // }, 2000);
    },
    showMap(){
      loadMap(this.baiduKey).then((BMapGL)=>{
        this.BDMap = new BMapGL.Map("BDMap", {
          coordsType: 5, // coordsType指定输入输出的坐标类型,3为gcj02坐标,5为bd0ll坐标,默认为5。
          // 指定完成后API将以指定的坐标类型处理您传入的坐标
        }); // 创建地图实例
        var point = new BMapGL.Point(119.978035,31.790264); // 创建点坐标
        this.BDMap.centerAndZoom(point, 15);
        this.BDMap.enableScrollWheelZoom(true)
        this.drawMarkers()
        // let myIcon = new BMapGL.Icon(require("@/assets/imgs/MapOnline.png"),new BMapGL.Size(19,27))
        // let marker = new BMapGL.Marker(new BMapGL.Point(119.978035,31.790264),{icon:myIcon})
        // BDMap.addOverlay(marker)
      })
    }
  },

  mounted() {
    this.showMap();
  },
};
</script>

<style>
/* .v-modal{z-index:5 !important;} */
.amap-sug-result {
  z-index: 2024;
}
</style>

<style scoped>
/deep/ .BMap_bubble_top{border-bottom:1px solid red;}
/deep/ .BMap_bubble_bottom+img{display:none;}
</style>
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022-12-14,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档