您的位置 首页 技术专栏

vue.js怎么引入地图

24小时课堂在线收录vue.js怎么引入地图,vue.js引入地图的方法:1、进入天地图官网拿到密钥;2、在vue项目中的index.html中引入对应的src;3、新建map.js文件…感谢您关注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怎么引入地图的详细内容,更多请关注24小时课堂在线其它相关文章!

本文来自网络,不代表24小时课堂在线立场,转载请注明出处:https://www.24ketang.cn/96379.html

为您推荐

返回顶部