vue項(xiàng)目中使用天地圖的簡(jiǎn)單代碼示例
關(guān)于天地圖
當(dāng)使用 Vue.js 開(kāi)發(fā) Web 應(yīng)用程序時(shí),使用地圖服務(wù)是一種常見(jiàn)的需求,在 Vue.js 中使用天地圖可以展示地理空間數(shù)據(jù)、實(shí)現(xiàn)地圖交互和定位等功能。
申請(qǐng)?zhí)斓貓Dapi key(創(chuàng)建一個(gè)應(yīng)用)
引入天地圖
在項(xiàng)目中public文件夾下index.html中引入
<script src="http://api.tianditu.gov.cn/api?v=3.0&tk=您的密鑰" type="text/javascript"></script>
創(chuàng)建map
<div id="map"></div>
初始化地圖
mounted() { this.load() }, methods:{ load() { const init = new Promise((resolve, reject) => { if (window.T) { console.log('地圖初始化成功') resolve(window.T) reject('error') } }) init.then(T => { this.map = new T.Map('map') this.map.maxZoom = 20 this.map.centerAndZoom( new T.LngLat(this.centerData[0], this.centerData[1]), 16 ) //創(chuàng)建地圖圖層對(duì)象 let mapTypeSelect = [ { title: '地圖', //地圖控件上所要顯示的圖層名稱 icon: 'http://api.tianditu.gov.cn/v4.0/image/map/maptype/vector.png', //地圖控件上所要顯示的圖層圖標(biāo)(默認(rèn)圖標(biāo)大小80x80) layer: window.TMAP_NORMAL_MAP //地圖類型對(duì)象,即MapType。 }, { title: '衛(wèi)星', icon: ' http://api.tianditu.gov.cn/v4.0/image/map/maptype/satellite.png', layer: window.TMAP_SATELLITE_MAP }, { title: '衛(wèi)星混合', http: 'api.tianditu.gov.cn/v4.0/image/map/maptype/satellitepoi.png', layer: 'TMAP_HYBRID_MAP' }, { title: '地形', icon: ' http://api.tianditu.gov.cn/v4.0/image/map/maptype/terrain.png', layer: window.TMAP_TERRAIN_MAP }, { title: '地形混合', icon: ' http://api.tianditu.gov.cn/v4.0/image/map/maptype/terrainpoi.png', layer: window.TMAP_TERRAIN_HYBRID_MAP } ] var ctrl = new T.Control.MapType({ mapTypes: mapTypeSelect }) // 初始化地圖類型選擇控件 // this.map.addControl(ctrl); //添加地圖選擇控件 this.map.setMapType(window.TMAP_SATELLITE_MAP) // 設(shè)置地圖位地星混合圖層 this.GetMaps() }) } }
寫(xiě)上css樣式
<style scoped> #map { width: calc(100vw - 26vw); height: 80vh; position: absolute; left: 26vw; top: 20vh; z-index: 0; } ::v-deep .tdt-infowindow-content { margin: 6px 9px !important; padding: 0 4px !important; text-align: center !important; } ::v-deep .tdt-infowindow-tip-container { margin: -2px auto !important; } ::v-deep .tdt-container a.tdt-infowindow-close-button { padding: 0 0 0 4px !important; } ::v-deep .tdt-label { line-height: 24px !important; padding: 0 5px !important; border-radius: 2px; } ::v-deep .tdt-infowindow-content-wrapper, .tdt-infowindow-tip { border-radius: 8px; } </style>
最后成果
總結(jié)
到此這篇關(guān)于vue項(xiàng)目中使用天地圖的文章就介紹到這了,更多相關(guān)vue項(xiàng)目使用天地圖內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue組件 $children,$refs,$parent的使用詳解
本篇文章主要介紹了vue組件 $children,$refs,$parent的使用詳解,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07Vue使用axios出現(xiàn)options請(qǐng)求方法
這篇文章主要介紹了Vue使用axios出現(xiàn)options請(qǐng)求,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05基于Vue 服務(wù)端Cookies刪除的問(wèn)題
今天小編就為大家分享一篇基于Vue 服務(wù)端Cookies刪除的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-09-09vue雙擊事件2.0事件監(jiān)聽(tīng)(點(diǎn)擊-雙擊-鼠標(biāo)事件)和事件修飾符操作
這篇文章主要介紹了vue雙擊事件2.0事件監(jiān)聽(tīng)(點(diǎn)擊-雙擊-鼠標(biāo)事件)和事件修飾符操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-07-07vue兩組件間值傳遞 $router.push實(shí)現(xiàn)方法
兩組件間傳值,可能包含多種情況,這篇文章主要介紹了vue兩組件間值傳遞 $router.push實(shí)現(xiàn)方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-05-05Nginx配置Vue項(xiàng)目,無(wú)法按路徑跳轉(zhuǎn)及刷新404的解決方案
這篇文章主要介紹了Nginx配置Vue項(xiàng)目,無(wú)法按路徑跳轉(zhuǎn)及刷新404的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06vue實(shí)現(xiàn)頂部左右滑動(dòng)導(dǎo)航
這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)頂部左右滑動(dòng)導(dǎo)航,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-06-06vue2導(dǎo)航根據(jù)路由傳值,而改變導(dǎo)航內(nèi)容的實(shí)例
下面小編就為大家?guī)?lái)一篇vue2導(dǎo)航根據(jù)路由傳值,而改變導(dǎo)航內(nèi)容的實(shí)例。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-11-11