Vue使用openlayers加載天地圖
申請(qǐng)?zhí)斓貓Dkey
官方:https://www.tianditu.gov.cn/
申請(qǐng)key:https://sso.tianditu.gov.cn/login?service=https%3A%2F%2Fconsole.tianditu.gov.cn%2F
進(jìn)去之后,先登錄,如果沒賬號(hào)先注冊(cè)一個(gè)就行。
可以創(chuàng)建個(gè)應(yīng)用,創(chuàng)建完成后,會(huì)自動(dò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' // 對(duì)應(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>
上面把天地圖密鑰替換成你第一步申請(qǐng)的key就可以。
上面示例加載了三種天地圖:矢量底圖、影像底圖、地形暈渲
效果圖
下面是效果圖:
矢量底圖
影像底圖
地形暈渲
到此這篇關(guān)于Vue使用openlayers加載天地圖的文章就介紹到這了,更多相關(guān)Vue openlayers加載天地圖內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 在Vue?3中使用OpenLayers加載GPX數(shù)據(jù)并顯示圖形效果
- vue+openlayers+nodejs+postgis實(shí)現(xiàn)軌跡運(yùn)動(dòng)效果
- Vue+OpenLayers?創(chuàng)建地圖并顯示鼠標(biāo)所在經(jīng)緯度(完整代碼)
- vue?openlayers實(shí)現(xiàn)臺(tái)風(fēng)軌跡示例詳解
- vue利用openlayers實(shí)現(xiàn)動(dòng)態(tài)軌跡
- Vue結(jié)合openlayers按照經(jīng)緯度坐標(biāo)實(shí)現(xiàn)錨地標(biāo)記及繪制多邊形區(qū)域
- Vue openLayers實(shí)現(xiàn)圖層數(shù)據(jù)切換與加載流程詳解
- Vue利用openlayers實(shí)現(xiàn)點(diǎn)擊彈窗的方法詳解
- Vue使用openlayers實(shí)現(xiàn)繪制圓形和多邊形
- 在Vue 3中使用OpenLayers讀取WKB數(shù)據(jù)并顯示圖形效果
相關(guān)文章
vue-cli或vue項(xiàng)目利用HBuilder打包成移動(dòng)端app操作
這篇文章主要介紹了vue-cli或vue項(xiàng)目利用HBuilder打包成移動(dòng)端app操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-07-07vue中watch和computed為什么能監(jiān)聽到數(shù)據(jù)的改變以及不同之處
這篇文章主要介紹了vue中watch和computed為什么能監(jiān)聽到數(shù)據(jù)的改變以及不同之處,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-12-12使用elementuiadmin去掉默認(rèn)mock權(quán)限控制的設(shè)置
這篇文章主要介紹了使用elementuiadmin去掉默認(rèn)mock權(quán)限控制的設(shè)置方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-04-04vue搭建本地JSON靜態(tài)數(shù)據(jù)服務(wù)器全過程
這篇文章主要介紹了vue搭建本地JSON靜態(tài)數(shù)據(jù)服務(wù)器全過程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-07-07vue打印瀏覽器頁面功能的兩種實(shí)現(xiàn)方法
這篇文章主要給大家介紹了關(guān)于vue打印瀏覽器頁面功能的兩種實(shí)現(xiàn)方法,這個(gè)功能其實(shí)也是自己學(xué)習(xí)到的,做完也有一段時(shí)間了,一直想記錄總結(jié)一下,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-04-04