cesium開發(fā)之如何在vue項(xiàng)目中使用cesium,使用離線地圖資源
vue使用cesium,使用離線地圖資源
第一步:創(chuàng)建vue項(xiàng)目并下載最新版本的Cesium
注意最好下載最新的版本(當(dāng)前是1.91),以確??梢粤鲿呈褂霉俜紸PI。博主有過(guò)因?yàn)槭褂门f版本Cesium導(dǎo)致無(wú)法調(diào)用API的情況。
npm cesium
第二步:在node_modules文件夾中補(bǔ)充離線資源
在項(xiàng)目根目錄的node_modules文件夾中,找到cesium文件夾,
根據(jù)以下2個(gè)路徑:node_modules\cesium\Build\Cesium\Assets\Textures和node_modules\cesium\Source\Assets\Textures找到用于存放離線地圖的文件夾。
在這個(gè)文件夾下,補(bǔ)充離線地圖數(shù)據(jù)(這個(gè)資源需要大家自己在網(wǎng)絡(luò)上找)。
第三步:使用cesium生成地球
1.在vue文件中創(chuàng)建html結(jié)構(gòu)
<div id="container"></div>
2.利用cesium構(gòu)造函數(shù)初始化地球
別忘了在data
中定義空對(duì)象viewer
this.viewer = new Cesium.Viewer("container", { geocoder: false, timeline: false, fullscreenButton: false, animation: false, shouldAnimate: true, });
第四步:使用離線地圖資源替換網(wǎng)絡(luò)地圖資源
在構(gòu)造函數(shù)中補(bǔ)充以下2個(gè)屬性:
this.viewer = new Cesium.Viewer("container", { imageryProvider: new Cesium.TileMapServiceImageryProvider({ url: Cesium.buildModuleUrl("Assets/Textures/MyEarthII"), }), baseLayerPicker: false });
最后附上cesium官網(wǎng)對(duì)imageryProvider
用法的解釋:
The imagery provider to use. This value is only valid if baseLayerPicker is set to false.
用于提供地圖圖像。僅當(dāng)baseLayerPicker
為false時(shí)有效。
cesium添加百度地圖
百度地圖的瓦塊切片規(guī)則與大多數(shù)地圖不同,其中心點(diǎn)位于地理坐標(biāo)的0,0點(diǎn),多數(shù)地圖的切片是以地圖左上角為瓦塊切片的起點(diǎn)。
Cesium中默認(rèn)的切片地圖(UrlTemplateImageryProvider)包括經(jīng)緯度模式和投影(墨卡托)模式都是以左上角切片為基準(zhǔn)。所以當(dāng)我們加載百度地圖瓦塊地圖時(shí),需要自定義地圖影像地圖類。
BaiduImageryProvider = function(options) { this._errorEvent = new Cesium.Event(); this._tileWidth = 256; this._tileHeight = 256; this._maximumLevel = 18; this._minimumLevel = 1; let southwestInMeters = new Cesium.Cartesian2(-33554054, -33746824); let northeastInMeters = new Cesium.Cartesian2(33554054, 33746824); this._tilingScheme = new Cesium.WebMercatorTilingScheme({ rectangleSouthwestInMeters: southwestInMeters, rectangleNortheastInMeters: northeastInMeters }); this._rectangle = this._tilingScheme.rectangle; this._resource = Cesium.Resource.createIfNeeded(options.url); this._tileDiscardPolicy = undefined; this._credit = undefined; this._readyPromise = undefined; }; Object.defineProperties(Cesium.gm.BaiduImageryProvider.prototype, { url: { get: function () { return this._resource.url; } }, proxy: { get: function () { return this._resource.proxy; } }, tileWidth: { get: function () { if (!this.ready) { throw new Cesium.DeveloperError('tileWidth must not be called before the imagery provider is ready.'); } return this._tileWidth; } }, tileHeight: { get: function () { if (!this.ready) { throw new Cesium.DeveloperError('tileHeight must not be called before the imagery provider is ready.'); } return this._tileHeight; } }, maximumLevel: { get: function () { if (!this.ready) { throw new Cesium.DeveloperError('maximumLevel must not be called before the imagery provider is ready.'); } return this._maximumLevel; } }, minimumLevel: { get: function () { if (!this.ready) { throw new Cesium.DeveloperError('minimumLevel must not be called before the imagery provider is ready.'); } return this._minimumLevel; } }, tilingScheme: { get: function () { if (!this.ready) { throw new Cesium.DeveloperError('tilingScheme must not be called before the imagery provider is ready.'); } return this._tilingScheme; } }, tileDiscardPolicy: { get: function () { if (!this.ready) { throw new Cesium.DeveloperError('tileDiscardPolicy must not be called before the imagery provider is ready.'); } return this._tileDiscardPolicy; } }, rectangle: { get: function () { if (!this.ready) { throw new Cesium.DeveloperError('rectangle must not be called before the imagery provider is ready.'); } return this._rectangle; } }, errorEvent: { get: function () { return this._errorEvent; } }, ready: { get: function () { return this._resource; } }, readyPromise: { get: function () { return this._readyPromise; } }, credit: { get: function () { if (!this.ready) { throw new Cesium.DeveloperError('credit must not be called before the imagery provider is ready.'); } return this._credit; } }, }); BaiduImageryProvider.prototype.requestImage = function (x, y, level, request) { let xTileCount = this._tilingScheme.getNumberOfXTilesAtLevel(level); let yTileCount = this._tilingScheme.getNumberOfYTilesAtLevel(level); let url = this.url .replace("{x}", x - xTileCount / 2) .replace("{y}", yTileCount / 2 - y - 1) .replace("{z}", level) .replace("{s}", Math.floor(10 * Math.random())); console.log("zxy:" + level + ", " + x + ", " + y + "; " + url); return Cesium.ImageryProvider.loadImage(this, url); };
調(diào)用時(shí),傳入url參數(shù)即可,url服務(wù)可以使在線服務(wù),也可以是代理服務(wù)(按百度地圖切片索引規(guī)則)。例如:
let baiduImageryProvider = new BaiduImageryProvider({ ? ? url: "http://online{s}.map.bdimg.com/onlinelabel/?qt=tile&x={x}&y={y}&z={z}&styles=pl&scaler=1&p=1" }); ? let viewer = new Cesium.Viewer('cesiumContainer', { ? ? imageryProvider: baiduImageryProvider, ? ? ... }
這時(shí)就可以在Cesium中正常加載百度地圖了。
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- vue中使用Cesium加載shp文件、wms服務(wù)、WMTS服務(wù)問(wèn)題
- Vue2中配置Cesium全過(guò)程
- vue中安裝使用cesium方式(親測(cè)可用)
- 使用開源Cesium+Vue實(shí)現(xiàn)傾斜攝影三維展示功能
- vue2.0項(xiàng)目集成Cesium的實(shí)現(xiàn)方法
- vue實(shí)現(xiàn)滾動(dòng)條到頂部或者到指定位置
- Vue列表如何實(shí)現(xiàn)滾動(dòng)到指定位置樣式改變效果
- vue keep-alive列表頁(yè)緩存 詳情頁(yè)返回上一頁(yè)不刷新,定位到之前位置
- vue?cesium加載點(diǎn)與定位到指定位置的實(shí)現(xiàn)方法
相關(guān)文章
vue中注冊(cè)組件的兩種方式詳解(全局注冊(cè)&& 局部注冊(cè))
vue 是一個(gè)完全支持組件化開發(fā)的框架, 組件之間可以進(jìn)行相互的引用,這篇文章主要介紹了vue中注冊(cè)組件的兩種方式詳解(全局注冊(cè)&& 局部注冊(cè)),需要的朋友可以參考下2023-06-06詳解vue實(shí)現(xiàn)坐標(biāo)拾取器功能示例
這篇文章主要介紹了詳解vue實(shí)現(xiàn)坐標(biāo)拾取器功能示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11在vue中對(duì)數(shù)組值變化的監(jiān)聽(tīng)與重新響應(yīng)渲染操作
這篇文章主要介紹了在vue中對(duì)數(shù)組值變化的監(jiān)聽(tīng)與重新響應(yīng)渲染操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-07-07Vuex unknown action type報(bào)錯(cuò)問(wèn)題及解決
這篇文章主要介紹了Vuex unknown action type報(bào)錯(cuò)問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02vue3中setup語(yǔ)法糖下父子組件間傳遞數(shù)據(jù)的方式
Vue3中父組件指的是包含一個(gè)或多個(gè)子組件的組件,它們通過(guò)props和事件等方式來(lái)傳遞數(shù)據(jù)和通信,這篇文章主要介紹了vue3中setup語(yǔ)法糖下父子組件間傳遞數(shù)據(jù)的方式,需要的朋友可以參考下2023-06-06vite+vue3+element-plus搭建項(xiàng)目的踩坑記錄
這篇文章主要介紹了vite+vue3+element-plus搭建項(xiàng)目的踩坑記錄,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-10-10Vue開發(fā)項(xiàng)目中如何使用Font Awesome 5
Font Awesome是一套流行的圖標(biāo)字體庫(kù),我們?cè)趯?shí)際開發(fā)的過(guò)程中會(huì)經(jīng)常遇到需要使用圖標(biāo)的場(chǎng)景,對(duì)于一些常用的圖標(biāo),我們可以直接在Font Awesome中找到并且使用,這篇文章主要給大家介紹了關(guān)于Vue開發(fā)項(xiàng)目中如何使用Font Awesome5的相關(guān)資料,需要的朋友可以參考下2021-11-11如何使用vue slot創(chuàng)建一個(gè)模態(tài)框的實(shí)例代碼
這篇文章主要介紹了如何使用vue slot創(chuàng)建一個(gè)模態(tài)框,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-05-05