vue項(xiàng)目中使用天地圖的簡單代碼示例
關(guān)于天地圖
當(dāng)使用 Vue.js 開發(fā) Web 應(yīng)用程序時(shí),使用地圖服務(wù)是一種常見的需求,在 Vue.js 中使用天地圖可以展示地理空間數(shù)據(jù)、實(shí)現(xiàn)地圖交互和定位等功能。
申請?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)建地圖圖層對象
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 //地圖類型對象,即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項(xiàng)目中使用天地圖的文章就介紹到這了,更多相關(guān)vue項(xiàng)目使用天地圖內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue-i18n使用$t導(dǎo)致的Typescript報(bào)錯(cuò)問題及解決
在Vue3項(xiàng)目中使用vue-i18n?v9.14.0時(shí),$t屬性可能因類型未聲明導(dǎo)致TS報(bào)錯(cuò),解決方案是創(chuàng)建src/vue-i18n.d.ts文件,添加至tsconfig.json的include項(xiàng)中,聲明$t屬性類型2025-08-08
基于Vue2實(shí)現(xiàn)數(shù)字縱向滾動(dòng)效果
這篇文章主要為大家詳細(xì)介紹了如何基于Vue2實(shí)現(xiàn)數(shù)字縱向滾動(dòng)效果,從而達(dá)到顯示計(jì)時(shí)器滾動(dòng)效果,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-03-03
vue3集成Element-Plus之全局導(dǎo)入和按需導(dǎo)入
這篇文章主要給大家介紹了關(guān)于vue3集成Element-Plus之全局導(dǎo)入和按需導(dǎo)入的相關(guān)資料,element-plus正是element-ui針對于vue3開發(fā)的一個(gè)UI組件庫,?它的使用方式和很多其他的組件庫是一樣的,需要的朋友可以參考下2023-07-07
vue實(shí)現(xiàn)前端拖拽div位置交換的方法詳解
這篇文章主要介紹了如何使用Vue技術(shù)實(shí)現(xiàn)一個(gè)簡單的備忘錄應(yīng)用,包括添加條目和拖拽條目兩個(gè)功能,文章還詳細(xì)解釋了如何使用Vue的draggable屬性和JavaScript獲取同級元素節(jié)點(diǎn)的方法,需要的朋友可以參考下2025-01-01
vue+highcharts實(shí)現(xiàn)3D餅圖效果
這篇文章主要為大家詳細(xì)介紹了vue+highcharts實(shí)現(xiàn)3D餅圖效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03
vue單頁面SEO優(yōu)化的實(shí)現(xiàn)
本文主要介紹了vue單頁面SEO優(yōu)化的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06
如何在Vue中使localStorage具有響應(yīng)式(思想實(shí)驗(yàn))
這篇文章主要介紹了如何在Vue中使localStorage具有響應(yīng)式,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-07-07

