温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

vue.js怎样引入地图

发布时间:2021-10-08 09:25:26 来源:亿速云 阅读:321 作者:柒染 栏目:编程语言

vue.js怎样引入地图,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。

vue.js引入地图的方法:1、进入天地图官网拿到密钥;2、在vue项目中的index.html中引入对应的src;3、新建map.js文件,在vue页面中引用即可。

本文操作环境:windows10系统、vue 2.5.2、thinkpad t480电脑。

要在vue项目中引入地图其实有很多种方法,比如我们可以使用天地图、vue-amap灯的功能,两种方式各有优势,大家可以根据自己的需要来进行选择。这里就介绍下天地图方式。

具体方法步骤如下所示:

第一步是按照天地图官网拿到自己的key(密钥)

第二步是在你的vue项目中的  index.html  中引入对应的src。

<script src="//api.tianditu.gov.cn/api?v=4.0&tk=396a532e3cc05a260931d1b308636316"></script>

第三步就是建一个js文件 Map.js ,方便天地图的引入,此文件可以放在你方便引入的位置。Map.js  中代码如下

// 初始化地图
export default {
  init() {
    return new Promise((resolve, reject) => {
      // 如果已加载直接返回
      if (window.T) {
        console.log('地图脚本初始化成功...')
        resolve(window.T)
        reject('error')
      }
    })
  }
}

第四步就可以在使用的vue页面中引用了。代码如下

<template>
    <div class="home">
        <div id="bdmap" class="map" style ="position:absolute;bottom:0px;top:0px;width:100%"></div>
    </div>
</template>
<script>
import MapInit from "@/components/Map.js"

export default {
    data(){
        return{
            map: null,
        }
    },
    created(){
        this.init()
    },
    methods:{
        init(){
            MapInit.init().then(
            T => {
                this.T = T;
                const imageURL = "http://t0.tianditu.gov.cn/img_c/wmts?tk=您的密钥";
                const lay = new T.TileLayer(imageURL, { minZoom: 1, maxZoom: 18 });
                const config = { layers: [lay], name: 'TMAP_SATELLITE_MAP' };
                this.map = new T.Map('bdmap', config);
                const ctrl = new T.Control.MapType();
                this.map.addControl(ctrl);
                this.map.centerAndZoom(new T.LngLat(118.62, 28.75), 16)
                this.map.addEventListener("zoomend", () => {
                console.log(lay.Pe)
            });
            }).catch()
            // 监听缩放级别(缩放后的级别)
            
        }
    }
}
</script>
<style>
.map{
    width: 100vw;
    height: 100%;
    position: absolute;
}
</style>

看完上述内容,你们掌握vue.js怎样引入地图的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注亿速云行业资讯频道,感谢各位的阅读!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI