Vue使用openlayers加載天地圖
申請?zhí)斓貓Dkey
官方:https://www.tianditu.gov.cn/
申請key:https://sso.tianditu.gov.cn/login?service=https%3A%2F%2Fconsole.tianditu.gov.cn%2F
進(jìn)去之后,先登錄,如果沒賬號先注冊一個就行。

可以創(chuàng)建個應(yīng)用,創(chuàng)建完成后,會自動生成key。
安裝ol加載天地圖
先安裝下
npm i ol
然后是完整代碼:
<template>
<div id="map-container"></div>
</template>
<script>
import { Map, View } from 'ol'
import { Tile as TileLayer } from 'ol/layer'
import { get } from 'ol/proj';
import { getWidth, getTopLeft } from 'ol/extent'
import { WMTS } from 'ol/source'
import WMTSTileGrid from 'ol/tilegrid/WMTS'
export const projection = get("EPSG:4326");
const projectionExtent = projection.getExtent();
const size = getWidth(projectionExtent) / 256;
const resolutions = [];
for (let z = 2; z < 19; ++z) {
resolutions[z] = size / Math.pow(2, z);
}
let map;
export default {
data() {
return {
}
},
mounted(){
this.initMap('SL') // 加載矢量底圖
// this.initMap('YX') // 加載影像底圖
// this.initMap('DX') // 加載地形暈渲
},
methods:{
initMap(layerType = 'SL') {
const TIANDI_KEY = '31499a6260cb9f29558750d04a934256'
// 對應(yīng)的值是固定的
const layerTypeMap = {
'SL': ['vec', 'cva'], // [矢量底圖, 矢量注記]
'YX': ['img', 'cia'], // [影像底圖, 影像注記]
'DX': ['ter', 'cta'] // [地形暈渲, 地形注記]
}
// c: 經(jīng)緯度投影 w: 球面墨卡托投影
const matrixSet = 'c'
map = new Map({
target: 'map-container',
layers: [
// 底圖
new TileLayer({
source: new WMTS({
url: `http://t{0-6}.tianditu.com/${layerTypeMap[layerType][0]}_${matrixSet}/wmts?tk=${TIANDI_KEY}`,
layer: layerTypeMap[layerType][0],
matrixSet: matrixSet,
style: "default",
crossOrigin: 'anonymous', // 解決跨域問題 如無該需求可不添加
format: "tiles",
wrapX: true,
tileGrid: new WMTSTileGrid({
origin: getTopLeft(projectionExtent),
//resolutions: res.slice(0, 15),
resolutions: resolutions,
matrixIds: ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14']
})
})
}),
// 標(biāo)注
new TileLayer({
source: new WMTS({
url: `http://t{0-6}.tianditu.com/${layerTypeMap[layerType][1]}_${matrixSet}/wmts?tk=${TIANDI_KEY}`,
layer: layerTypeMap[layerType][1],
matrixSet: matrixSet,
style: "default",
crossOrigin: 'anonymous', // 解決跨域問題 如無該需求可不添加
format: "tiles",
wrapX: true,
tileGrid: new WMTSTileGrid({
origin: getTopLeft(projectionExtent),
resolutions: resolutions,
matrixIds: ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14']
})
})
})
],
view: new View({
center: [169.40, 65.35],
projection: projection,
zoom: 3,
maxZoom: 17,
minZoom: 1
})
})
}
}
}
</script>
<style scoped>
#map-container {
width: 100%;
height: 100%;
}
</style>上面把天地圖密鑰替換成你第一步申請的key就可以。
上面示例加載了三種天地圖:矢量底圖、影像底圖、地形暈渲
效果圖
下面是效果圖:
矢量底圖

影像底圖

地形暈渲

到此這篇關(guān)于Vue使用openlayers加載天地圖的文章就介紹到這了,更多相關(guān)Vue openlayers加載天地圖內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 在Vue?3中使用OpenLayers加載GPX數(shù)據(jù)并顯示圖形效果
- vue+openlayers+nodejs+postgis實現(xiàn)軌跡運(yùn)動效果
- Vue+OpenLayers?創(chuàng)建地圖并顯示鼠標(biāo)所在經(jīng)緯度(完整代碼)
- vue?openlayers實現(xiàn)臺風(fēng)軌跡示例詳解
- vue利用openlayers實現(xiàn)動態(tài)軌跡
- Vue結(jié)合openlayers按照經(jīng)緯度坐標(biāo)實現(xiàn)錨地標(biāo)記及繪制多邊形區(qū)域
- Vue openLayers實現(xiàn)圖層數(shù)據(jù)切換與加載流程詳解
- Vue利用openlayers實現(xiàn)點(diǎn)擊彈窗的方法詳解
- Vue使用openlayers實現(xiàn)繪制圓形和多邊形
- 在Vue 3中使用OpenLayers讀取WKB數(shù)據(jù)并顯示圖形效果
相關(guān)文章
關(guān)于vue-cli-service:command?not?found報錯引發(fā)的實戰(zhàn)案例
這篇文章主要給大家介紹了關(guān)于vue-cli-service:command?not?found報錯引發(fā)的相關(guān)資料,文中通過實例代碼將解決的過程介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2023-02-02
elementui的table根據(jù)是否符合需求合并列的實現(xiàn)代碼
這篇文章主要介紹了elementui的table根據(jù)是否符合需求合并列的實現(xiàn)代碼,代碼簡單易懂,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧2024-03-03

