vue項目中使用天地圖的簡單代碼示例
關(guān)于天地圖
當使用 Vue.js 開發(fā) Web 應(yīng)用程序時,使用地圖服務(wù)是一種常見的需求,在 Vue.js 中使用天地圖可以展示地理空間數(shù)據(jù)、實現(xiàn)地圖交互和定位等功能。
申請?zhí)斓貓Dapi key(創(chuàng)建一個應(yī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)建地圖圖層對象 let mapTypeSelect = [ { title: '地圖', //地圖控件上所要顯示的圖層名稱 icon: 'http://api.tianditu.gov.cn/v4.0/image/map/maptype/vector.png', //地圖控件上所要顯示的圖層圖標(默認圖標大小80x80) layer: window.TMAP_NORMAL_MAP //地圖類型對象,即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() }) } }
寫上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項目中使用天地圖的文章就介紹到這了,更多相關(guān)vue項目使用天地圖內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue組件 $children,$refs,$parent的使用詳解
本篇文章主要介紹了vue組件 $children,$refs,$parent的使用詳解,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-07-07vue雙擊事件2.0事件監(jiān)聽(點擊-雙擊-鼠標事件)和事件修飾符操作
這篇文章主要介紹了vue雙擊事件2.0事件監(jiān)聽(點擊-雙擊-鼠標事件)和事件修飾符操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-07-07vue兩組件間值傳遞 $router.push實現(xiàn)方法
兩組件間傳值,可能包含多種情況,這篇文章主要介紹了vue兩組件間值傳遞 $router.push實現(xiàn)方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-05-05Nginx配置Vue項目,無法按路徑跳轉(zhuǎn)及刷新404的解決方案
這篇文章主要介紹了Nginx配置Vue項目,無法按路徑跳轉(zhuǎn)及刷新404的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-06-06vue2導(dǎo)航根據(jù)路由傳值,而改變導(dǎo)航內(nèi)容的實例
下面小編就為大家?guī)硪黄獀ue2導(dǎo)航根據(jù)路由傳值,而改變導(dǎo)航內(nèi)容的實例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-11-11