当前位置: 代码网 > it编程>编程语言>Javascript > 完美解决vue引入BMapGL is not defined的问题

完美解决vue引入BMapGL is not defined的问题

2024年10月28日 Javascript 我要评论
vue引入bmapgl is not defined在项目src下添加bmp.js内容如下:(ak是密钥)// bmp.jsexport function bmpgl(ak) { return ne

vue引入bmapgl is not defined

在项目src下添加bmp.js

内容如下:(ak是密钥)

// bmp.js
export function bmpgl(ak) {
  return new promise(function(resolve, reject) {
    window.init = function() {
      // eslint-disable-next-line
      resolve(bmapgl)
    }
    const script = document.createelement('script')
    script.type = 'text/javascript'
    script.src = `http://api.map.baidu.com/api?v=1.0&type=webgl&ak=${ak}&callback=init`
    script.onerror = reject
    document.head.appendchild(script)
  })
}

在要用到bmapgl的vue文件中导入

<template>
  <div class="home">
  <!--创建地图容器-->
    <div id="admap" class="allmap"></div>
  </div>
</template>
 
<script>
import { bmpgl } from "@/bmpgl.js"
export default {
  name: "home",
  data() {
    return {
      ak: "xxxxxxxxx", // 百度的地图密钥
      mymap: null
    };
  },
  mounted() {
    this.initmap()
  },
  methods: {
    initmap() {
      // 传入密钥获取地图回调。
      bmpgl(this.ak).then((bmapgl) => {
        // 创建地图实例
        let map = new bmapgl.map("admap");
        // 创建点坐标 axios => res 获取的初始化定位坐标
        let point = new bmapgl.point(114.031761, 22.542826)
        // 初始化地图,设置中心点坐标和地图级别
        map.centerandzoom(point, 19)
        //开启鼠标滚轮缩放
        map.enablescrollwheelzoom(true)
        map.setheading(64.5)
        //map.settilt(73)
        // 保存数据
        // this.mymap = map
      })
      .catch((err)=>{
        console.log(err)
      })
    },
  }
};
</script>
<style lang="scss" scoped>
.admap{
  width: 100%;
  height: 100vh;
  position: relative;
  z-index: 1;
}
</style>

完美解决啦!

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com