cesium开发之如何在vue项目中使用cesium,使用离线地图资源

 更新时间:2023年04月29日 11:25:06   作者:下一次就是永远  
这篇文章主要介绍了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中正常加载百度地图了。

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • 关于vue中媒体查询不起效的原因总结

    关于vue中媒体查询不起效的原因总结

    这篇文章主要介绍了关于vue中媒体查询不起效的原因总结,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-09-09
  • vue中注册组件的两种方式详解(全局注册&& 局部注册)

    vue中注册组件的两种方式详解(全局注册&& 局部注册)

    vue 是一个完全支持组件化开发的框架, 组件之间可以进行相互的引用,这篇文章主要介绍了vue中注册组件的两种方式详解(全局注册&& 局部注册),需要的朋友可以参考下
    2023-06-06
  • vue3中setup声明变量的方式汇总

    vue3中setup声明变量的方式汇总

    本文给大家介绍Vue3中setup()函数中声明变量的几种方法,希望本文能够帮助你更好地理解Vue3的开发方式,感兴趣的朋友跟随小编一起看看吧
    2023-11-11
  • 详解vue实现坐标拾取器功能示例

    详解vue实现坐标拾取器功能示例

    这篇文章主要介绍了详解vue实现坐标拾取器功能示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-11-11
  • 在vue中对数组值变化的监听与重新响应渲染操作

    在vue中对数组值变化的监听与重新响应渲染操作

    这篇文章主要介绍了在vue中对数组值变化的监听与重新响应渲染操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-07-07
  • Vuex unknown action type报错问题及解决

    Vuex unknown action type报错问题及解决

    这篇文章主要介绍了Vuex unknown action type报错问题及解决方案,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-02-02
  • vue3中setup语法糖下父子组件间传递数据的方式

    vue3中setup语法糖下父子组件间传递数据的方式

    Vue3中父组件指的是包含一个或多个子组件的组件,它们通过props和事件等方式来传递数据和通信,这篇文章主要介绍了vue3中setup语法糖下父子组件间传递数据的方式,需要的朋友可以参考下
    2023-06-06
  • vite+vue3+element-plus搭建项目的踩坑记录

    vite+vue3+element-plus搭建项目的踩坑记录

    这篇文章主要介绍了vite+vue3+element-plus搭建项目的踩坑记录,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-10-10
  • Vue开发项目中如何使用Font Awesome 5

    Vue开发项目中如何使用Font Awesome 5

    Font Awesome是一套流行的图标字体库,我们在实际开发的过程中会经常遇到需要使用图标的场景,对于一些常用的图标,我们可以直接在Font Awesome中找到并且使用,这篇文章主要给大家介绍了关于Vue开发项目中如何使用Font Awesome5的相关资料,需要的朋友可以参考下
    2021-11-11
  • 如何使用vue slot创建一个模态框的实例代码

    如何使用vue slot创建一个模态框的实例代码

    这篇文章主要介绍了如何使用vue slot创建一个模态框,本文通过实例代码给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-05-05

最新评论