cesium開發之如何在vue項目中使用cesium,使用離線地圖資源

vue使用cesium,使用離線地圖資源

第一步:創建vue項目並下載最新版本的Cesium

註意最好下載最新的版本(當前是1.91),以確保可以流暢使用官方API。博主有過因為使用舊版本Cesium導致無法調用API的情況。

npm cesium

 

第二步:在node_modules文件夾中補充離線資源

在項目根目錄的node_modules文件夾中,找到cesium文件夾

根據以下2個路徑:node_modules\cesium\Build\Cesium\Assets\Textures和node_modules\cesium\Source\Assets\Textures找到用於存放離線地圖的文件夾。

在這個文件夾下,補充離線地圖數據(這個資源需要大傢自己在網絡上找)。

離線地圖資源

第三步:使用cesium生成地球

1.在vue文件中創建html結構

<div id="container"></div>

2.利用cesium構造函數初始化地球

別忘瞭在data中定義空對象viewer

this.viewer = new Cesium.Viewer("container", {
      geocoder: false,
      timeline: false,
      fullscreenButton: false,
      animation: false,
      shouldAnimate: true,
    });

第四步:使用離線地圖資源替換網絡地圖資源

在構造函數中補充以下2個屬性:

this.viewer = new Cesium.Viewer("container", {
      imageryProvider: new Cesium.TileMapServiceImageryProvider({
        url: Cesium.buildModuleUrl("Assets/Textures/MyEarthII"),
      }),
      baseLayerPicker: false
    });

最後附上cesium官網對imageryProvider用法的解釋:

The imagery provider to use. This value is only valid if baseLayerPicker is set to false.

用於提供地圖圖像。僅當baseLayerPicker為false時有效。

cesium添加百度地圖

百度地圖的瓦塊切片規則與大多數地圖不同,其中心點位於地理坐標的0,0點,多數地圖的切片是以地圖左上角為瓦塊切片的起點。

Cesium中默認的切片地圖(UrlTemplateImageryProvider)包括經緯度模式和投影(墨卡托)模式都是以左上角切片為基準。所以當我們加載百度地圖瓦塊地圖時,需要自定義地圖影像地圖類。

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);
};

調用時,傳入url參數即可,url服務可以使在線服務,也可以是代理服務(按百度地圖切片索引規則)。例如:

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,
    ...
}

這時就可以在Cesium中正常加載百度地圖瞭。

總結

以上為個人經驗,希望能給大傢一個參考,也希望大傢多多支持WalkonNet。

推薦閱讀: