cesium開發(fā)之如何在vue項(xiàng)目中使用cesium,使用離線地圖資源
vue使用cesium,使用離線地圖資源
第一步:創(chuàng)建vue項(xiàng)目并下載最新版本的Cesium
注意最好下載最新的版本(當(dāng)前是1.91),以確??梢粤鲿呈褂霉俜紸PI。博主有過因?yàn)槭褂门f版本Cesium導(dǎo)致無法調(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ù)問題
- Vue2中配置Cesium全過程
- 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列表頁緩存 詳情頁返回上一頁不刷新,定位到之前位置
- vue?cesium加載點(diǎn)與定位到指定位置的實(shí)現(xiàn)方法
相關(guān)文章
vue項(xiàng)目,代碼提交至碼云,iconfont的用法說明
這篇文章主要介紹了vue項(xiàng)目,代碼提交至碼云,iconfont的用法說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-07-07
詳解vue-cli 快速搭建單頁應(yīng)用之遇到的問題及解決辦法
這篇文章主要介紹了詳解vue-cli 快速搭建單頁應(yīng)用之遇到的問題及解決辦法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-03-03
基于Vue中使用節(jié)流Lodash throttle詳解
今天小編就為大家分享一篇基于Vue中使用節(jié)流Lodash throttle詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-10-10
elementui導(dǎo)出數(shù)據(jù)為xlsx、excel表格
本文主要介紹了elementui導(dǎo)出數(shù)據(jù)為xlsx、excel表格,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09
關(guān)于el-table-column的formatter的使用及說明
這篇文章主要介紹了關(guān)于el-table-column的formatter的使用及說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10
Vue項(xiàng)目中首頁長(zhǎng)時(shí)間白屏的原因以及解決過程
這篇文章主要介紹了Vue項(xiàng)目中首頁長(zhǎng)時(shí)間白屏的原因以及解決過程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-01-01

