1、安装cesiumJS、heatmap、webpack插件依赖包:

yarn install/npm install

"dependencies": {
...
"cesium": "^1.90.0",
"heatmap.js": "^2.0.5"
...
},
"devDependencies": {
...
"copy-webpack-plugin": "^5.0",
"html-webpack-plugin": "3.2.0",
...
}

2、cesium目录配置

项目是用vue-cli生成的,在vue.config.js中配置CesiumJS目录:

'use strict'
const path = require('path')
... function resolve(dir) {
return path.join(__dirname, dir)
} ... const CopyWebpackPlugin = require('copy-webpack-plugin')
const webpack = require('webpack')
const cesiumSource = 'node_modules/cesium/Source';
const cesiumWorkers = '../Build/Cesium/Workers'; // console.log('cesiumSource->',cesiumSource)
// console.log('cesiumWorkers->',cesiumWorkers)
// console.log('__dirName->', __dirname)
// console.log('cesium', path.resolve(__dirname, cesiumSource)) module.exports = {
publicPath: process.env.NODE_ENV === 'development' ? '/xxx' : './',
outputDir: 'dist',
assetsDir: 'static',
lintOnSave: process.env.NODE_ENV === 'development',
productionSourceMap: false,
devServer: {
port: 8888,
open: false,
overlay: {
warnings: false,
errors: true
},
proxy: {
...
}
},
configureWebpack: {
   ...
resolve: {
alias: {
'@': resolve('src'),'cesium': path.resolve(__dirname, cesiumSource) //CesiumSource绝对路径别名
}
},
plugins:
[
new CopyWebpackPlugin([{ from: path.join(cesiumSource, cesiumWorkers), to: 'Workers' }]),
new CopyWebpackPlugin([{ from: path.join(cesiumSource, 'Assets'), to: 'Assets' }]),
new CopyWebpackPlugin([{ from: path.join(cesiumSource, 'Widgets'), to: 'Widgets' }]),
new CopyWebpackPlugin([ { from: path.join(cesiumSource, 'ThirdParty/Workers'), to: 'ThirdParty/Workers'}]),
new webpack.DefinePlugin({
CESIUM_BASE_URL: JSON.stringify('')
})
],
},
...
}

项目页面JS中这样导包:

import * as Cesium from 'cesium/Cesium'
import 'cesium/Widgets/widgets.css'

3、开发热力图

直接参考https://github.com/manuelnas/CesiumHeatmap

由于此插件不支持es6导入,需要改源码(找了github上的其它支持es6 import方式导入的cesium热力图插件,存在一些未知的bug反而影响了开发效率,故还是自己整靠谱)

移除CesiumHeatmap.js中的h337源码,直接在文件头引入cesium和heatmap.js,如下:

import * as Cesium from 'cesium/Cesium'
import h337 from 'heatmap.js'

删掉IIEF及相关匿名函数包裹代码,直接暴露出CesiumHeatmap对象并导出,大致如下:

/**
* 修改自https://github.com/manuelnas/CesiumHeatmap
*/
import * as Cesium from 'cesium/Cesium'
import h337 from 'heatmap.js'
/*
* CesiumHeatmap.js v0.1 | Cesium Heatmap Library
*
* Works with heatmap.js v2.0.0: http://www.patrick-wied.at/static/heatmapjs/
*/
const CesiumHeatmap = {
defaults: {
...
CesiumHeatmap.rad2deg = function(r) {
var d = r / (Math.PI / 180.0)
return d
} /* Initiate a CesiumHeatmap instance
* c: CesiumWidget instance
* bb: a WGS84 bounding box like {north, east, south, west}
* o: a heatmap.js options object (see http://www.patrick-wied.at/static/heatmapjs/docs.html#h337-create)
*/
function CHInstance(c, bb, o) {
... export default CesiumHeatmap

4、页面中引入CesiumHeatmap并使用

把修改的cesiumHeatmap.js放入某个公共目录下,如src/utils/gis下面。

vue中引入使用:

...
import CesiumHeatmap from '@/utils/gis/cesiumHeatmap.js'
...
addHeatmap() {
const geojson = this.geojson
const points = []
let west = 1000; let east = -1000; let south = 1000; let north = -1000
geojson.features.forEach(function(feature) {
const lon = feature.geometry.coordinates[0]
const lat = feature.geometry.coordinates[1]
west = Math.min(west, lon)
east = Math.max(east, lon)
south = Math.min(south, lat)
north = Math.max(north, lat)
points.push({
x: lon,
y: lat,
value: 1
})
})
const bounds = {
west,
east,
south,
north
} // init heatmap
const heatMap = CesiumHeatmap.create(
this.viewer,
bounds,
{
minOpacity: 0.15,
maxOpacity: 0.8,
radius: 15,
blur: 0.9,
gradient: {
'.7': '#546AA4',
'.9': '#0DB212',
'.95': '#D6CE3B',
'.96': '#CC9C1B',
'.998': '#DC0F2A'
}
}
) const valueMin = 0
const valueMax = 10
heatMap.setWGS84Data(valueMin, valueMax, points)
this.heatmap = heatMap
},
...

最新文章

  1. python中怎么查看当前工作目录和更改工作目录
  2. Android Material Design的FloatingActionButton,Snackbar和CoordinatorLayout
  3. SingletonBeanRegistry
  4. [BZOJ 2165] 大楼 【DP + 倍增 + 二进制】
  5. Codeforces 543D Road Improvement
  6. 单片机脚本语言-移植lua到stm32-MDK
  7. [置顶] highcharts封装使用总结
  8. Oracle中如何插入特殊字符:& 和 ' (多种解决方案)
  9. ural1772 Ski-Trails for Robots
  10. 备忘:MySQL中修改表中某列的数据类型、删除外键约束
  11. python numpy 科学计算通用函数汇总
  12. 使用HM16.0对视频编码
  13. php获取数组最后一个值
  14. .Net Core 读取配置文件 appsettings.json
  15. Call to a member function display() on a non-object问题的解决
  16. Nginx+Keepalived双机热备
  17. Flume架构
  18. 动态创建生成lambd表达式
  19. Git-git rebase详解
  20. Xamarin/Mono IOS Limitations

热门文章

  1. 题解 P5072 【[Ynoi2015] 盼君勿忘】
  2. 初学 Canvas
  3. LeetCode-689 三个无重叠子数组的最大和
  4. js控制关闭layui的switch开关
  5. python collection Chainmap Counter
  6. 【11】java之抽象类
  7. 最火小游戏《羊了个羊》最新H5升级通关版
  8. 微信h5调分享功能
  9. Linux(CentOS) Mysql 8.0.30 安装(多源安装)
  10. 函数:3ds max 给选择对象设置轴心点