Vue+百度地图GL

这次使用的原理还是引用bmapGl.js这个文件,并不是引用模块,可能是因为对于Vue模块管理不够了解,模块引用死活整不上去,话不多说开始正题。

1.创建bmpgl.js

//BMapGL
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);
    });
}

这里的ak是变量,代表的是你申请的秘钥。

2.创建bmap.vue

<template>
    <div id="container"></div>
</template>
<script>
import {BMPGL} from "../../assets/js/bmap/bmpgl.js";

export default {
    name:'map',
    data(){
        return{
            ak:"你的秘钥",
        };
    },
    mounted(){
        this.initMap();
    },
    methods:{
        initMap(){
            BMPGL(this.ak)
            .then(BMapGL => {
                //创建地图实例
                let map = new BMapGL.Map("container");
                map.centerAndZoom("北京市",5);
                map.enableScrollWheelZoom(true);
                map.setMapType(BMAP_EARTH_MAP);
            })
        }
    }


}
</script>
<style>
    /* The container of BaiduMap must be set width & height. */
    #container {
        overflow: hidden;
        width: 100%;
        height: 100%;
        margin: 0;
        font-family: "微软雅黑";
    }
</style>

代码就这么多。

3.效果

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值